Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
18namespace ROOT {
19
20/// ROOT type_traits extensions
21namespace TypeTraits {
22/// Lightweight storage for a collection of types.
23/// Differently from std::tuple, no instantiation of objects of stored types is performed
24template <typename... Types>
25struct TypeList {
26 static constexpr std::size_t list_size = sizeof...(Types);
27};
28} // end ns TypeTraits
29
30namespace Detail {
31template <typename T> constexpr auto HasCallOp(int /*goodOverload*/) -> decltype(&T::operator(), true) { return true; }
32template <typename T> constexpr bool HasCallOp(char /*badOverload*/) { return false; }
33
34/// Extract types from the signature of a callable object. See CallableTraits.
35template <typename T, bool HasCallOp = ROOT::Detail::HasCallOp<T>(0)>
37
38// Extract signature of operator() and delegate to the appropriate CallableTraitsImpl overloads
39template <typename T>
40struct 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
47template <typename R, typename T, typename... Args>
48struct CallableTraitsImpl<R (T::*)(Args...) const, false> {
51 using ret_type = R;
52};
53
54// mutable lambdas and functor classes, non-const member functions
55template <typename R, typename T, typename... Args>
56struct CallableTraitsImpl<R (T::*)(Args...), false> {
59 using ret_type = R;
60};
61
62// function pointers
63template <typename R, typename... Args>
64struct CallableTraitsImpl<R (*)(Args...), false> {
67 using ret_type = R;
68};
69
70// free functions
71template <typename R, typename... Args>
72struct CallableTraitsImpl<R(Args...), false> {
75 using ret_type = R;
76};
77} // end ns Detail
78
79namespace TypeTraits {
80
81///\class ROOT::TypeTraits::
82template <class T>
83class IsSmartOrDumbPtr : public std::integral_constant<bool, std::is_pointer<T>::value> {
84};
85
86template <class P>
87class IsSmartOrDumbPtr<std::shared_ptr<P>> : public std::true_type {
88};
89
90template <class P>
91class IsSmartOrDumbPtr<std::unique_ptr<P>> : public std::true_type {
92};
93
94/// Checks for signed integers types that are not characters
95template<class T>
96struct 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
103template<class T>
104struct 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)
111template<class T>
112using 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
118template<typename F>
120
121// Return first of a variadic list of types.
122template <typename T, typename... Rest>
124 using type = T;
125};
126
127template <typename... Types>
128using 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>
132template <typename T, typename... Rest>
134 using type = TypeList<Rest...>;
135};
136
137template <typename... Args>
138using 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
144template <typename T>
146 using type = void;
147};
148
149template <template <typename...> class Template, typename T, typename... Rest>
150struct TakeFirstParameter<Template<T, Rest...>> {
151 using type = T;
152};
153
154template <typename T>
156
157/// Remove first of possibly many template parameters.
158/// e.g. RemoveFirstParameter_t<U<A,B>> is U<B>
159template <typename>
161};
162
163template <typename T, template <typename...> class U, typename... Rest>
164struct RemoveFirstParameter<U<T, Rest...>> {
165 using type = U<Rest...>;
166};
167
168template <typename T>
169using RemoveFirstParameter_t = typename RemoveFirstParameter<T>::type;
170
171template <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
typedef void(GLAPIENTRYP _GLUfuncptr)(void)
#define R(a, b, c, d, e, f, g, h, i)
Definition RSha256.hxx:110
int type
Definition TGX11.cxx:121
typename TakeFirstParameter< T >::type TakeFirstParameter_t
typename RemoveFirst< Args... >::type RemoveFirst_t
std::is_floating_point< T > IsFloatNumeral
Checks for floating point types (that are not characters)
typename TakeFirstType< Types... >::type TakeFirstType_t
typename RemoveFirstParameter< T >::type RemoveFirstParameter_t
constexpr auto HasCallOp(int) -> decltype(&T::operator(), true)
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
typename CallableTraitsImpl< decltype(&T::operator())>::arg_types arg_types
typename CallableTraitsImpl< decltype(&T::operator())>::arg_types_nodecay arg_types_nodecay
typename CallableTraitsImpl< decltype(&T::operator())>::ret_type ret_type
Extract types from the signature of a callable object. See CallableTraits.
typename V::const_iterator(V::*)() const Begin_t
static constexpr bool Check(...)
static constexpr auto Check(int) -> decltype(static_cast< Begin_t< V > >(&V::begin), static_cast< End_t< V > >(&V::end), true)
typename V::const_iterator(V::*)() const End_t
static constexpr bool const value
Checks for signed integers types that are not characters.
Checks for unsigned integer types that are not characters.
Remove first of possibly many template parameters.
Return first of possibly many template parameters.
Lightweight storage for a collection of types.
static constexpr std::size_t list_size