HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SYS_TypeTraits.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: SYS_TypeTraits.h (SYS Library, C++)
7  *
8  * COMMENTS: Type trait utilities
9  */
10 
11 #ifndef __SYS_TYPETRAITS_H_INCLUDED__
12 #define __SYS_TYPETRAITS_H_INCLUDED__
13 
14 #include "SYS_TypeDecorate.h"
15 #include "SYS_Inline.h"
16 #include <type_traits>
17 
18 /// @file
19 
20 // In C++17, this could be std::bool_constant
21 template< bool v >
22 struct SYS_BoolConstant : std::integral_constant< bool, v > {};
23 
26 
27 /// Alternative for C++17's std::void that can be used in C++14:
28 template< typename ... >
29 using SYS_Void_t = void;
30 
31 /// SYS_IsIntegral is true if std::is_integral
32 /// but is also true for additional types,
33 /// which may be declared using SYS_DECLARE_IS_INTEGRAL
34 template< typename T >
36  std::is_integral< T >::value ||
37  AddIntegralNoCV< std::remove_cv_t< T > >::value
38 > {};
39 
40 template< typename T >
42 
43 /// SYS_IsFloatingPoint is true if std::is_floating_point,
44 /// but is also true for additional types,
45 /// which may be declared using SYS_DECLARE_IS_FLOATING_POINT
46 template< typename T >
48  std::is_floating_point< T >::value ||
49  AddFloatingPointNoCV< std::remove_cv_t< T > >::value
50 > {};
51 
52 template< typename T >
54 
55 /// SYS_IsPod is true if std::is_pod,
56 /// but is also true for additional types,
57 /// which may be declared using SYS_DECLARE_IS_POD
58 template< typename T >
60  std::is_pod< T >::value ||
61  AddPodNoCV< std::remove_cv_t< T > >::value
62 > {};
63 
64 template< typename T >
66 
67 /// Check whether a given type is plain-old-data (POD)
68 template <typename T>
69 static constexpr SYS_FORCE_INLINE bool SYSisPOD()
70 {
71  return SYS_IsPod_v< T >;
72 }
73 
74 /// If SYS_IsTriviallyRelocatable is true, then it should be safe
75 /// to bitwise copy an object of type T to a different memory location.
76 /// This can be declared using the macro SYS_DECLARE_IS_TR
77 template< typename T >
79  std::is_fundamental< T >::value ||
80  std::is_enum< T >::value ||
81  std::is_pointer< T >::value ||
82  SafeTrivialRelocationNoCV< std::remove_cv_t< T > >::value
83 > {};
84 
85 template< typename T >
87 
88 /// Check whether a type is arithmetic (integer or floating-point type),
89 /// which means SYS_IsIntegral or SYS_IsFloatingPoint
90 /// This struct should not be specialized; instead use
91 /// SYS_DECLARE_IS_INTEGRAL and SYS_DECLARE_IS_FLOATING_POINT
92 template <typename T>
94  ( SYS_IsIntegral< T >::value || SYS_IsFloatingPoint< T >::value )
95 > {};
96 
97 template< typename T >
99 
100 /// Check whether an arithmetic type is signed.
101 template <typename T>
102 struct SYS_IsSigned : public std::is_signed<T> {};
103 
104 template <typename T>
106 
107 /// Check whether two types are the same
108 /// @{
109 
110 // Primary: assume not the same by default
111 template< typename T, typename U >
113 
114 // Partial specialization for two the same types
115 template< typename T >
116 struct SYS_IsSame< T, T > : SYS_TrueType {};
117 
118 template< typename T, typename U >
120 
121 template <typename T1, typename T2>
122 static constexpr SYS_FORCE_INLINE bool SYSisSame()
123 {
124  return SYS_IsSame_v< T1, T2 >;
125 }
126 
127 /// @}
128 
129 /// Check whether a given type is a pointer
130 /// @{
131 
132 template <typename T>
133 struct SYS_IsPointer : SYS_BoolConstant< false > {};
134 
135 template <typename U>
137 
138 template <typename T>
140 
141 /// @}
142 
143 /// Map a type to itself.
144 /// Useful for specifying the result of a type metafunction;
145 /// simply inherit from SYS_TypeIdentity<ResultType>
146 /// @{
147 
148 template< typename T >
149 struct SYS_TypeIdentity { using type = T; };
150 
151 template< typename T >
153 
154 /// @}
155 
156 /// Remove reference, if T is a reference type.
157 /// After that, remove the topmost cv-qualifiers.
158 /// @{
159 
160 template< typename T >
162  SYS_RemoveCV_t< std::remove_reference_t< T > >
163 > {};
164 
165 template< typename T >
167 
168 /// @}
169 
170 /// Evaluates to true if
171 /// T is not a reference and
172 /// T has no const/volatile qualifiers.
173 /// Otherwise evaluates to false.
174 /// @{
175 
176 template< typename T >
178  std::is_same< T, SYS_RemoveCVRef_t< T > >::value
179 > {};
180 
181 template< typename T >
183 
184 /// @}
185 
186 /// Choose whether to make a type const or not with a template bool.
187 /// This allows a set of types to be made const or not with a single
188 /// bool template, instead of having a separate template parameter for
189 /// each type.
190 /// @{
191 
192 template<typename T,bool MAKE_CONST>
194 
195 template<typename T>
196 struct SYS_ConstType< T, true > : SYS_TypeIdentity< const T > {};
197 
198 template<typename T,bool MAKE_CONST>
200 
201 /// @}
202 
203 /// Choose a type from two alternatives based on a bool.
204 /// It choses the first type if false, and the second type if true.
205 /// @{
206 
207 template<typename T0,typename T1,bool IST1>
209 
210 template<typename T0,typename T1>
211 struct SYS_SelectType< T0, T1, true > : SYS_TypeIdentity< T1 > {};
212 
213 template<typename T0,typename T1,bool IST1>
215 
216 /// @}
217 
218 /// Hides a function overload or template specialization based on a
219 /// compile-time boolean.
220 /// {@
221 template <bool B, typename T = void>
222 struct SYS_EnableIf : public std::enable_if<B, T> {};
223 
224 template <bool B, typename T = void>
226 
227 template <bool B, typename T = void>
228 struct SYS_DisableIf : public SYS_EnableIf<!B, T> {};
229 
230 template <bool B, typename T = void>
232 /// @}
233 
234 /// Return the number of elements of a static array
235 template <typename T, std::size_t N>
236 static SYS_FORCE_INLINE constexpr auto
237 SYSarraySize(const T (&)[N]) noexcept
238 {
239  return N; // same as std::extent<T>::value
240 }
241 
242 ///
243 /// Determine whether a type is designated as a fixed-array-like type;
244 /// its must have a compile-time size and support square-bracket indexing.
245 ///
246 /// Examples of such types include a built-in bounded array and std::array< T, N >.
247 ///
248 /// Specialize SYS_IsFixedArrayNoCVRef to add support to additional types.
249 /// {@
250 
251 template< typename T >
252 struct SYS_IsFixedArray : SYS_IsFixedArrayNoCVRef< SYS_RemoveCVRef_t< T > > {};
253 
254 template< typename T >
256 
257 /// @}
258 
259 /// Extracts the element type of a fixed-array-like type.
260 /// Specialize SYS_FixedArrayElementNoCVRef to add support to additional types.
261 /// {@
262 
263 template< typename T >
264 struct SYS_FixedArrayElement : SYS_FixedArrayElementNoCVRef< SYS_RemoveCVRef_t< T > > {};
265 
266 template< typename T >
268 
269 /// @}
270 
271 ///
272 /// Obtain the size of a fixed-array-like type.
273 /// Specialize SYS_FixedArraySizeNoCVRef to add support to additional types.
274 /// {@
275 
276 template< typename T >
277 struct SYS_FixedArraySize : SYS_FixedArraySizeNoCVRef< SYS_RemoveCVRef_t< T > > {};
278 
279 template< typename T >
281 
282 /// @}
283 
284 ///
285 /// Determine whether T, with const and volatile qualifiers removed,
286 /// is a fixed-array-like type that has element type E and size N.
287 /// The CV qualifiers of the element type E are not discarded and do matter.
288 /// {@
289 ///
290 
291 template< typename T, typename E, std::size_t N >
293  SYS_IsFixedArray_v< T > &&
294  ( std::is_same< SYS_FixedArrayElement_t< std::remove_cv_t< T > >, E >::value ) &&
295  ( SYS_FixedArraySize_v< T > == N )
296  > {};
297 
298 template< typename T, typename E, std::size_t N >
300 
301 /// @}
302 
303 
304 ///
305 /// Same as SYS_IsFixedArrayOf, except that it is checked that
306 /// the element type of the array *with CV qualifiers removed* is the same as E.
307 /// E must not have any CV qualifiers.
308 /// {@
309 ///
310 
311 template< typename T, typename E, std::size_t N >
313  SYS_IsFixedArray_v< T > &&
314  ( std::is_same< std::remove_cv_t< SYS_FixedArrayElement_t< std::remove_cv_t< T > > >, E >::value ) &&
315  ( SYS_FixedArraySize_v< T > == N )
316 >
317 {
318  static_assert( SYS_HasNoCVRef_v< E > );
319 };
320 
321 template< typename T, typename E, std::size_t N >
323 
324 /// @}
325 
326 ///
327 /// Determine whether the element type of T is equal to E.
328 /// T must be a fixed array-like type.
329 
330 /// {@
331 ///
332 
333 template< typename T, typename E >
335  SYS_IsSame< SYS_FixedArrayElement_t< T >, E >
336 {
337  static_assert( SYS_IsFixedArray_v< T > );
338 };
339 
340 template< typename T, typename E >
342 
343 /// @}
344 
345 ///
346 /// Determine whether the element type of T * with CV qualifiers removed *
347 /// is equal to E.
348 /// T must be a fixed array-like type and E must not have any CV qualifiers.
349 
350 /// {@
351 ///
352 
353 template< typename T, typename E >
355  SYS_IsSame< SYS_RemoveCV_t< SYS_FixedArrayElement_t< T > >, E >
356 {
357  static_assert( SYS_IsFixedArray_v< T > );
358  static_assert( SYS_HasNoCVRef_v< E > );
359 };
360 
361 template< typename T, typename E >
363 
364 /// @}
365 
366 // Specialize FixedArray traits for built-in bounded arrays
367 
368 template< typename T, std::size_t N >
369 struct SYS_IsFixedArrayNoCVRef< T[N] > : SYS_BoolConstant< true > {};
370 
371 template< typename T, std::size_t N >
373 
374 template< typename T, std::size_t N >
375 struct SYS_FixedArraySizeNoCVRef< T[N] > : std::integral_constant< std::size_t, N > {};
376 
377 #endif // __SYS_TYPETRAITS_H_INCLUDED__
constexpr auto SYS_IsIntegral_v
constexpr auto SYS_IsPod_v
void
Definition: png.h:1083
typename SYS_TypeIdentity< T >::type SYS_TypeIdentity_t
void SYS_Void_t
Alternative for C++17's std::void that can be used in C++14:
typename SYS_FixedArrayElement< T >::type SYS_FixedArrayElement_t
constexpr auto SYS_IsSame_v
constexpr bool SYS_FixedArrayHasElementNoCV_v
typename SYS_DisableIf< B, T >::type SYS_DisableIf_t
typename SYS_RemoveCVRef< T >::type SYS_RemoveCVRef_t
std::integral_constant< bool, std::numeric_limits< T >::is_signed||std::is_same< T, int128_t >::value > is_signed
Definition: format.h:821
constexpr bool SYS_IsFixedArray_v
typename SYS_EnableIf< B, T >::type SYS_EnableIf_t
constexpr auto SYS_IsFloatingPoint_v
typename SYS_ConstType< T, MAKE_CONST >::type SYS_ConstType_t
constexpr auto SYS_HasNoCVRef_v
Check whether an arithmetic type is signed.
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
SYS_RemoveCV_t< std::remove_reference_t< T > > type
typename SYS_SelectType< T0, T1, IST1 >::type SYS_SelectType_t
constexpr bool SYS_FixedArrayHasElement_v
constexpr bool SYS_IsFixedArrayOfNoCV_v
constexpr auto SYS_IsPointer_v
constexpr bool SYS_IsFixedArrayOf_v
constexpr auto SYS_IsTriviallyRelocatable_v
constexpr auto SYS_IsArithmetic_v
GA_API const UT_StringHolder N
constexpr std::size_t SYS_FixedArraySize_v
constexpr auto SYS_IsSigned_v