3 #ifndef INCLUDE_NN_NLIB_TYPETRAITS_H_ 4 #define INCLUDE_NN_NLIB_TYPETRAITS_H_ 8 #ifndef NLIB_HAS_NATIVE_TYPETRAITS 11 #include <type_traits> 17 template<
class T, T v>
18 struct IntegralConstant {
19 static const T value = v;
21 typedef IntegralConstant type;
22 operator value_type()
const {
return value; }
26 typedef IntegralConstant<bool, true> TrueType;
27 typedef IntegralConstant<bool, false> FalseType;
29 #define NLIB_DEF_TRUETYPE(name, tp) \ 30 template<> struct name<tp> { typedef TrueType type; } 34 template<
class T>
struct my_or1 {
typedef T type; };
36 template<
class T1,
class T2>
struct my_or2;
37 template<
class T>
struct my_or2<TrueType, T> {
typedef TrueType type; };
38 template<
class T>
struct my_or2<FalseType, T> {
39 typedef typename my_or1<T>::type type;
42 template<
class T1,
class T2,
class T3>
struct my_or3;
43 template<
class T1,
class T2>
struct my_or3<TrueType, T1, T2> {
44 typedef TrueType type;
46 template<
class T1,
class T2>
struct my_or3<FalseType, T1, T2> {
47 typedef typename my_or2<T1, T2>::type type;
50 template<
class T1,
class T2,
class T3,
class T4>
struct my_or4;
51 template<
class T1,
class T2,
class T3>
struct my_or4<TrueType, T1, T2, T3> {
52 typedef TrueType type;
54 template<
class T1,
class T2,
class T3>
struct my_or4<FalseType, T1, T2, T3> {
55 typedef typename my_or3<T1, T2, T3>::type type;
58 template<
class T1 = FalseType,
class T2 = FalseType,
59 class T3 = FalseType,
class T4 = FalseType>
61 typedef typename my_or4<T1, T2, T3, T4>::type type;
67 template<
bool B,
class T =
void>
struct EnableIf {};
68 template<
class T>
struct EnableIf<true, T> {
typedef T type; };
71 template<
bool B,
class T,
class F>
struct Conditional {
typedef T type; };
72 template<
class T,
class F>
struct Conditional<false, T, F> {
typedef F type; };
75 template<
class T>
struct RemoveConst {
typedef T type; };
76 template<
class T>
struct RemoveConst<const T> {
typedef T type; };
78 template<
class T>
struct RemoveVolatile {
typedef T type; };
79 template<
class T>
struct RemoveVolatile<volatile T> {
typedef T type; };
83 typedef typename RemoveVolatile<typename RemoveConst<T>::type>::type type;
87 template<
class T>
struct RemovePtr {
typedef T type; };
88 template<
class T>
struct RemovePtr<T*> {
typedef T type; };
89 template<
class T>
struct RemovePtr<const T*> {
typedef T type; };
90 template<
class T>
struct RemovePtr<volatile T*> {
typedef T type; };
91 template<
class T>
struct RemovePtr<const volatile T*> {
typedef T type; };
94 template<
class T>
struct RemoveRef {
typedef T type; };
95 template<
class T>
struct RemoveRef<T&> {
typedef T type; };
97 template<
class T>
struct AddRef {
typedef T& type; };
98 template<>
struct AddRef<void> {
typedef void type; };
101 template<
class T1,
class T2>
struct IsSame :
public FalseType {};
102 template<
class T>
struct IsSame<T, T> :
public TrueType {};
105 namespace tt_detail {
106 template<
class T>
struct is_void {
typedef FalseType type; };
107 NLIB_DEF_TRUETYPE(is_void,
void);
112 public tt_detail::is_void<typename RemoveCv<T>::type>::type {
116 namespace tt_detail {
118 template<
class T>
struct is_integral_ex {
typedef FalseType type; };
119 NLIB_DEF_TRUETYPE(is_integral_ex,
signed char);
120 NLIB_DEF_TRUETYPE(is_integral_ex,
signed short);
121 NLIB_DEF_TRUETYPE(is_integral_ex,
signed int);
122 NLIB_DEF_TRUETYPE(is_integral_ex,
signed long);
123 NLIB_DEF_TRUETYPE(is_integral_ex,
signed long long);
125 template<
class T>
struct is_integral {
typedef FalseType type; };
126 NLIB_DEF_TRUETYPE(is_integral,
bool);
127 NLIB_DEF_TRUETYPE(is_integral,
char);
128 NLIB_DEF_TRUETYPE(is_integral,
wchar_t);
129 NLIB_DEF_TRUETYPE(is_integral,
short);
130 NLIB_DEF_TRUETYPE(is_integral,
int);
131 NLIB_DEF_TRUETYPE(is_integral,
long);
132 NLIB_DEF_TRUETYPE(is_integral,
long long);
134 NLIB_DEF_TRUETYPE(is_integral,
unsigned char);
135 NLIB_DEF_TRUETYPE(is_integral,
unsigned short);
136 NLIB_DEF_TRUETYPE(is_integral,
unsigned int);
137 NLIB_DEF_TRUETYPE(is_integral,
unsigned long long);
138 NLIB_DEF_TRUETYPE(is_integral,
unsigned long);
143 :
public tt_detail::my_or<
144 typename tt_detail::is_integral<typename RemoveCv<T>::type>::type,
145 typename tt_detail::is_integral_ex<typename RemoveCv<T>::type>::type
150 namespace tt_detail {
152 template<
class T>
struct is_signed_ex {
typedef FalseType type; };
153 NLIB_DEF_TRUETYPE(is_signed_ex,
signed char);
154 NLIB_DEF_TRUETYPE(is_signed_ex,
signed short);
155 NLIB_DEF_TRUETYPE(is_signed_ex,
signed int);
156 NLIB_DEF_TRUETYPE(is_signed_ex,
signed long);
157 NLIB_DEF_TRUETYPE(is_signed_ex,
signed long long);
159 template<
class T>
struct is_signed {
typedef FalseType type; };
160 NLIB_DEF_TRUETYPE(is_signed,
char);
161 NLIB_DEF_TRUETYPE(is_signed,
short);
162 NLIB_DEF_TRUETYPE(is_signed,
int);
163 NLIB_DEF_TRUETYPE(is_signed,
long);
164 NLIB_DEF_TRUETYPE(is_signed,
long long);
165 NLIB_DEF_TRUETYPE(is_signed,
float);
166 NLIB_DEF_TRUETYPE(is_signed,
double);
171 :
public tt_detail::my_or<
172 typename tt_detail::is_signed<typename RemoveCv<T>::type>::type,
173 typename tt_detail::is_signed_ex<typename RemoveCv<T>::type>::type
177 template<
class T>
struct IsUnsigned {
typedef FalseType type; };
178 template<>
struct IsUnsigned<bool> {
typedef TrueType type; };
179 template<>
struct IsUnsigned<wchar_t> {
typedef TrueType type; };
180 template<>
struct IsUnsigned<unsigned char> {
typedef TrueType type; };
181 template<>
struct IsUnsigned<unsigned short> {
typedef TrueType type; };
182 template<>
struct IsUnsigned<unsigned int> {
typedef TrueType type; };
183 template<>
struct IsUnsigned<unsigned long> {
typedef TrueType type; };
184 template<>
struct IsUnsigned<unsigned long long> {
typedef TrueType type; };
187 namespace tt_detail {
189 template<
class T>
struct is_floating_point {
typedef FalseType type; };
190 NLIB_DEF_TRUETYPE(is_floating_point,
float);
191 NLIB_DEF_TRUETYPE(is_floating_point,
double);
192 NLIB_DEF_TRUETYPE(is_floating_point,
long double);
197 struct IsFloatingPoint
198 :
public tt_detail::is_floating_point<typename RemoveCv<T>::type>::type {
204 :
public IntegralConstant<
206 IsIntegral<T>::value || IsFloatingPoint<T>::value> {
210 namespace tt_detail {
211 template<
class T>
struct IsPointerHelper :
public FalseType {};
212 template<
class T>
struct IsPointerHelper<T*> :
public TrueType {};
216 public tt_detail::IsPointerHelper<typename RemoveCv<T>::type> {
220 template<
class T>
struct IsReference :
public FalseType {};
221 template<
class T>
struct IsReference<T&> :
public TrueType {};
224 template<
class T>
struct IsArray :
public FalseType {};
225 template<
class T>
struct IsArray<T[]> :
public TrueType {};
226 template<
class T,
size_t N>
227 struct IsArray<T[N]> :
public TrueType {};
230 namespace tt_detail {
231 template<
class T>
struct is_member_pointer :
public FalseType {};
232 template<
class T,
class U>
233 struct is_member_pointer<T U::*> :
public TrueType {};
236 struct IsMemberPointer :
237 public tt_detail::is_member_pointer<typename RemoveCv<T>::type> {
241 namespace tt_detail {
242 template<
bool B>
struct is_function_ {
typedef FalseType type; };
243 template<>
struct is_function_<true> {
typedef TrueType type; };
248 typedef struct {
char a[2]; } Two;
249 template<
class U>
static One test(...);
250 template<
class U>
static Two test(U (*)[1]);
254 # pragma diag_suppress 1931 256 typedef typename is_function_<sizeof(test<T>(0)) == 1>::type type;
258 # pragma diag_warning 1931 261 template<
class T>
struct is_function<T&> {
typedef FalseType type; };
262 template<>
struct is_function<void> {
typedef FalseType type; };
263 template<>
struct is_function<const void> {
typedef FalseType type; };
264 template<>
struct is_function<volatile void> {
typedef FalseType type; };
265 template<>
struct is_function<const volatile void> {
typedef FalseType type; };
268 struct IsFunction :
public tt_detail::is_function<T>::type {};
270 #if !defined(NN_PLATFORM_CTR) 273 public IntegralConstant<bool, __is_enum(T)> {
277 namespace tt_detail {
278 typedef char (&SizeOverOne)[2];
280 bool convert_possible =
281 !IsArithmetic<T>::value &&
282 !IsReference<T>::value &&
283 !IsPointer<T>::value &&
284 !IsMemberPointer<T>::value &&
285 !IsFunction<T>::value &&
292 struct ConsumeUDC<T, false> {
293 operator SizeOverOne()
const;
295 char enum_check(
bool x);
296 char enum_check(
char x);
297 char enum_check(
signed char x);
298 char enum_check(
unsigned char x);
299 char enum_check(
wchar_t x);
300 char enum_check(
signed short x);
301 char enum_check(
unsigned short x);
302 char enum_check(
signed int x);
303 char enum_check(
unsigned int x);
304 char enum_check(
signed long x);
305 char enum_check(
unsigned long x);
306 char enum_check(
signed long long x);
307 char enum_check(
unsigned long long x);
308 char enum_check(
float x);
309 char enum_check(
double x);
310 char enum_check(
long double x);
311 SizeOverOne enum_check(SizeOverOne x);
312 SizeOverOne enum_check(...);
316 value =
sizeof(enum_check(ConsumeUDC<T>())) == 1
323 :
public IntegralConstant<bool, tt_detail::is_enum<T>::value> {};
326 #if !defined(NN_PLATFORM_CTR) && !defined(CAFE) 329 public IntegralConstant<bool,
330 __is_pod(T) || IsEnum<T>::value ||
331 IsArithmetic<T>::value || IsPointer<T>::value ||
332 IsMemberPointer<T>::value> {
336 struct IsTriviallyDestructible :
337 public IntegralConstant<bool, __has_trivial_destructor(T)> {
341 struct IsTriviallyDefaultConstructible :
342 public IntegralConstant<bool, __has_trivial_default_constructor(T)> {
346 struct IsTriviallyDefaultConstructible :
347 public IntegralConstant<bool, std::has_trivial_default_constructor<T>::value > {
353 namespace tt_detail {
355 template<
class T>
struct is_pod
356 :
public tt_detail::my_or<typename IsArithmetic<T>::type,
357 typename IsPointer<T>::type,
358 typename IsMemberPointer<T>::type,
359 typename IsEnum<T>::type>::type {
364 typedef typename is_pod<typename RemoveCv<T>::type>::type type;
366 template<
class T,
size_t N>
367 struct is_pod<T[N]> {
368 typedef typename is_pod<typename RemoveCv<T>::type>::type type;
381 struct IsTriviallyDestructible
382 :
public tt_detail::is_pod<typename RemoveCv<T>::type>::type {
385 struct IsTriviallyDefaultConstructible
386 :
public tt_detail::is_pod<typename RemoveCv<T>::type>::type {
390 struct IsPod :
public IsTriviallyDestructible<T> {};
393 #if !defined(NN_PLATFORM_CTR) 396 public IntegralConstant<bool, __is_empty(T)> {
400 namespace tt_detail {
402 struct IsEmptyHelper1 :
public T {
409 struct IsEmptyHelper2 {
int data[256]; };
414 public IntegralConstant<bool, sizeof(tt_detail::IsEmptyHelper1<T>) ==
415 sizeof(tt_detail::IsEmptyHelper2)> {
419 #if !defined(NN_PLATFORM_CTR) 422 public IntegralConstant<bool, __is_class(T)> {
429 typedef struct {
char a[2]; } no;
432 static yes check(
void (X::*)(
void));
434 static no check(...);
437 static const bool value =
sizeof(check<T>(NULL)) ==
sizeof(yes);
446 #undef NLIB_DEF_TRUETYPE 449 #include <type_traits> 452 #ifdef NLIB_HAS_TR1_TYPETRAITS 453 #define NLIB_DEF_REDIRECT(nlibstruct, stdstruct) \ 454 template<class T> struct nlibstruct \ 455 : public std::tr1::stdstruct<T> {} 456 template<
class T, T v>
457 struct IntegralConstant
458 :
public std::tr1::integral_constant<T, v> {
460 typedef std::tr1::true_type TrueType;
461 typedef std::tr1::false_type FalseType;
462 template<
bool B,
class T =
void>
struct EnableIf {};
463 template<
class T>
struct EnableIf<true, T> {
typedef T type; };
464 template<
bool B,
class T,
class F>
struct Conditional {
typedef T type; };
465 template<
class T,
class F>
struct Conditional<false, T, F> {
typedef F type; };
466 template<
class T1,
class T2>
467 struct IsSame :
public std::tr1::is_same<T1, T2> {};
469 NLIB_DEF_REDIRECT(IsTriviallyDestructible, has_trivial_destructor);
470 NLIB_DEF_REDIRECT(IsTriviallyDefaultConstructible, has_trivial_default_constructor);
473 #define NLIB_DEF_REDIRECT(nlibstruct, stdstruct) \ 474 template<class T> struct nlibstruct \ 475 : public std::stdstruct<T> {} 476 template<
class T, T v>
477 struct IntegralConstant
478 :
public std::integral_constant<T, v> {
480 typedef std::true_type TrueType;
481 typedef std::false_type FalseType;
482 template<
bool B,
class T>
struct EnableIf :
public std::enable_if<B, T> {};
483 template<
bool B,
class T,
class F>
485 :
public std::conditional<B, T, F> {
487 template<
class T1,
class T2>
488 struct IsSame :
public std::is_same<T1, T2> {};
492 #if defined(__GLIBCXX__) 494 struct IsTriviallyDestructible :
495 public IntegralConstant<bool, __has_trivial_destructor(T)> {
498 struct IsTriviallyDefaultConstructible :
499 public IntegralConstant<bool, __has_trivial_constructor(T)> {
502 NLIB_DEF_REDIRECT(IsTriviallyDestructible, is_trivially_destructible);
503 NLIB_DEF_REDIRECT(IsTriviallyDefaultConstructible, is_trivially_constructible);
508 NLIB_DEF_REDIRECT(RemoveConst, remove_const);
509 NLIB_DEF_REDIRECT(RemoveVolatile, remove_volatile);
510 NLIB_DEF_REDIRECT(RemovePtr, remove_pointer);
511 NLIB_DEF_REDIRECT(RemoveRef, remove_reference);
512 NLIB_DEF_REDIRECT(AddRef, add_lvalue_reference);
513 NLIB_DEF_REDIRECT(RemoveCv, remove_cv);
514 NLIB_DEF_REDIRECT(IsVoid, is_void);
515 NLIB_DEF_REDIRECT(IsIntegral, is_integral);
516 NLIB_DEF_REDIRECT(IsSigned, is_signed);
517 NLIB_DEF_REDIRECT(IsUnsigned, is_unsigned);
518 NLIB_DEF_REDIRECT(IsFloatingPoint, is_floating_point);
519 NLIB_DEF_REDIRECT(IsArithmetic, is_arithmetic);
520 NLIB_DEF_REDIRECT(IsPointer, is_pointer);
521 NLIB_DEF_REDIRECT(IsReference, is_reference);
522 NLIB_DEF_REDIRECT(IsArray, is_array);
523 NLIB_DEF_REDIRECT(IsMemberPointer, is_member_pointer);
524 NLIB_DEF_REDIRECT(IsFunction, is_function);
525 NLIB_DEF_REDIRECT(IsEnum, is_enum);
527 NLIB_DEF_REDIRECT(IsPod, is_pod);
528 NLIB_DEF_REDIRECT(IsEmpty, is_empty);
529 NLIB_DEF_REDIRECT(IsClass, is_class);
533 #undef NLIB_DEF_REDIRECT 543 typedef struct {
char a[2]; } no;
545 template<
class X,
void (X::*Func)(T&)>
548 static yes check(helper<X, &X::swap>* p);
550 static no check(...);
554 # pragma diag_suppress 1931 556 static const bool value =
sizeof(check<T>(NULL)) ==
sizeof(yes);
558 # pragma diag_warning 1931 562 template<
class T,
bool isClass>
563 struct IsSwappable_ :
564 public IntegralConstant<bool, detail::HasSwapMemFn<T>::value> {
568 struct IsSwappable_<T, false> :
569 public IntegralConstant<
571 IsArithmetic<T>::value ||
572 IsPointer<T>::value ||
573 IsFunction<T>::value ||
574 IsMemberPointer<T>::value ||
585 struct IsSwappable :
public detail::IsSwappable_<T, IsClass<T>::value> {};
589 #endif // INCLUDE_NN_NLIB_TYPETRAITS_H_ #define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
A file that contains the configuration information for each development environment.