Logo ROOT  
Reference Guide
TypeTraits.hxx
Go to the documentation of this file.
1 // @(#)root/foundation:
2 // Author: Axel Naumann, Enrico Guiraud, June 2017
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2017, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TypeTraits
13 #define ROOT_TypeTraits
14 
15 #include <memory> // shared_ptr, unique_ptr for IsSmartOrDumbPtr
16 #include <type_traits>
17 
18 namespace ROOT {
19 
20 /// ROOT type_traits extensions
21 namespace TypeTraits {
22 /// Lightweight storage for a collection of types.
23 /// Differently from std::tuple, no instantiation of objects of stored types is performed
24 template <typename... Types>
25 struct TypeList {
26  static constexpr std::size_t list_size = sizeof...(Types);
27 };
28 } // end ns TypeTraits
29 
30 namespace Detail {
31 template <typename T> constexpr auto HasCallOp(int /*goodOverload*/) -> decltype(&T::operator(), true) { return true; }
32 template <typename T> constexpr bool HasCallOp(char /*badOverload*/) { return false; }
33 
34 /// Extract types from the signature of a callable object. See CallableTraits.
35 template <typename T, bool HasCallOp = ROOT::Detail::HasCallOp<T>(0)>
37 
38 // Extract signature of operator() and delegate to the appropriate CallableTraitsImpl overloads
39 template <typename T>
40 struct CallableTraitsImpl<T, true> {
41  using arg_types = typename CallableTraitsImpl<decltype(&T::operator())>::arg_types;
42  using arg_types_nodecay = typename CallableTraitsImpl<decltype(&T::operator())>::arg_types_nodecay;
43  using ret_type = typename CallableTraitsImpl<decltype(&T::operator())>::ret_type;
44 };
45 
46 // lambdas, std::function, const member functions
47 template <typename R, typename T, typename... Args>
48 struct CallableTraitsImpl<R (T::*)(Args...) const, false> {
51  using ret_type = R;
52 };
53 
54 // mutable lambdas and functor classes, non-const member functions
55 template <typename R, typename T, typename... Args>
56 struct CallableTraitsImpl<R (T::*)(Args...), false> {
59  using ret_type = R;
60 };
61 
62 // function pointers
63 template <typename R, typename... Args>
64 struct CallableTraitsImpl<R (*)(Args...), false> {
67  using ret_type = R;
68 };
69 
70 // free functions
71 template <typename R, typename... Args>
72 struct CallableTraitsImpl<R(Args...), false> {
75  using ret_type = R;
76 };
77 } // end ns Detail
78 
79 namespace TypeTraits {
80 
81 ///\class ROOT::TypeTraits::
82 template <class T>
83 class IsSmartOrDumbPtr : public std::integral_constant<bool, std::is_pointer<T>::value> {
84 };
85 
86 template <class P>
87 class IsSmartOrDumbPtr<std::shared_ptr<P>> : public std::true_type {
88 };
89 
90 template <class P>
91 class IsSmartOrDumbPtr<std::unique_ptr<P>> : public std::true_type {
92 };
93 
94 /// Checks for signed integers types that are not characters
95 template<class T>
96 struct IsSignedNumeral : std::integral_constant<bool,
97  std::is_integral<T>::value &&
98  std::is_signed<T>::value &&
99  !std::is_same<T, char>::value
100 > {};
101 
102 /// Checks for unsigned integer types that are not characters
103 template<class T>
104 struct IsUnsignedNumeral : std::integral_constant<bool,
105  std::is_integral<T>::value &&
106  !std::is_signed<T>::value &&
107  !std::is_same<T, char>::value
108 > {};
109 
110 /// Checks for floating point types (that are not characters)
111 template<class T>
112 using IsFloatNumeral = std::is_floating_point<T>;
113 
114 /// Extract types from the signature of a callable object.
115 /// The `CallableTraits` struct contains three type aliases:
116 /// - arg_types: a `TypeList` of all types in the signature, decayed through std::decay
117 /// - arg_types_nodecay: a `TypeList` of all types in the signature, including cv-qualifiers
118 template<typename F>
120 
121 // Return first of a variadic list of types.
122 template <typename T, typename... Rest>
124  using type = T;
125 };
126 
127 template <typename... Types>
128 using TakeFirstType_t = typename TakeFirstType<Types...>::type;
129 
130 // Remove first type from a variadic list of types, return a TypeList containing the rest.
131 // e.g. RemoveFirst_t<A,B,C> is TypeList<B,C>
132 template <typename T, typename... Rest>
133 struct RemoveFirst {
134  using type = TypeList<Rest...>;
135 };
136 
137 template <typename... Args>
138 using RemoveFirst_t = typename RemoveFirst<Args...>::type;
139 
140 /// Return first of possibly many template parameters.
141 /// For non-template types, the result is void
142 /// e.g. TakeFirstParameter<U<A,B>> is A
143 /// TakeFirstParameter<T> is void
144 template <typename T>
146  using type = void;
147 };
148 
149 template <template <typename...> class Template, typename T, typename... Rest>
150 struct TakeFirstParameter<Template<T, Rest...>> {
151  using type = T;
152 };
153 
154 template <typename T>
156 
157 /// Remove first of possibly many template parameters.
158 /// e.g. RemoveFirstParameter_t<U<A,B>> is U<B>
159 template <typename>
161 };
162 
163 template <typename T, template <typename...> class U, typename... Rest>
164 struct RemoveFirstParameter<U<T, Rest...>> {
165  using type = U<Rest...>;
166 };
167 
168 template <typename T>
170 
171 template <typename T>
173 
174  template <typename V>
175  using Begin_t = typename V::const_iterator (V::*)() const;
176 
177  template <typename V>
178  using End_t = typename V::const_iterator (V::*)() const;
179 
180  template <typename V>
181  static constexpr auto Check(int)
182  -> decltype(static_cast<Begin_t<V>>(&V::begin), static_cast<End_t<V>>(&V::end), true)
183  {
184  return true;
185  }
186 
187  template <typename V>
188  static constexpr bool Check(...)
189  {
190  return false;
191  }
192 
193  static constexpr bool const value = Check<T>(0);
194 };
195 
196 } // ns TypeTraits
197 } // ns ROOT
198 #endif // ROOT_TTypeTraits
TypeTraits::IsFloatNumeral
std::is_floating_point< T > IsFloatNumeral
Checks for floating point types (that are not characters)
Definition: TypeTraits.hxx:112
ROOT::TypeTraits::HasBeginAndEnd::value
static constexpr bool const value
Definition: TypeTraits.hxx:193
ROOT::Detail::CallableTraitsImpl< T, true >::ret_type
typename CallableTraitsImpl< decltype(&T::operator())>::ret_type ret_type
Definition: TypeTraits.hxx:43
TypeTraits::RemoveFirst_t
typename RemoveFirst< Args... >::type RemoveFirst_t
Definition: TypeTraits.hxx:138
ROOT::Detail::CallableTraitsImpl
Extract types from the signature of a callable object. See CallableTraits.
Definition: TypeTraits.hxx:36
ROOT::TypeTraits::IsSmartOrDumbPtr
Definition: TypeTraits.hxx:83
ROOT::Detail::HasCallOp
constexpr auto HasCallOp(int) -> decltype(&T::operator(), true)
Definition: TypeTraits.hxx:31
ROOT::TypeTraits::HasBeginAndEnd::End_t
typename V::const_iterator(V::*)() const End_t
Definition: TypeTraits.hxx:178
ROOT::TypeTraits::TypeList
Lightweight storage for a collection of types.
Definition: TypeTraits.hxx:25
ROOT::Detail::CallableTraitsImpl< T, true >::arg_types_nodecay
typename CallableTraitsImpl< decltype(&T::operator())>::arg_types_nodecay arg_types_nodecay
Definition: TypeTraits.hxx:42
TypeTraits::TakeFirstType_t
typename TakeFirstType< Types... >::type TakeFirstType_t
Definition: TypeTraits.hxx:128
TypeTraits::TakeFirstParameter_t
typename TakeFirstParameter< T >::type TakeFirstParameter_t
Definition: TypeTraits.hxx:155
TypeTraits::RemoveFirstParameter_t
typename RemoveFirstParameter< T >::type RemoveFirstParameter_t
Definition: TypeTraits.hxx:169
ROOT::TypeTraits::TypeList::list_size
static constexpr std::size_t list_size
Definition: TypeTraits.hxx:26
ROOT::TypeTraits::RemoveFirst
Definition: TypeTraits.hxx:133
TypeTraits
ROOT::Detail::CallableTraitsImpl< R(Args...), false >::ret_type
R ret_type
Definition: TypeTraits.hxx:75
ROOT::TypeTraits::HasBeginAndEnd::Check
static constexpr auto Check(int) -> decltype(static_cast< Begin_t< V >>(&V::begin), static_cast< End_t< V >>(&V::end), true)
Definition: TypeTraits.hxx:181
R
#define R(a, b, c, d, e, f, g, h, i)
Definition: RSha256.hxx:110
ROOT::TypeTraits::IsSignedNumeral
Checks for signed integers types that are not characters.
Definition: TypeTraits.hxx:100
ROOT::Detail::CallableTraitsImpl< R(*)(Args...), false >::ret_type
R ret_type
Definition: TypeTraits.hxx:67
ROOT::Detail::CallableTraitsImpl< R(T::*)(Args...), false >::ret_type
R ret_type
Definition: TypeTraits.hxx:59
ROOT::Detail::CallableTraitsImpl< T, true >::arg_types
typename CallableTraitsImpl< decltype(&T::operator())>::arg_types arg_types
Definition: TypeTraits.hxx:41
ROOT::TypeTraits::HasBeginAndEnd::Check
static constexpr bool Check(...)
Definition: TypeTraits.hxx:188
void
typedef void((*Func_t)())
ROOT::TypeTraits::HasBeginAndEnd::Begin_t
typename V::const_iterator(V::*)() const Begin_t
Definition: TypeTraits.hxx:175
ROOT::TypeTraits::HasBeginAndEnd
Definition: TypeTraits.hxx:172
ROOT::TypeTraits::TakeFirstParameter
Return first of possibly many template parameters.
Definition: TypeTraits.hxx:145
ROOT::TypeTraits::RemoveFirstParameter
Remove first of possibly many template parameters.
Definition: TypeTraits.hxx:160
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:34
ROOT::TypeTraits::TakeFirstParameter::type
void type
Definition: TypeTraits.hxx:146
ROOT::TypeTraits::TakeFirstType
Definition: TypeTraits.hxx:123
ROOT::TypeTraits::TakeFirstParameter< Template< T, Rest... > >::type
T type
Definition: TypeTraits.hxx:151
type
int type
Definition: TGX11.cxx:121
ROOT::TypeTraits::TakeFirstType::type
T type
Definition: TypeTraits.hxx:124
ROOT::Detail::CallableTraitsImpl< R(T::*)(Args...) const, false >::ret_type
R ret_type
Definition: TypeTraits.hxx:51
ROOT
VSD Structures.
Definition: StringConv.hxx:21
ROOT::TypeTraits::IsUnsignedNumeral
Checks for unsigned integer types that are not characters.
Definition: TypeTraits.hxx:108
ROOT::TypeTraits::RemoveFirstParameter< U< T, Rest... > >::type
U< Rest... > type
Definition: TypeTraits.hxx:165