-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHandyUtils.hpp
352 lines (289 loc) · 9.82 KB
/
HandyUtils.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/// ========================================================================
/// UNLICENSE
///
/// This is free and unencumbered software released into the public domain.
/// Anyone is free to copy, modify, publish, use, compile, sell, or
/// distribute this software, either in source code form or as a compiled
/// binary, for any purpose, commercial or non-commercial, and by any
/// means.
///
/// In jurisdictions that recognize copyright laws, the author or authors
/// of this software dedicate any and all copyright interest in the
/// software to the public domain. We make this dedication for the benefit
/// of the public at large and to the detriment of our heirs and
/// successors. We intend this dedication to be an overt act of
/// relinquishment in perpetuity of all present and future rights to this
/// software under copyright law.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
/// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
/// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
/// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
/// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
/// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
/// OTHER DEALINGS IN THE SOFTWARE.
///
/// For more information, please refer to <http://unlicense.org/>
/// ========================================================================
#pragma once
#include <functional>
#include <chrono>
#include <string>
#include <iostream>
#include <sstream>
#include <atomic>
#include <random>
#ifdef __GNUG__
#include <cxxabi.h> // for TypeInfo
#endif
#if defined IS_WINDOWS
#include <intrin.h>
#pragma intrinsic(_BitScanForward)
#pragma intrinsic(_BitScanReverse)
#pragma intrinsic(_BitScanForward64)
#pragma intrinsic(_BitScanReverse64)
#endif
#include "HandyBase.hpp"
#include "HandyCompat.hpp"
#include "HandyEncoding.hpp"
#include "HandySerDe.hpp"
namespace HANDY_NS {
inline void PrintSystemInfo()
{
std::cout << BuildInfoString();
}
/// Escape will allow you to prevent compiler value pre-evaluation optimization for
/// a specific (presumably a result) value/variable. Great for benchmarking intrinsics.
#ifdef _MSC_VER
#pragma optimize("", off)
inline void Escape(void * p)
{
*reinterpret_cast<char volatile*>(p) =
*reinterpret_cast<char const volatile*>(p);
}
#pragma optimize("", on)
#else
// Only works on GGC/Clang: Boo.
inline void Clobber() { asm volatile("" : : : "memory"); }
inline void Escape(void* p) { asm volatile("" : : "g"(p) : "memory"); }
#endif
/// Errno checker for Windows and *nix, returns empty string if no error.
/// Does not clear errors, just reports them. Typically, you'll call both in sequence.
/// Convenient to use as part of a render loop, etc...
std::string GetAnyError();
void ClearErrors();
class OnScopeExit final
{
public:
inline explicit OnScopeExit(std::function<void()> onExit) : m_onExit(onExit) {}
inline ~OnScopeExit() { m_onExit(); }
private:
std::function<void()> m_onExit;
OnScopeExit(const OnScopeExit &) = delete;
OnScopeExit & operator=(OnScopeExit const &) = delete;
};
class StopWatch
{
typedef std::chrono::steady_clock clock;
typedef std::chrono::microseconds microseconds;
typedef std::chrono::milliseconds milliseconds;
typedef std::chrono::seconds seconds;
clock::time_point mStart;
public:
FORCEINLINE StopWatch() : mStart(clock::now()) { static_assert(std::chrono::steady_clock::is_steady, "Serious OS/C++ library issues. std::chrono::steady_clock is not steady."); }
FORCEINLINE StopWatch(StopWatch const & other) : mStart(other.mStart) { }
FORCEINLINE StopWatch & operator=(StopWatch const & rhs) { mStart = rhs.mStart; return *this; }
FORCEINLINE uint64_t Microseconds() const { return std::chrono::duration_cast<microseconds>(clock::now() - mStart).count(); }
FORCEINLINE uint64_t Milliseconds() const { return std::chrono::duration_cast<milliseconds>(clock::now() - mStart).count(); }
FORCEINLINE uint64_t Seconds() const { return std::chrono::duration_cast<seconds> (clock::now() - mStart).count(); }
FORCEINLINE float SecondsF() const { return std::chrono::duration_cast<std::chrono::duration<float >>(clock::now() - mStart).count(); }
FORCEINLINE double SecondsD() const { return std::chrono::duration_cast<std::chrono::duration<double>>(clock::now() - mStart).count(); }
FORCEINLINE auto Duration() const { return clock::now() - mStart; }
FORCEINLINE void Restart() { mStart = clock::now();}
};
/// Timer that prints how long the object existed, from ctor to dtor.
class DisposeTimer final
{
StopWatch m_timer;
std::string m_message;
bool m_logged = false;
public:
FORCEINLINE explicit DisposeTimer(std::string const & message = "")
: m_timer(),
m_message(message)
{ }
FORCEINLINE ~DisposeTimer()
{
if (m_logged)
return;
m_logged = true;
if (m_message.size() == 0)
std::cout << "Time : " << m_timer.SecondsF() << std::endl;
else
std::cout << m_message << " : " << m_timer.SecondsF() << std::endl;
}
};
class UIDMixin
{
static std::atomic_int64_t & s_lastUID() { static std::atomic_int64_t instance = 0; return instance; }
public:
static void ResetCounter() { s_lastUID() = 0; }
int64_t UID;
UIDMixin() : UID(s_lastUID()++) { }
bool operator<(UIDMixin const & rhs) const { return UID < rhs.UID; }
};
/// Can be used as a mixin or standalone
class UniqueName
{
static uint64_t getInc() { static std::atomic_uint64_t instance = 0; return instance.operator++(); };
public:
uint64_t const UniqueValue; /// DO NOT CHANGE DECLARATION ORDER
std::string const UniqueSuffix;
UniqueName()
: UniqueValue(getInc())
, UniqueSuffix(std::to_string(UniqueValue))
{ }
virtual ~UniqueName() = default;
};
template <typename T>
T RandomInRange(T min, T max)
{
static std::random_device rd; // only used once to initialise (seed) engine
static std::mt19937 rng(rd()); // random-number engine used (Mersenne-Twister in this case)
static std::mutex mtx;
static_assert(std::is_integral<T>::value || std::is_floating_point<T>::value, "Unsupported Type: RandomInRange<T>");
Locked (mtx)
{
if constexpr (std::is_integral<T>::value)
{
std::uniform_int_distribution<T> uni(min,max); // guaranteed unbiased
return uni(rng);
}
else /// Then it must be floating point, due to the assert above.
{
std::uniform_real_distribution<T> uni(min,max); // guaranteed unbiased
return uni(rng);
}
}
}
template <typename T>
class OnlyOne
{
public:
static std::shared_ptr<T> Get()
{
static std::weak_ptr<T> Existing;
auto instance = Existing.lock();
if (!instance)
{
instance.reset(new T());
Existing = instance;
}
return instance;
}
};
/// Search the mask data from least significant bit (LSB) to the most significant bit (MSB) for a set bit (1).
/// Returns 64 if there are no set bits, otherwise returns the index of the first set bit.
FORCEINLINE uint8_t BitscanLSB(uint64_t n)
{
#if defined IS_WINDOWS
unsigned long result = 0;
if (!_BitScanForward64(&result, n))
return 64_u8;
return (uint8_t)result;
#else
if (!n)
return 64;
return (uint8_t)__builtin_ctzll(n);
#endif
}
/// Search the mask data from most significant bit (MSB) to least significant bit (LSB) for a set bit (1).
/// Returns 64 if there are no set bits, otherwise returns the index of the first set bit.
FORCEINLINE uint8_t BitscanMSB(uint64_t n)
{
#if defined IS_WINDOWS
unsigned long result = 0;
if (!_BitScanReverse64(&result, n))
return 64_u8;
return (uint8_t)result;
#else
if (!n)
return 64;
return (uint8_t)__builtin_clzll(n);
#endif
}
#if !defined IS_ANDROID
///-----------------------------------------------
/// Type Info
///
class TypeDescriptor;
typedef const TypeDescriptor * TypeInfo;
template <class TYPE> TypeInfo NOINLINE FCONST typeInfo();
class TypeDescriptor
{
public:
const std::string name;
const size_t size;
private:
TypeDescriptor(const std::type_info & info, size_t sz);
template <class TYPE> friend TypeInfo typeInfo();
};
template <class TYPE> TypeInfo NOINLINE FCONST typeInfo()
{
static const TypeDescriptor descriptor(typeid(TYPE), sizeof(TYPE));
return &descriptor;
}
template <class TYPE> inline FCONST TypeInfo typeInfo(TYPE)
{
return typeInfo<TYPE>();
}
namespace detail {
#ifdef IS_GPP
namespace
{
struct nameptr
{
char * p;
nameptr(char * ptr) : p(ptr) {}
~nameptr() { free(p); }
};
}
static std::string UnmangleName(const char * name)
{
int status = -1;
nameptr ptr(abi::__cxa_demangle(name, nullptr, nullptr, &status));
return (status == 0 ? ptr.p : name);
}
#else
static std::string UnmangleName(const char * name)
{
return name;
}
#endif
}
inline
TypeDescriptor::TypeDescriptor(const std::type_info & info, size_t sz)
: name(detail::UnmangleName(info.name()))
, size(sz)
{ }
///-----------------------------------------------
#endif
#if !defined IS_ANDROID
//Get the paths of all sub-directories of a given directory
inline std::vector<std::filesystem::path> SubDirectories(std::filesystem::path DirPath)
{
std::vector<std::filesystem::path> subdirs;
if ((! std::filesystem::exists(DirPath)) || (! std::filesystem::is_directory(DirPath)))
return subdirs;
std::filesystem::directory_iterator dirIter{DirPath};
while (dirIter != std::filesystem::directory_iterator{})
{
std::filesystem::path myPath = dirIter->path();
if (std::filesystem::is_directory(myPath) && (! std::filesystem::equivalent(myPath, DirPath)))
subdirs.push_back(myPath);
dirIter++;
}
return subdirs;
}
#endif
} // HANDY_NS