16 #ifndef INCLUDE_NN_NLIB_TYPETRAITS_H_ 17 #define INCLUDE_NN_NLIB_TYPETRAITS_H_ 21 #ifndef NLIB_HAS_NATIVE_TYPETRAITS 24 #include <type_traits> 30 template<
class T, T v>
31 struct IntegralConstant {
32 static const T value = v;
34 typedef IntegralConstant type;
35 operator value_type()
const {
return value; }
39 typedef IntegralConstant<bool, true> TrueType;
40 typedef IntegralConstant<bool, false> FalseType;
42 #define NLIB_DEF_TRUETYPE(name, tp) \ 43 template<> struct name<tp> { typedef TrueType type; } 47 template<
class T>
struct my_or1 {
typedef T type; };
49 template<
class T1,
class T2>
struct my_or2;
50 template<
class T>
struct my_or2<TrueType, T> {
typedef TrueType type; };
51 template<
class T>
struct my_or2<FalseType, T> {
52 typedef typename my_or1<T>::type type;
55 template<
class T1,
class T2,
class T3>
struct my_or3;
56 template<
class T1,
class T2>
struct my_or3<TrueType, T1, T2> {
57 typedef TrueType type;
59 template<
class T1,
class T2>
struct my_or3<FalseType, T1, T2> {
60 typedef typename my_or2<T1, T2>::type type;
63 template<
class T1,
class T2,
class T3,
class T4>
struct my_or4;
64 template<
class T1,
class T2,
class T3>
struct my_or4<TrueType, T1, T2, T3> {
65 typedef TrueType type;
67 template<
class T1,
class T2,
class T3>
struct my_or4<FalseType, T1, T2, T3> {
68 typedef typename my_or3<T1, T2, T3>::type type;
71 template<
class T1 = FalseType,
class T2 = FalseType,
72 class T3 = FalseType,
class T4 = FalseType>
74 typedef typename my_or4<T1, T2, T3, T4>::type type;
80 template<
bool B,
class T =
void>
struct EnableIf {};
81 template<
class T>
struct EnableIf<true, T> {
typedef T type; };
84 template<
bool B,
class T,
class F>
struct Conditional {
typedef T type; };
85 template<
class T,
class F>
struct Conditional<false, T, F> {
typedef F type; };
88 template<
class T>
struct RemoveConst {
typedef T type; };
89 template<
class T>
struct RemoveConst<const T> {
typedef T type; };
91 template<
class T>
struct RemoveVolatile {
typedef T type; };
92 template<
class T>
struct RemoveVolatile<volatile T> {
typedef T type; };
96 typedef typename RemoveVolatile<typename RemoveConst<T>::type>::type type;
100 template<
class T>
struct RemovePtr {
typedef T type; };
101 template<
class T>
struct RemovePtr<T*> {
typedef T type; };
102 template<
class T>
struct RemovePtr<const T*> {
typedef T type; };
103 template<
class T>
struct RemovePtr<volatile T*> {
typedef T type; };
104 template<
class T>
struct RemovePtr<const volatile T*> {
typedef T type; };
107 template<
class T>
struct RemoveRef {
typedef T type; };
108 template<
class T>
struct RemoveRef<T&> {
typedef T type; };
110 template<
class T>
struct AddRef {
typedef T& type; };
111 template<>
struct AddRef<void> {
typedef void type; };
114 template<
class T1,
class T2>
struct IsSame :
public FalseType {};
115 template<
class T>
struct IsSame<T, T> :
public TrueType {};
118 namespace tt_detail {
119 template<
class T>
struct is_void {
typedef FalseType type; };
120 NLIB_DEF_TRUETYPE(is_void,
void);
125 public tt_detail::is_void<typename RemoveCv<T>::type>::type {
129 namespace tt_detail {
131 template<
class T>
struct is_integral_ex {
typedef FalseType type; };
132 NLIB_DEF_TRUETYPE(is_integral_ex,
signed char);
133 NLIB_DEF_TRUETYPE(is_integral_ex,
signed short);
134 NLIB_DEF_TRUETYPE(is_integral_ex,
signed int);
135 NLIB_DEF_TRUETYPE(is_integral_ex,
signed long);
136 NLIB_DEF_TRUETYPE(is_integral_ex,
signed long long);
138 template<
class T>
struct is_integral {
typedef FalseType type; };
139 NLIB_DEF_TRUETYPE(is_integral,
bool);
140 NLIB_DEF_TRUETYPE(is_integral,
char);
141 NLIB_DEF_TRUETYPE(is_integral,
wchar_t);
142 NLIB_DEF_TRUETYPE(is_integral,
short);
143 NLIB_DEF_TRUETYPE(is_integral,
int);
144 NLIB_DEF_TRUETYPE(is_integral,
long);
145 NLIB_DEF_TRUETYPE(is_integral,
long long);
147 NLIB_DEF_TRUETYPE(is_integral,
unsigned char);
148 NLIB_DEF_TRUETYPE(is_integral,
unsigned short);
149 NLIB_DEF_TRUETYPE(is_integral,
unsigned int);
150 NLIB_DEF_TRUETYPE(is_integral,
unsigned long long);
151 NLIB_DEF_TRUETYPE(is_integral,
unsigned long);
156 :
public tt_detail::my_or<
157 typename tt_detail::is_integral<typename RemoveCv<T>::type>::type,
158 typename tt_detail::is_integral_ex<typename RemoveCv<T>::type>::type
163 namespace tt_detail {
165 template<
class T>
struct is_signed_ex {
typedef FalseType type; };
166 NLIB_DEF_TRUETYPE(is_signed_ex,
signed char);
167 NLIB_DEF_TRUETYPE(is_signed_ex,
signed short);
168 NLIB_DEF_TRUETYPE(is_signed_ex,
signed int);
169 NLIB_DEF_TRUETYPE(is_signed_ex,
signed long);
170 NLIB_DEF_TRUETYPE(is_signed_ex,
signed long long);
172 template<
class T>
struct is_signed {
typedef FalseType type; };
173 NLIB_DEF_TRUETYPE(is_signed,
char);
174 NLIB_DEF_TRUETYPE(is_signed,
short);
175 NLIB_DEF_TRUETYPE(is_signed,
int);
176 NLIB_DEF_TRUETYPE(is_signed,
long);
177 NLIB_DEF_TRUETYPE(is_signed,
long long);
178 NLIB_DEF_TRUETYPE(is_signed,
float);
179 NLIB_DEF_TRUETYPE(is_signed,
double);
184 :
public tt_detail::my_or<
185 typename tt_detail::is_signed<typename RemoveCv<T>::type>::type,
186 typename tt_detail::is_signed_ex<typename RemoveCv<T>::type>::type
190 template<
class T>
struct IsUnsigned {
typedef FalseType type; };
191 template<>
struct IsUnsigned<bool> {
typedef TrueType type; };
192 template<>
struct IsUnsigned<wchar_t> {
typedef TrueType type; };
193 template<>
struct IsUnsigned<unsigned char> {
typedef TrueType type; };
194 template<>
struct IsUnsigned<unsigned short> {
typedef TrueType type; };
195 template<>
struct IsUnsigned<unsigned int> {
typedef TrueType type; };
196 template<>
struct IsUnsigned<unsigned long> {
typedef TrueType type; };
197 template<>
struct IsUnsigned<unsigned long long> {
typedef TrueType type; };
200 namespace tt_detail {
202 template<
class T>
struct is_floating_point {
typedef FalseType type; };
203 NLIB_DEF_TRUETYPE(is_floating_point,
float);
204 NLIB_DEF_TRUETYPE(is_floating_point,
double);
205 NLIB_DEF_TRUETYPE(is_floating_point,
long double);
210 struct IsFloatingPoint
211 :
public tt_detail::is_floating_point<typename RemoveCv<T>::type>::type {
217 :
public IntegralConstant<
219 IsIntegral<T>::value || IsFloatingPoint<T>::value> {
223 namespace tt_detail {
224 template<
class T>
struct IsPointerHelper :
public FalseType {};
225 template<
class T>
struct IsPointerHelper<T*> :
public TrueType {};
229 public tt_detail::IsPointerHelper<typename RemoveCv<T>::type> {
233 template<
class T>
struct IsReference :
public FalseType {};
234 template<
class T>
struct IsReference<T&> :
public TrueType {};
237 template<
class T>
struct IsArray :
public FalseType {};
238 template<
class T>
struct IsArray<T[]> :
public TrueType {};
239 template<
class T,
size_t N>
240 struct IsArray<T[N]> :
public TrueType {};
243 namespace tt_detail {
244 template<
class T>
struct is_member_pointer :
public FalseType {};
245 template<
class T,
class U>
246 struct is_member_pointer<T U::*> :
public TrueType {};
249 struct IsMemberPointer :
250 public tt_detail::is_member_pointer<typename RemoveCv<T>::type> {
254 namespace tt_detail {
255 template<
bool B>
struct is_function_ {
typedef FalseType type; };
256 template<>
struct is_function_<true> {
typedef TrueType type; };
261 typedef struct {
char a[2]; } Two;
262 template<
class U>
static One test(...);
263 template<
class U>
static Two test(U (*)[1]);
267 # pragma diag_suppress 1931 269 typedef typename is_function_<sizeof(test<T>(0)) == 1>::type type;
271 # pragma diag_warning 1931 274 template<
class T>
struct is_function<T&> {
typedef FalseType type; };
275 template<>
struct is_function<void> {
typedef FalseType type; };
276 template<>
struct is_function<const void> {
typedef FalseType type; };
277 template<>
struct is_function<volatile void> {
typedef FalseType type; };
278 template<>
struct is_function<const volatile void> {
typedef FalseType type; };
281 struct IsFunction :
public tt_detail::is_function<T>::type {};
283 #if !defined(NN_PLATFORM_CTR) 286 public IntegralConstant<bool, __is_enum(T)> {
290 namespace tt_detail {
291 typedef char (&SizeOverOne)[2];
293 bool convert_possible =
294 !IsArithmetic<T>::value &&
295 !IsReference<T>::value &&
296 !IsPointer<T>::value &&
297 !IsMemberPointer<T>::value &&
298 !IsFunction<T>::value &&
305 struct ConsumeUDC<T, false> {
306 operator SizeOverOne()
const;
308 char enum_check(
bool x);
309 char enum_check(
char x);
310 char enum_check(
signed char x);
311 char enum_check(
unsigned char x);
312 char enum_check(
wchar_t x);
313 char enum_check(
signed short x);
314 char enum_check(
unsigned short x);
315 char enum_check(
signed int x);
316 char enum_check(
unsigned int x);
317 char enum_check(
signed long x);
318 char enum_check(
unsigned long x);
319 char enum_check(
signed long long x);
320 char enum_check(
unsigned long long x);
321 char enum_check(
float x);
322 char enum_check(
double x);
323 char enum_check(
long double x);
324 SizeOverOne enum_check(SizeOverOne x);
325 SizeOverOne enum_check(...);
329 value =
sizeof(enum_check(ConsumeUDC<T>())) == 1
336 :
public IntegralConstant<bool, tt_detail::is_enum<T>::value> {};
339 #if !defined(NN_PLATFORM_CTR) && !defined(CAFE) 342 public IntegralConstant<bool,
343 __is_pod(T) || IsEnum<T>::value ||
344 IsArithmetic<T>::value || IsPointer<T>::value ||
345 IsMemberPointer<T>::value> {
349 struct IsTriviallyDestructible :
350 public IntegralConstant<bool, __has_trivial_destructor(T)> {
354 struct IsTriviallyDefaultConstructible :
355 public IntegralConstant<bool, __has_trivial_default_constructor(T)> {
359 struct IsTriviallyDefaultConstructible :
360 public IntegralConstant<bool, std::has_trivial_default_constructor<T>::value > {
366 namespace tt_detail {
368 template<
class T>
struct is_pod
369 :
public tt_detail::my_or<typename IsArithmetic<T>::type,
370 typename IsPointer<T>::type,
371 typename IsMemberPointer<T>::type,
372 typename IsEnum<T>::type>::type {
377 typedef typename is_pod<typename RemoveCv<T>::type>::type type;
379 template<
class T,
size_t N>
380 struct is_pod<T[N]> {
381 typedef typename is_pod<typename RemoveCv<T>::type>::type type;
394 struct IsTriviallyDestructible
395 :
public tt_detail::is_pod<typename RemoveCv<T>::type>::type {
398 struct IsTriviallyDefaultConstructible
399 :
public tt_detail::is_pod<typename RemoveCv<T>::type>::type {
403 struct IsPod :
public IsTriviallyDestructible<T> {};
406 #if !defined(NN_PLATFORM_CTR) 409 public IntegralConstant<bool, __is_empty(T)> {
413 namespace tt_detail {
415 struct IsEmptyHelper1 :
public T {
422 struct IsEmptyHelper2 {
int data[256]; };
427 public IntegralConstant<bool, sizeof(tt_detail::IsEmptyHelper1<T>) ==
428 sizeof(tt_detail::IsEmptyHelper2)> {
432 #if !defined(NN_PLATFORM_CTR) 435 public IntegralConstant<bool, __is_class(T)> {
442 typedef struct {
char a[2]; } no;
445 static yes check(
void (X::*)(
void));
447 static no check(...);
450 static const bool value =
sizeof(check<T>(NULL)) ==
sizeof(yes);
459 #undef NLIB_DEF_TRUETYPE 462 #include <type_traits> 465 #ifdef NLIB_HAS_TR1_TYPETRAITS 466 #define NLIB_DEF_REDIRECT(nlibstruct, stdstruct) \ 467 template<class T> struct nlibstruct \ 468 : public std::tr1::stdstruct<T> {} 469 template<
class T, T v>
470 struct IntegralConstant
471 :
public std::tr1::integral_constant<T, v> {
473 typedef std::tr1::true_type TrueType;
474 typedef std::tr1::false_type FalseType;
475 template<
bool B,
class T =
void>
struct EnableIf {};
476 template<
class T>
struct EnableIf<true, T> {
typedef T type; };
477 template<
bool B,
class T,
class F>
struct Conditional {
typedef T type; };
478 template<
class T,
class F>
struct Conditional<false, T, F> {
typedef F type; };
479 template<
class T1,
class T2>
480 struct IsSame :
public std::tr1::is_same<T1, T2> {};
482 NLIB_DEF_REDIRECT(IsTriviallyDestructible, has_trivial_destructor);
483 NLIB_DEF_REDIRECT(IsTriviallyDefaultConstructible, has_trivial_default_constructor);
486 #define NLIB_DEF_REDIRECT(nlibstruct, stdstruct) \ 487 template<class T> struct nlibstruct \ 488 : public std::stdstruct<T> {} 489 template<
class T, T v>
490 struct IntegralConstant
491 :
public std::integral_constant<T, v> {
493 typedef std::true_type TrueType;
494 typedef std::false_type FalseType;
495 template<
bool B,
class T>
struct EnableIf :
public std::enable_if<B, T> {};
496 template<
bool B,
class T,
class F>
498 :
public std::conditional<B, T, F> {
500 template<
class T1,
class T2>
501 struct IsSame :
public std::is_same<T1, T2> {};
505 #if defined(__GLIBCXX__) 507 struct IsTriviallyDestructible :
508 public IntegralConstant<bool, __has_trivial_destructor(T)> {
511 struct IsTriviallyDefaultConstructible :
512 public IntegralConstant<bool, __has_trivial_constructor(T)> {
515 NLIB_DEF_REDIRECT(IsTriviallyDestructible, is_trivially_destructible);
516 NLIB_DEF_REDIRECT(IsTriviallyDefaultConstructible, is_trivially_constructible);
521 NLIB_DEF_REDIRECT(RemoveConst, remove_const);
522 NLIB_DEF_REDIRECT(RemoveVolatile, remove_volatile);
523 NLIB_DEF_REDIRECT(RemovePtr, remove_pointer);
524 NLIB_DEF_REDIRECT(RemoveRef, remove_reference);
525 NLIB_DEF_REDIRECT(AddRef, add_lvalue_reference);
526 NLIB_DEF_REDIRECT(RemoveCv, remove_cv);
527 NLIB_DEF_REDIRECT(IsVoid, is_void);
528 NLIB_DEF_REDIRECT(IsIntegral, is_integral);
529 NLIB_DEF_REDIRECT(IsSigned, is_signed);
530 NLIB_DEF_REDIRECT(IsUnsigned, is_unsigned);
531 NLIB_DEF_REDIRECT(IsFloatingPoint, is_floating_point);
532 NLIB_DEF_REDIRECT(IsArithmetic, is_arithmetic);
533 NLIB_DEF_REDIRECT(IsPointer, is_pointer);
534 NLIB_DEF_REDIRECT(IsReference, is_reference);
535 NLIB_DEF_REDIRECT(IsArray, is_array);
536 NLIB_DEF_REDIRECT(IsMemberPointer, is_member_pointer);
537 NLIB_DEF_REDIRECT(IsFunction, is_function);
538 NLIB_DEF_REDIRECT(IsEnum, is_enum);
540 NLIB_DEF_REDIRECT(IsPod, is_pod);
541 NLIB_DEF_REDIRECT(IsEmpty, is_empty);
542 NLIB_DEF_REDIRECT(IsClass, is_class);
546 #undef NLIB_DEF_REDIRECT 556 typedef struct {
char a[2]; } no;
558 template<
class X,
void (X::*Func)(T&)>
561 static yes check(helper<X, &X::swap>* p);
563 static no check(...);
567 # pragma diag_suppress 1931 569 static const bool value =
sizeof(check<T>(NULL)) ==
sizeof(yes);
571 # pragma diag_warning 1931 575 template<
class T,
bool isClass>
576 struct IsSwappable_ :
577 public IntegralConstant<bool, detail::HasSwapMemFn<T>::value> {
581 struct IsSwappable_<T, false> :
582 public IntegralConstant<
584 IsArithmetic<T>::value ||
585 IsPointer<T>::value ||
586 IsFunction<T>::value ||
587 IsMemberPointer<T>::value ||
598 struct IsSwappable :
public detail::IsSwappable_<T, IsClass<T>::value> {};
602 #endif // INCLUDE_NN_NLIB_TYPETRAITS_H_ #define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。