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_standard_layout< T >::value
61  && std::is_trivially_copyable< T >::value
62  && std::is_trivially_default_constructible< T >::value)
63  || AddPodNoCV< std::remove_cv_t< T > >::value
64 > {};
65 
66 template< typename T >
68 
69 /// Check whether a given type is plain-old-data (POD)
70 template <typename T>
71 static constexpr SYS_FORCE_INLINE bool SYSisPOD()
72 {
73  return SYS_IsPod_v< T >;
74 }
75 
76 /// If SYS_IsTriviallyRelocatable is true, then it should be safe
77 /// to bitwise copy an object of type T to a different memory location.
78 /// This can be declared using the macro SYS_DECLARE_IS_TR
79 template< typename T >
81  std::is_fundamental< T >::value ||
82  std::is_enum< T >::value ||
83  std::is_pointer< T >::value ||
84  SafeTrivialRelocationNoCV< std::remove_cv_t< T > >::value
85 > {};
86 
87 template< typename T >
89 
90 /// Check whether a type is arithmetic (integer or floating-point type),
91 /// which means SYS_IsIntegral or SYS_IsFloatingPoint
92 /// This struct should not be specialized; instead use
93 /// SYS_DECLARE_IS_INTEGRAL and SYS_DECLARE_IS_FLOATING_POINT
94 template <typename T>
96  ( SYS_IsIntegral< T >::value || SYS_IsFloatingPoint< T >::value )
97 > {};
98 
99 template< typename T >
101 
102 /// Check whether an arithmetic type is signed.
103 template <typename T>
104 struct SYS_IsSigned : public std::is_signed<T> {};
105 
106 template <typename T>
108 
109 /// Check whether two types are the same
110 /// @{
111 
112 // Primary: assume not the same by default
113 template< typename T, typename U >
115 
116 // Partial specialization for two the same types
117 template< typename T >
118 struct SYS_IsSame< T, T > : SYS_TrueType {};
119 
120 template< typename T, typename U >
122 
123 template <typename T1, typename T2>
124 static constexpr SYS_FORCE_INLINE bool SYSisSame()
125 {
126  return SYS_IsSame_v< T1, T2 >;
127 }
128 
129 /// @}
130 
131 /// Check whether a given type is a pointer
132 /// @{
133 
134 template <typename T>
135 struct SYS_IsPointer : SYS_BoolConstant< false > {};
136 
137 template <typename U>
139 
140 template <typename T>
142 
143 /// @}
144 
145 /// Map a type to itself.
146 /// Useful for specifying the result of a type metafunction;
147 /// simply inherit from SYS_TypeIdentity<ResultType>
148 /// @{
149 
150 template< typename T >
151 struct SYS_TypeIdentity { using type = T; };
152 
153 template< typename T >
155 
156 /// @}
157 
158 /// Remove reference, if T is a reference type.
159 /// After that, remove the topmost cv-qualifiers.
160 /// @{
161 
162 template< typename T >
164  SYS_RemoveCV_t< std::remove_reference_t< T > >
165 > {};
166 
167 template< typename T >
169 
170 /// @}
171 
172 /// Evaluates to true if
173 /// T is not a reference and
174 /// T has no const/volatile qualifiers.
175 /// Otherwise evaluates to false.
176 /// @{
177 
178 template< typename T >
180  std::is_same< T, SYS_RemoveCVRef_t< T > >::value
181 > {};
182 
183 template< typename T >
185 
186 /// @}
187 
188 /// Choose whether to make a type const or not with a template bool.
189 /// This allows a set of types to be made const or not with a single
190 /// bool template, instead of having a separate template parameter for
191 /// each type.
192 /// @{
193 
194 template<typename T,bool MAKE_CONST>
196 
197 template<typename T>
198 struct SYS_ConstType< T, true > : SYS_TypeIdentity< const T > {};
199 
200 template<typename T,bool MAKE_CONST>
202 
203 /// @}
204 
205 /// Given a type T, return the same type but with the opposite constness
206 /// @{
207 
208 // Primary
209 template< typename T >
210 struct SYS_ReverseConst : SYS_TypeIdentity< const T > {};
211 
212 // Partially specialize for const T
213 template< typename T >
214 struct SYS_ReverseConst< const T > : SYS_TypeIdentity< T > {};
215 
216 // Convenience
217 template< typename T >
219 
220 /// @}
221 
222 /// If add_const is true, then add constness to T
223 /// @{
224 
225 // Primary
226 template< bool add_const, typename T >
228  std::conditional_t< add_const, std::add_const_t< T >, T >
229 > {};
230 
231 template< bool add_const, typename T >
233 
234 /// @}
235 
236 /// If the type U is const qualified, then add constness to T
237 /// @{
238 
239 // Primary
240 template< typename U, typename T >
241 struct SYS_TransferConst : SYS_TypeIdentity< SYS_ConditionalAddConst_t< std::is_const_v< U >, T > > {};
242 
243 template< typename U, typename T >
245 
246 /// @}
247 
248 /// Choose a type from two alternatives based on a bool.
249 /// It choses the first type if false, and the second type if true.
250 /// @{
251 
252 template<typename T0,typename T1,bool IST1>
254 
255 template<typename T0,typename T1>
256 struct SYS_SelectType< T0, T1, true > : SYS_TypeIdentity< T1 > {};
257 
258 template<typename T0,typename T1,bool IST1>
260 
261 /// @}
262 
263 /// Hides a function overload or template specialization based on a
264 /// compile-time boolean.
265 /// {@
266 template <bool B, typename T = void>
267 struct SYS_EnableIf : public std::enable_if<B, T> {};
268 
269 template <bool B, typename T = void>
271 
272 template <bool B, typename T = void>
273 struct SYS_DisableIf : public SYS_EnableIf<!B, T> {};
274 
275 template <bool B, typename T = void>
277 /// @}
278 
279 /// Return the number of elements of a static array
280 template <typename T, std::size_t N>
281 static SYS_FORCE_INLINE constexpr auto
282 SYSarraySize(const T (&)[N]) noexcept
283 {
284  return N; // same as std::extent<T>::value
285 }
286 
287 ///
288 /// Determine whether a type is designated as a fixed-array-like type;
289 /// its must have a compile-time size and support square-bracket indexing.
290 ///
291 /// Examples of such types include a built-in bounded array and std::array< T, N >.
292 ///
293 /// Specialize SYS_IsFixedArrayNoCVRef to add support to additional types.
294 /// {@
295 
296 template< typename T >
297 struct SYS_IsFixedArray : SYS_IsFixedArrayNoCVRef< SYS_RemoveCVRef_t< T > > {};
298 
299 template< typename T >
301 
302 /// @}
303 
304 /// Extracts the element type of a fixed-array-like type.
305 /// Specialize SYS_FixedArrayElementNoCVRef to add support to additional types.
306 /// {@
307 
308 template< typename T >
309 struct SYS_FixedArrayElement : SYS_FixedArrayElementNoCVRef< SYS_RemoveCVRef_t< T > > {};
310 
311 template< typename T >
313 
314 /// @}
315 
316 ///
317 /// Obtain the size of a fixed-array-like type.
318 /// Specialize SYS_FixedArraySizeNoCVRef to add support to additional types.
319 /// {@
320 
321 template< typename T >
322 struct SYS_FixedArraySize : SYS_FixedArraySizeNoCVRef< SYS_RemoveCVRef_t< T > > {};
323 
324 template< typename T >
326 
327 /// @}
328 
329 ///
330 /// Determine whether T, with const and volatile qualifiers removed,
331 /// is a fixed-array-like type that has element type E and size N.
332 /// The CV qualifiers of the element type E are not discarded and do matter.
333 /// {@
334 ///
335 
336 template< typename T, typename E, std::size_t N >
338  SYS_IsFixedArray_v< T > &&
339  ( std::is_same< SYS_FixedArrayElement_t< std::remove_cv_t< T > >, E >::value ) &&
340  ( SYS_FixedArraySize_v< T > == N )
341  > {};
342 
343 template< typename T, typename E, std::size_t N >
345 
346 /// @}
347 
348 
349 ///
350 /// Same as SYS_IsFixedArrayOf, except that it is checked that
351 /// the element type of the array *with CV qualifiers removed* is the same as E.
352 /// E must not have any CV qualifiers.
353 /// {@
354 ///
355 
356 template< typename T, typename E, std::size_t N >
358  SYS_IsFixedArray_v< T > &&
359  ( std::is_same< std::remove_cv_t< SYS_FixedArrayElement_t< std::remove_cv_t< T > > >, E >::value ) &&
360  ( SYS_FixedArraySize_v< T > == N )
361 >
362 {
363  static_assert( SYS_HasNoCVRef_v< E > );
364 };
365 
366 template< typename T, typename E, std::size_t N >
368 
369 /// @}
370 
371 ///
372 /// Determine whether the element type of T is equal to E.
373 /// T must be a fixed array-like type.
374 
375 /// {@
376 ///
377 
378 template< typename T, typename E >
380  SYS_IsSame< SYS_FixedArrayElement_t< T >, E >
381 {
382  static_assert( SYS_IsFixedArray_v< T > );
383 };
384 
385 template< typename T, typename E >
387 
388 /// @}
389 
390 ///
391 /// Determine whether the element type of T * with CV qualifiers removed *
392 /// is equal to E.
393 /// T must be a fixed array-like type and E must not have any CV qualifiers.
394 
395 /// {@
396 ///
397 
398 template< typename T, typename E >
400  SYS_IsSame< SYS_RemoveCV_t< SYS_FixedArrayElement_t< T > >, E >
401 {
402  static_assert( SYS_IsFixedArray_v< T > );
403  static_assert( SYS_HasNoCVRef_v< E > );
404 };
405 
406 template< typename T, typename E >
408 
409 /// @}
410 
411 // Specialize FixedArray traits for built-in bounded arrays
412 
413 template< typename T, std::size_t N >
414 struct SYS_IsFixedArrayNoCVRef< T[N] > : SYS_BoolConstant< true > {};
415 
416 template< typename T, std::size_t N >
418 
419 template< typename T, std::size_t N >
420 struct SYS_FixedArraySizeNoCVRef< T[N] > : std::integral_constant< std::size_t, N > {};
421 
422 template <typename T>
424 SYSaddressof(T &val) noexcept
425 {
426  return __builtin_addressof(val);
427 }
428 
429 #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
constexpr bool SYS_IsFixedArray_v
typename SYS_EnableIf< B, T >::type SYS_EnableIf_t
SYS_NO_DISCARD_RESULT SYS_FORCE_INLINE constexpr T * SYSaddressof(T &val) noexcept
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
std::conditional_t< add_const, std::add_const_t< T >, T > type
typename SYS_SelectType< T0, T1, IST1 >::type SYS_SelectType_t
#define SYS_NO_DISCARD_RESULT
Definition: SYS_Compiler.h:86
constexpr bool SYS_FixedArrayHasElement_v
constexpr bool SYS_IsFixedArrayOfNoCV_v
constexpr auto SYS_IsPointer_v
std::integral_constant< bool, std::numeric_limits< T >::is_signed||std::is_same< T, int128_opt >::value > is_signed
Definition: format.h:818
constexpr bool SYS_IsFixedArrayOf_v
constexpr auto SYS_IsTriviallyRelocatable_v
constexpr auto SYS_IsArithmetic_v
GLuint GLfloat * val
Definition: glcorearb.h:1608
GA_API const UT_StringHolder N
typename SYS_ConditionalAddConst< add_const, T >::type SYS_ConditionalAddConst_t
constexpr std::size_t SYS_FixedArraySize_v
typename SYS_TransferConst< U, T >::type SYS_TransferConst_t
constexpr auto SYS_IsSigned_v
typename SYS_ReverseConst< T >::type SYS_ReverseConst_t