gwnavruntime/base/types.h Source File

types.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 #pragma once
8 
10 #include <math.h>
11 
12 //------------------------------------------------------------------------------------------------
13 // KY_NO_LOG_OUTPUT is different from KY_NO_DEBUG_OUTPUT
14 // => KY_NO_DEBUG_OUTPUT disables KY_DEBUG_XXX macros
15 // => KY_NO_LOG_OUTPUT disables all KY_LOG_XXX macros and Kaim::LogStream() <<
16 #if defined(KY_CONFIG_RELEASE)
17 # define KY_NO_LOG_OUTPUT
18 #endif
19 
20 // The comments are necessary to avoid doxygen warning: explicit link request to 'KyInt8' could not be resolved
21 
22 typedef std::int8_t KyInt8;
23 typedef std::int16_t KyInt16;
24 typedef std::int32_t KyInt32;
25 typedef std::int64_t KyInt64;
26 
27 typedef std::uint8_t KyUInt8;
28 typedef std::uint16_t KyUInt16;
29 typedef std::uint32_t KyUInt32;
30 typedef std::uint64_t KyUInt64;
31 
32 typedef float KyFloat32;
33 typedef double KyFloat64;
34 
35 // when we drop vc11, let's prefer the using syntax
36 
37 // using KyInt8 = std::int8_t;
38 // using KyInt16 = std::int16_t;
39 // using KyInt32 = std::int32_t;
40 // using KyInt64 = std::int64_t;
41 
42 // using KyUInt8 = std::uint8_t;
43 // using KyUInt16 = std::uint16_t;
44 // using KyUInt32 = std::uint32_t;
45 // using KyUInt64 = std::uint64_t;
46 
47 // using KyFloat32 = float;
48 // using KyFloat64 = double;
49 
50 
51 //----------------------------------------------------------------------------------------
52 // MAXVAL and MINVAL
53 
54 #define KyInt8MAXVAL 0x7F
55 #define KyInt8MINVAL 0x80
56 
57 #define KyInt16MAXVAL 0x7FFF
58 #define KyInt16MINVAL 0x8000
59 
60 #define KyInt32MAXVAL 0x7FFFFFFF
61 #define KyInt32MINVAL 0x80000000
62 
63 #define KyInt64MAXVAL 0x7FFFFFFFFFFFFFFFLL
64 #define KyInt64MINVAL 0x8000000000000000LL
65 
66 #define KyUInt8MAXVAL 0xFF
67 #define KyUInt16MAXVAL 0xFFFF
68 #define KyUInt32MAXVAL 0xFFFFFFFF
69 #define KyUInt64MAXVAL 0xFFFFFFFFFFFFFFFFULL
70 
71 #define KyFloat32MAXVAL 3.402823466e+38f
72 
73 //----------------------------------------------------------------------------------------
74 // KY_PERF_MARKERS_ENABLED
75 #if defined(KY_CONFIG_DEV)
76 # define KY_PERF_MARKERS_ENABLED
77 #endif
78 
79 //----------------------------------------------------------------------------------------
80 // KY_DEPRECATED
87 #ifndef KY_DEPRECATED
88 # if defined (KY_CC_MSVC)
89 # define KY_DEPRECATED(expr) __declspec(deprecated) expr
90 # elif defined (KY_CC_GCC) || defined(KY_CC_CLANG)
91 # define KY_DEPRECATED(expr) expr __attribute__((deprecated))
92 # else
93 # define KY_DEPRECATED(expr) expr
94 # endif
95 #endif
96 
97 //----------------------------------------------------------------------------------------
98 // KyResult
99 
103 namespace Kaim
104 {
105 
108 struct Result
109 {
110  static Result Success() { return Result(1); }
111  static Result Failure() { return Result(0); }
112  static Result Error() { return Result(0); }
113 
114  static bool Check(Result result) { return result == Success(); }
115  static bool Fail(Result result) { return result == Failure(); }
116 
117  explicit Result(KyUInt8 code_) : code(code_) {}
118 
119  bool operator==(Result r) const { return code == r.code; }
120  bool operator!=(Result r) const { return code != r.code; }
121  operator bool() const { return code != 0; }
122  bool operator!() const { return code == 0; }
123 
124  KyUInt8 code;
125 };
126 
127 }
128 
129 typedef Kaim::Result KyResult; // using KyResult = Kaim::Result;
130 
132 #define KY_ERROR KyResult::Error()
133 
134 // use `result == KY_SUCCESS to test for success
135 #define KY_SUCCESS KyResult::Success()
136 
137 // deprecated since 2.17.0, use == KY_ERROR instead
138 KY_DEPRECATED(bool KY_FAILED(KyResult result));
139 KY_DEPRECATED(bool KY_SUCCEEDED(KyResult result));
140 inline bool KY_FAILED(KyResult result) { return result == Kaim::Result::Error(); }
141 inline bool KY_SUCCEEDED(KyResult result) { return result == Kaim::Result::Success(); }
142 
144 #define KY_TRY(expr) { KyResult result = (expr); if (result == Kaim::Result::Error()) { return result; }}
145 
146 
147 //----------------------------------------------------------------------------------------
148 // KY_MACRO_START KY_MACRO_END
149 #if defined(KY_CC_MSVC)
150 # define KY_MACRO_START \
151  do { \
152  KY_PUSH_COMPILE_WARNING \
153  KY_PRAGMA_WARNING_DISABLE(4127)
154 # define KY_MACRO_END \
155  } while(0) \
156  KY_POP_COMPILE_WARNING
157 #else
158 # define KY_MACRO_START do {
159 # define KY_MACRO_END } while(0)
160 #endif
161 
162 
163 //----------------------------------------------------------------------------------------
164 // KY_DISABLE_ALIGN_WARNING_START KY_DISABLE_ALIGN_WARNING_END
165 #if defined(KY_CC_MSVC)
166 # define KY_DISABLE_ALIGN_WARNING_START KY_PUSH_COMPILE_WARNING KY_PRAGMA_WARNING_DISABLE(4324) // structure was padded due to __declspec(align())
167 # define KY_DISABLE_ALIGN_WARNING_END KY_POP_COMPILE_WARNING
168 #else
169 # define KY_DISABLE_ALIGN_WARNING_START
170 # define KY_DISABLE_ALIGN_WARNING_END
171 #endif
172 
173 
174 //----------------------------------------------------------------------------------------
175 // KY_FORCE_ALIGNMENT
176 #if defined (KY_CC_MSVC)
177 # define KY_FORCE_ALIGNMENT(alignment, declaration) __declspec(align(alignment)) declaration
178 #elif defined (KY_CC_GCC) || defined (KY_CC_CLANG)
179 # define KY_FORCE_ALIGNMENT(alignment, declaration) declaration __attribute__((aligned(alignment)))
180 #endif
181 
182 
183 //----------------------------------------------------------------------------------------
184 // ArraySize
185 namespace Kaim
186 {
188 template<typename T, KyUInt32 N>
189 KY_INLINE KyUInt32 ArraySize(T (&) [N]) { return N; }
190 }
191 
192 
193 //----------------------------------------------------------------------------------------
194 // KY_CLASS_WITHOUT_COPY
196 #define KY_CLASS_WITHOUT_COPY(ClassName) \
197 private: \
198  ClassName(const ClassName& rhs); \
199  ClassName& operator=(const ClassName& rhs);
200 
201 
202 namespace Kaim
203 {
204 //----------------------------------------------------------------------------------------
207 template <typename F> struct ScopeExit
208 {
209  explicit ScopeExit(const F& func_) : func(func_), active(true) {}
210  ~ScopeExit()
211  {
212  if (active)
213  func();
214  }
215 
216  // when we drop vc11, let's use the = delete syntax
217  //ScopeExit(ScopeExit<F>&) = delete;
218  //ScopeExit& operator=(ScopeExit<F>&) = delete;
219 private:
220  ScopeExit(ScopeExit<F>&);
221  ScopeExit& operator=(ScopeExit<F>&);
222 
223 public:
224  ScopeExit(ScopeExit<F>&& moved) : func(moved.func), active(moved.active) { moved.active = false; }
225 
226  ScopeExit& operator=(ScopeExit<F>&& moved)
227  {
228  func = moved.func;
229  active = moved.active;
230  moved.active = false;
231  }
232 
233 private:
234  F func;
235  bool active;
236 };
237 // Get type F from lambda/functor/function pointer
238 template <typename F> inline ScopeExit<F> MakeScopeExit(const F& func) { return ScopeExit<F>(func); }
239 }
240 
241 //----------------------------------------------------------------------------------------
242 #if defined(KY_CC_MSVC) && (KY_CC_MSVC <= 1800) // vc12 or previous
243 #define KY_RETURN_MOVE(x) std::move(x)
244 #else
245 #define KY_RETURN_MOVE(x) x
246 #endif
247 
248 
249 
250 
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
#define KY_DEPRECATED(expr)
The compiler issues a warning when a deprecated function or typedef is used.
Definition: types.h:93
Navigation return code class.
Definition: types.h:108
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
KyUInt32 ArraySize(T(&)[N])
Returns the size of a fixed-size array.
Definition: types.h:189
std::int32_t KyInt32
int32_t
Definition: types.h:24
std::int8_t KyInt8
int8_t
Definition: types.h:22
double KyFloat64
double
Definition: types.h:33
std::uint8_t KyUInt8
uint8_t
Definition: types.h:27
std::int16_t KyInt16
int16_t
Definition: types.h:23
float KyFloat32
float
Definition: types.h:32