1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     util_TypeTraits.h
4 
5   Copyright (C)2009 Nintendo Co., Ltd.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Rev: 12449 $
14  *---------------------------------------------------------------------------*/
15 
16 
17 #ifndef NN_UTIL_UTIL_TYPETRAITS_H_
18 #define NN_UTIL_UTIL_TYPETRAITS_H_
19 
20 #ifdef __cplusplus
21 
22 #include <nn/types.h>
23 #include <nn/util/util_StaticAssert.h>
24 
25 // C++0x の <type_traits> の先取り。
26 // 必要になったものから順次実装してゆく。
27 // コメントアウトされているものは未実装。
28 // 一部はコンパイラサポートが必要だったり、C++0xでないと定義されないものもある。
29 
30 // なお、このヘッダには、互換性を保つため、
31 // <type_traits> 以外の要素を加えないこと。
32 
33 namespace nn { namespace util {
34 
35 // 宣言部
36 // 実装したものは、ここでコメントからはずすこと。
37 // ここに実装は書かないこと。
38 
39 // [20.4.3] helper class:
40 template <class T, T v> struct integral_constant;
41 typedef integral_constant<bool, true> true_type;
42 typedef integral_constant<bool, false> false_type;
43 
44 // [20.4.4.1] primary type categories:
45 template <class T> struct is_void;
46 //template <class T> struct is_integral;
47 //template <class T> struct is_floating_point;
48 //template <class T> struct is_array;
49 //template <class T> struct is_pointer;
50 //template <class T> struct is_lvalue_reference;
51 //template <class T> struct is_rvalue_reference;
52 //template <class T> struct is_member_object_pointer;
53 //template <class T> struct is_member_function_pointer;
54 //template <class T> struct is_enum;
55 //template <class T> struct is_union;
56 //template <class T> struct is_class;
57 //template <class T> struct is_function;
58 
59 // [20.4.4.2] composite type categories:
60 //template <class T> struct is_reference;
61 //template <class T> struct is_arithmetic;
62 //template <class T> struct is_fundamental;
63 //template <class T> struct is_object;
64 //template <class T> struct is_scalar;
65 //template <class T> struct is_compound;
66 //template <class T> struct is_member_pointer;
67 
68 // [20.4.4.3] type properties:
69 //template <class T> struct is_const;
70 //template <class T> struct is_volatile;
71 //template <class T> struct is_trivial;
72 //template <class T> struct is_standard_layout;
73 //template <class T> struct is_pod;
74 //template <class T> struct is_empty;
75 //template <class T> struct is_polymorphic;
76 //template <class T> struct is_abstract;
77 //template <class T> struct has_trivial_default_constructor;
78 //template <class T> struct has_trivial_copy_constructor;
79 //template <class T> struct has_trivial_assign;
80 //template <class T> struct has_trivial_destructor;
81 //template <class T> struct has_nothrow_default_constructor;
82 //template <class T> struct has_nothrow_copy_constructor;
83 //template <class T> struct has_nothrow_assign;
84 //template <class T> struct has_virtual_destructor;
85 //template <class T> struct is_signed;
86 //template <class T> struct is_unsigned;
87 template <class T> struct alignment_of;
88 //template <class T> struct rank;
89 //template <class T, unsigned I = 0> struct extent;
90 
91 // [20.4.5] type relations:
92 template <class T, class U> struct is_same;
93 template <class Base, class Derived> struct is_base_of;
94 template <class From, class To> struct is_convertible;
95 
96 // [20.4.6.1] const-volatile modifications:
97 //template <class T> struct remove_const;
98 //template <class T> struct remove_volatile;
99 //template <class T> struct remove_cv;
100 //template <class T> struct add_const;
101 //template <class T> struct add_volatile;
102 //template <class T> struct add_cv;
103 
104 // [20.4.6.2] reference modifications:
105 //template <class T> struct remove_reference;
106 //template <class T> struct add_lvalue_reference;
107 //template <class T> struct add_rvalue_reference;
108 
109 // [20.4.6.3] sign modifications:
110 //template <class T> struct make_signed;
111 //template <class T> struct make_unsigned;
112 
113 // [20.4.6.4] array modifications:
114 //template <class T> struct remove_extent;
115 //template <class T> struct remove_all_extents;
116 
117 // [20.4.6.5] pointer modifications:
118 //template <class T> struct remove_pointer;
119 //template <class T> struct add_pointer;
120 
121 // [20.4.7] other transformations:
122 template <size_t Len, size_t Align> struct aligned_storage;
123 //template <size_t Len, class... Types> struct aligned_union;
124 //template <class T> struct decay;
125 template <bool, class T = void> struct enable_if;
126 //template <bool, class T, class F> struct conditional;
127 
128 
129 // ここから実装部
130 
131 
132 // template <class T, T v> struct integral_constant; 実装
133 
134 template <class T, T v>
135 struct integral_constant
136 {
137     typedef integral_constant<T, v> type;
138     typedef T value_type;
139     static const value_type value = v;
140 };
141 
142 
143 // template <class T> struct is_void; 実装
144 
145 template <class>
146 struct is_void : public false_type {};
147 
148 template <>
149 struct is_void<void> : public true_type {};
150 
151 
152 //template <class T> struct alignment_of; 実装
153 
154 #ifdef NN_COMPILER_RVCT
155 
156 template <class T>
157 struct alignment_of : public integral_constant<size_t, __alignof__(T)> {};
158 
159 #else
160 
161 namespace detail
162 {
163     template <class T>
164     class AlignmentHack
165     {
166         char c;
167         T x;
168     };
169 }
170 
171 template <class T>
172 struct alignment_of : public integral_constant<size_t, sizeof(detail::AlignmentHack<T>) - sizeof(T)> {};
173 
174 #endif
175 
176 //template <size_t Len, size_t Align> struct aligned_storage; 実装
177 
178 namespace detail
179 {
180     template <size_t>
181     struct AlignmentType {};
182 
183     template <>
184     struct AlignmentType<1>
185     {
186         typedef u8 type;
187     };
188 
189     template <>
190     struct AlignmentType<2>
191     {
192         typedef u16 type;
193     };
194 
195     template <>
196     struct AlignmentType<4>
197     {
198         typedef u32 type;
199     };
200 
201     template <>
202     struct AlignmentType<8>
203     {
204         typedef u64 type;
205     };
206 }
207 
208 template <size_t Size, size_t Align>
209 struct aligned_storage
210 {
211 private:
212 
213     union UnionType
214     {
215         char c[Size];
216         typename detail::AlignmentType<Align>::type a;
217     };
218 
219 public:
220     typedef UnionType type;
221 };
222 
223 
224 // template <bool b, class T = void> struct enable_if; 実装
225 
226 template <bool, class>
227 struct enable_if {};
228 
229 template <class T>
230 struct enable_if<true, T> { typedef T type; };
231 
232 
233 // template <class From, class To> struct is_convertible; 実装
234 
235 template <class From, class To>
236 struct is_convertible
237 {
238 private:
239 
240     typedef char T1;
241     struct T2 { char dummy[2]; };
242     static T1 IsConvertibleTest(To);
243     static T2 IsConvertibleTest(...);
244     static From MakeFrom();
245 
246 public:
247 
248    typedef bool value_type;
249     static const bool value = sizeof(IsConvertibleTest(MakeFrom())) == sizeof(T1);
250 
251 };
252 
253 
254 // template <class T, class U> struct is_same; 実装
255 
256 template <class T, class U>
257 struct is_same : public false_type {};
258 
259 template <class T>
260 struct is_same<T, T> : public true_type {};
261 
262 
263 // template <class Base, class Derived> struct is_base_of; 実装
264 
265 template <class Base, class Derived>
266 struct is_base_of : public integral_constant<bool, is_convertible<const Derived*, const Base*>::value && !is_same<const Base*, const void*>::value> {};
267 
268 }}
269 
270 #endif // __cplusplus
271 
272 #endif /* NN_UTIL_STATICASSERT_H_ */
273