gwnavruntime/base/typetraits.h Source File

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