gwnavruntime/base/typetraits.h Source File

typetraits.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 
8 #ifndef Navigation_TypeTraits_H
9 #define Navigation_TypeTraits_H
10 
12 #include <string.h> // memcpy
13 
14 #if defined (KY_OS_WIIU)
15 #pragma ghs nowarning 236
16 #endif
17 
18 namespace Kaim
19 {
20 
21 // IsIntegral
22 
23 template <typename T>
24 struct IsIntegral
25 {
26  enum { value = false };
27 };
28 
29 template <>
30 struct IsIntegral<bool>
31 {
32  enum { value = true };
33 };
34 
35 template <>
36 struct IsIntegral<unsigned char>
37 {
38  enum { value = true };
39 };
40 
41 template <>
42 struct IsIntegral<signed char>
43 {
44  enum { value = true };
45 };
46 
47 template <>
48 struct IsIntegral<char>
49 {
50  enum { value = true };
51 };
52 
53 template <>
54 struct IsIntegral<KyUInt16>
55 {
56  enum { value = true };
57 };
58 
59 template <>
60 struct IsIntegral<KyInt16>
61 {
62  enum { value = true };
63 };
64 
65 template <>
66 struct IsIntegral<KyUInt32>
67 {
68  enum { value = true };
69 };
70 
71 template <>
72 struct IsIntegral<KyInt32>
73 {
74  enum { value = true };
75 };
76 
77 template <>
78 struct IsIntegral<KyUInt64>
79 {
80  enum { value = true };
81 };
82 
83 template <>
84 struct IsIntegral<KyInt64>
85 {
86  enum { value = true };
87 };
88 
89 // IsFloat
90 
91 template <typename T>
92 struct IsFloat
93 {
94  enum { value = false };
95 };
96 
97 template <>
98 struct IsFloat<KyFloat32>
99 {
100  enum { value = true };
101 };
102 
103 template <>
104 struct IsFloat<KyFloat64>
105 {
106  enum { value = true };
107 };
108 
109 // IsVoid
110 
111 template <typename T>
112 struct IsVoid
113 {
114  enum { value = false };
115 };
116 
117 template <>
118 struct IsVoid<void>
119 {
120  enum { value = true };
121 };
122 
123 //IsPointer
124 
125 template <typename T>
126 struct IsPointer
127 {
128  enum { value = false };
129 };
130 
131 template <typename T>
132 struct IsPointer<T*>
133 {
134  enum { value = true };
135 };
136 
137 //IsArithmetic
138 
139 template <typename T>
140 struct IsArithmetic
141 {
142  enum { value = IsIntegral<T>::value || IsFloat<T>::value };
143 };
144 
145 // IsFundamental
146 
147 template <typename T>
148 struct IsFundamental
149 {
150  enum { value = IsArithmetic<T>::value || IsVoid<T>::value };
151 };
152 
153 
154 template <typename T>
155 struct HasTrivialConstructor
156 {
157  enum { value = IsArithmetic<T>::value || IsPointer<T>::value };
158 };
159 
160 template <typename T>
161 struct HasTrivialDestructor
162 {
163  enum { value = IsArithmetic<T>::value || IsPointer<T>::value };
164 };
165 
166 // Optimized constructor && destructor for fundamental types
167 
168 namespace Internal
169 {
170  template <bool hasTrivialConstructor>
171  struct DefaultConstructorImpl;
172 
173  template <>
174  struct DefaultConstructorImpl</*hasTrivialConstructor*/false>
175  {
176  template <typename T>
177  void PlacementNew(T* ptr) { new (ptr) T; }
178 
179  template <typename T>
180  void PlacementNewRange(T* begin, T* end)
181  {
182  for (T* it = begin; it != end; ++it)
183  {
184  new (it) T;
185  }
186  }
187 
188  template <typename Iterator, typename T>
189  void ConstructRange(Iterator begin, Iterator end, T* buffer)
190  {
191  for (Iterator it = begin; it != end; ++it)
192  {
193  new (buffer) T(*it);
194  ++buffer;
195  }
196  }
197 
198  };
199 
200  template <>
201  struct DefaultConstructorImpl</*hasTrivialConstructor*/true>
202  {
203  template <typename T>
204  void PlacementNew(T* /*ptr*/) {} // no call for fundamental types
205  template <typename T>
206  void PlacementNewRange(T* /*begin*/, T* /*end*/) {} // no call for fundamental types
207 
208  template <typename T>
209  void ConstructRange(const T* begin, const T* end, T* buffer)
210  {
211  // NoOverlap
212  memcpy(buffer, begin, (end - begin) * sizeof (T));
213  }
214 
215  };
216 
217  // Destructor
218 
219  template <bool hasTrivialDestructor>
220  struct DestructorImpl;
221 
222  template <>
223  struct DestructorImpl</*hasTrivialDestructor*/false>
224  {
225  template <typename T>
226  void Destroy(T* ptr)
227  {
228  KY_UNUSED(ptr); // to avoid warning with classes with trivial destructors
229  ptr->~T();
230  }
231  template <typename T>
232  void DestroyRange(T* begin, T* end)
233  {
234  for (T* it = begin; it != end; ++it)
235  {
236  (*it).~T();
237  }
238  }
239  };
240 
241  template <>
242  struct DestructorImpl</*hasTrivialDestructor*/true>
243  {
244  template <typename T>
245  void Destroy(T* /*ptr*/) {} /* no call for fundamental types */
246  template <typename T>
247  void DestroyRange(T* /*begin*/, T* /*end*/) {}
248  };
249 
250 }
251 
252 template <typename T>
253 inline void CallDefaultConstructor(T* ptr)
254 {
255  Internal::DefaultConstructorImpl<HasTrivialConstructor<T>::value>().PlacementNew(ptr);
256 }
257 
258 template <typename T>
259 inline void CallDefaultConstructorRange(T* begin, T* end)
260 {
261  Internal::DefaultConstructorImpl<HasTrivialConstructor<T>::value>().PlacementNewRange(begin, end);
262 }
263 
264 template <typename Iterator, typename T>
265 inline void ConstructRange(Iterator begin, Iterator end, T* buffer)
266 {
267  Internal::DefaultConstructorImpl<IsPointer<Iterator>::value && HasTrivialConstructor<T>::value>().ConstructRange(begin, end, buffer);
268 }
269 
270 template <typename T>
271 inline void CallDestructor(T* ptr)
272 {
273  Internal::DestructorImpl<HasTrivialDestructor<T>::value>().Destroy(ptr);
274 }
275 
276 template <typename T>
277 inline void DestroyRange(T* begin, T* end)
278 {
279  Internal::DestructorImpl<HasTrivialDestructor<T>::value>().DestroyRange(begin, end);
280 }
281 
282 } // namespace Kaim
283 
284 #endif // Navigation_TypeTraits_H
285 
int KyInt32
Type used internally to represent a 32-bit integer.
Definition: types.h:35
unsigned __int64 KyUInt64
Type used internally to represent an unsigned 64-bit integer.
Definition: types.h:38
Definition: gamekitcrowddispersion.h:20
unsigned short KyUInt16
Type used internally to represent an unsigned 16-bit integer.
Definition: types.h:40
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36
double KyFloat64
Type used internally to represent a 64-bit floating-point number.
Definition: types.h:44
__int64 KyInt64
Type used internally to represent a 64-bit integer.
Definition: types.h:37
short KyInt16
Type used internally to represent a 16-bit integer.
Definition: types.h:39
float KyFloat32
Type used internally to represent a 32-bit floating-point number.
Definition: types.h:43