Anklang-0.3.0.dev712+gdc4e642f anklang-0.3.0.dev712+gdc4e642f
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
cxxaux.hh
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#ifndef __ASE_CXXAUX_HH__
3#define __ASE_CXXAUX_HH__
4
5#include <ase/sysconfig.h>
6#include <sys/types.h> // uint, ssize
7#include <cstdint> // uint64_t
8#include <string>
9#include <functional>
10#include <vector>
11#include <memory>
12#include <cmath>
13#include <mutex>
14#include <map>
15
16namespace Ase {
17
18typedef uint32_t uint;
19static_assert (sizeof (uint) == 4, "");
20
21// == type aliases ==
22typedef uint8_t uint8;
26typedef int8_t int8;
27typedef int16_t int16;
28typedef int32_t int32;
29typedef int64_t int64;
31static_assert (sizeof (uint8) == 1 && sizeof (uint16) == 2 && sizeof (uint32) == 4 && sizeof (uint64) == 8, "");
32static_assert (sizeof (int8) == 1 && sizeof (int16) == 2 && sizeof (int32) == 4 && sizeof (int64) == 8, "");
33static_assert (sizeof (int) == 4 && sizeof (uint) == 4 && sizeof (unichar) == 4, "");
34using std::void_t;
38using VoidF = std::function<void()>;
39
40// == Utility Macros ==
41#define ASE_CPP_STRINGIFY(s) ASE_CPP_STRINGIFY_ (s)
42#define ASE_CPP_STRINGIFY_(s) #s // Indirection helper, required to expand macros like __LINE__
43#define ASE_CPP_PASTE2_(a,b) a ## b // Indirection helper, required to expand macros like __LINE__
44#define ASE_CPP_PASTE2(a,b) ASE_CPP_PASTE2_ (a,b)
45#define ASE_ISLIKELY(expr) __builtin_expect (bool (expr), 1)
46#define ASE_UNLIKELY(expr) __builtin_expect (bool (expr), 0)
47#define ASE_ABS(a) ((a) < 0 ? -(a) : (a))
48#define ASE_MIN(a,b) ((a) <= (b) ? (a) : (b))
49#define ASE_MAX(a,b) ((a) >= (b) ? (a) : (b))
50#define ASE_CLAMP(v,mi,ma) ((v) < (mi) ? (mi) : ((v) > (ma) ? (ma) : (v)))
51#define ASE_ARRAY_SIZE(array) (sizeof (array) / sizeof ((array)[0]))
52#define ASE_ALIGN(size, base) ((base) * ((size_t (size) + (base) - 1) / (base)))
53#define ASE_ALIGNMENT16(pointer) (0xf & ptrdiff_t (pointer))
54#define ASE_ALIGNED16(pointer) (!ALIGNMENT16 (pointer))
55
56// Ase macro shorthands for <a href="https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html">GCC Attributes</a>.
57#define ASE_ALWAYS_INLINE __attribute__ ((__always_inline__))
58#define ASE_COLD __attribute__ ((__cold__))
59#define ASE_CONST __attribute__ ((__const__))
60#define ASE_CONSTRUCTOR __attribute__ ((constructor,used)) // gcc-3.3 also needs "used" to emit code
61#define ASE_DEPRECATED __attribute__ ((__deprecated__))
62#define ASE_FORMAT(fx) __attribute__ ((__format_arg__ (fx)))
63#define ASE_HOT __attribute__ ((__hot__))
64#define ASE_MALLOC __attribute__ ((__malloc__))
65#define ASE_MAY_ALIAS __attribute__ ((may_alias))
66#define ASE_NOINLINE __attribute__ ((noinline))
67#define ASE_NORETURN __attribute__ ((__noreturn__))
68#define ASE_NO_INSTRUMENT __attribute__ ((__no_instrument_function__))
69#define ASE_PRINTF(fx, ax) __attribute__ ((__format__ (__printf__, fx, ax)))
70#define ASE_PURE __attribute__ ((__pure__))
71#define ASE_SCANF(fx, ax) __attribute__ ((__format__ (__scanf__, fx, ax)))
72#define ASE_SENTINEL __attribute__ ((__sentinel__))
73#define ASE_UNUSED __attribute__ ((__unused__))
74#define ASE_USE_RESULT __attribute__ ((warn_unused_result))
75#define ASE_USED __attribute__ ((__used__))
76#define ASE_WEAK __attribute__ ((__weak__))
77
79#define ASE_RETURN_UNLESS(cond, ...) do { if (ASE_UNLIKELY (!bool (cond))) return __VA_ARGS__; } while (0)
80
82#define ASE_ASSERT_RETURN(expr, ...) do { if (expr) [[likely]] break; ::Ase::assertion_failed (#expr); return __VA_ARGS__; } while (0)
83
85#define ASE_ASSERT_RETURN_UNREACHED(...) do { ::Ase::assertion_failed (nullptr); return __VA_ARGS__; } while (0)
86
88#define ASE_ASSERT_UNREACHED(...) do { ::Ase::assertion_fatal (nullptr); } while (0)
89
91#define ASE_ASSERT(expr) do { if (expr) [[likely]] break; ::Ase::assertion_fatal (#expr); } while (0)
92
94#define ASE_ASSERT_WARN(expr) do { if (expr) [[likely]] break; ::Ase::assertion_failed (#expr); } while (0)
95
96#ifndef NDEBUG
98#define ASE_PARANOID(expr) do { if (expr) [[likely]] break; ::Ase::assertion_failed (#expr); } while (0)
99#else
100#define ASE_PARANOID(expr) do { break; } while (expr)
101#endif
102
103
105#define ASE_DIE(msg) do { errno = 0; ::Ase::perror_die (msg); } while (0)
106
108#define ASE_PERROR_DIE(msg) do { ::Ase::perror_die (msg); } while (0)
109
111#define ASE_CLASS_NON_COPYABLE(ClassName) \
112 /*copy-ctor*/ ClassName (const ClassName&) = delete; \
113 ClassName& operator= (const ClassName&) = delete
114
116#define ASE_STRUCT_DECLS(Klass) \
117 struct Klass; \
118 using ASE_CPP_PASTE2 (Klass, P) = ::std::shared_ptr<Klass>; \
119 using ASE_CPP_PASTE2 (Klass, S) = ::std::vector<Klass>;
120
122#define ASE_CLASS_DECLS(Klass) \
123 class Klass; \
124 using ASE_CPP_PASTE2 (Klass, W) = ::std::weak_ptr<Klass>; \
125 using ASE_CPP_PASTE2 (Klass, P) = ::std::shared_ptr<Klass>; \
126 using ASE_CPP_PASTE2 (Klass, S) = ::std::vector<ASE_CPP_PASTE2 (Klass, P)>;
127
128// == Operations on flags enum classes ==
129#define ASE_DEFINE_ENUM_EQUALITY(Enum) \
130 constexpr bool operator== (Enum v, int64_t n) { return int64_t (v) == n; } \
131 constexpr bool operator== (int64_t n, Enum v) { return n == int64_t (v); } \
132 constexpr bool operator!= (Enum v, int64_t n) { return int64_t (v) != n; } \
133 constexpr bool operator!= (int64_t n, Enum v) { return n != int64_t (v); }
134#define ASE_DEFINE_FLAGS_ARITHMETIC(Enum) \
135 constexpr int64_t operator>> (Enum v, int64_t n) { return int64_t (v) >> n; } \
136 constexpr int64_t operator<< (Enum v, int64_t n) { return int64_t (v) << n; } \
137 constexpr int64_t operator^ (Enum v, int64_t n) { return int64_t (v) ^ n; } \
138 constexpr int64_t operator^ (int64_t n, Enum v) { return n ^ int64_t (v); } \
139 constexpr Enum operator^ (Enum v, Enum w) { return Enum (int64_t (v) ^ w); } \
140 constexpr int64_t operator| (Enum v, int64_t n) { return int64_t (v) | n; } \
141 constexpr int64_t operator| (int64_t n, Enum v) { return n | int64_t (v); } \
142 constexpr Enum operator| (Enum v, Enum w) { return Enum (int64_t (v) | w); } \
143 constexpr int64_t operator& (Enum v, int64_t n) { return int64_t (v) & n; } \
144 constexpr int64_t operator& (int64_t n, Enum v) { return n & int64_t (v); } \
145 constexpr Enum operator& (Enum v, Enum w) { return Enum (int64_t (v) & w); } \
146 constexpr int64_t operator~ (Enum v) { return ~int64_t (v); } \
147 constexpr int64_t operator+ (Enum v) { return +int64_t (v); } \
148 constexpr int64_t operator- (Enum v) { return -int64_t (v); } \
149 constexpr int64_t operator+ (Enum v, int64_t n) { return int64_t (v) + n; } \
150 constexpr int64_t operator+ (int64_t n, Enum v) { return n + int64_t (v); } \
151 constexpr int64_t operator- (Enum v, int64_t n) { return int64_t (v) - n; } \
152 constexpr int64_t operator- (int64_t n, Enum v) { return n - int64_t (v); } \
153 constexpr int64_t operator* (Enum v, int64_t n) { return int64_t (v) * n; } \
154 constexpr int64_t operator* (int64_t n, Enum v) { return n * int64_t (v); } \
155 constexpr int64_t operator/ (Enum v, int64_t n) { return int64_t (v) / n; } \
156 constexpr int64_t operator/ (int64_t n, Enum v) { return n / int64_t (v); } \
157 constexpr int64_t operator% (Enum v, int64_t n) { return int64_t (v) % n; } \
158 constexpr int64_t operator% (int64_t n, Enum v) { return n % int64_t (v); } \
159 constexpr Enum& operator^= (Enum &e, int64_t n) { e = Enum (e ^ n); return e; } \
160 constexpr Enum& operator|= (Enum &e, int64_t n) { e = Enum (e | n); return e; } \
161 constexpr Enum& operator&= (Enum &e, int64_t n) { e = Enum (e & n); return e; } \
162 constexpr Enum& operator+= (Enum &e, int64_t n) { e = Enum (e + n); return e; } \
163 constexpr Enum& operator-= (Enum &e, int64_t n) { e = Enum (e - n); return e; } \
164 constexpr Enum& operator*= (Enum &e, int64_t n) { e = Enum (e * n); return e; } \
165 constexpr Enum& operator/= (Enum &e, int64_t n) { e = Enum (e / n); return e; } \
166 constexpr Enum& operator%= (Enum &e, int64_t n) { e = Enum (e % n); return e; } \
167 ASE_DEFINE_ENUM_EQUALITY (Enum)
168
170template<typename UInt> static inline constexpr UInt
171rotr (UInt bits, uint32_t offset)
172{
173 const uint32_t wrapped_bits = sizeof (bits) * 8 - offset;
174 return (bits >> offset) | (bits << wrapped_bits);
175}
176
178template<typename UInt> static inline constexpr UInt
179rotl (UInt bits, uint32_t offset)
180{
181 const uint32_t wrapped_bits = sizeof (bits) * 8 - offset;
182 return (bits << offset) | (bits >> wrapped_bits);
183}
184
186template<typename T> static inline constexpr T
187divmod (T dividend, T divisor, T *reminderp)
188{
189 *reminderp = dividend % divisor; // uses single DIV or IDIV for %
190 return dividend / divisor; // and / in gcc and clang with -O1
191}
192
194const char* cxx_demangle (const std::type_info &typeinfo) noexcept;
195std::string cxx_demangle (const char *mangled_identifier) noexcept;
196
198template<class T> ASE_PURE static inline const char*
199typeid_name()
200{
201 return cxx_demangle (typeid (T));
202}
203
205template<class T> ASE_PURE static inline const char*
206typeid_name (T &obj)
207{
208 return cxx_demangle (typeid (obj));
209}
210
212template<typename T> static T*
213unalias_ptr (T *ptr)
214{
215 asm ("" : "+r" (ptr)); // accept ptr and yield ptr
216 return ptr;
217}
218
221protected:
222 virtual ~VirtualBase() noexcept = 0;
223};
225
227void perror_die (const std::string &msg) noexcept ASE_NORETURN;
228void assertion_failed (const char *msg = nullptr, const char *file = __builtin_FILE(),
229 int line = __builtin_LINE(), const char *func = __builtin_FUNCTION()) noexcept;
230void assertion_fatal (const char *msg = nullptr, const char *file = __builtin_FILE(),
231 int line = __builtin_LINE(), const char *func = __builtin_FUNCTION()) noexcept ASE_NORETURN;
232
234void ase_rethrow (std::exception_ptr exception = std::current_exception());
235
237extern inline constexpr bool
238constexpr_equals (const char *a, const char *b, size_t n)
239{
240 return n == 0 || (a[0] == b[0] && (a[0] == 0 || constexpr_equals (a + 1, b + 1, n - 1)));
241}
242
244template<class Type, class ...Ts> ASE_ALWAYS_INLINE inline void
245new_inplace (Type &typemem, Ts &&... args)
246{
247 new (&typemem) Type (std::forward<Ts> (args)...);
248}
249
251template<class Type> ASE_ALWAYS_INLINE inline void
252delete_inplace (Type &typemem)
253{
254 typemem.~Type();
255}
256
258template<class T> void
260{
261 delete o; // automatically handles nullptr
262}
263
265template<bool value> using REQUIRES = typename ::std::enable_if<value, bool>::type;
266
268template<bool value> using REQUIRESv = typename ::std::enable_if<value, void>::type;
269
274#define ASE_DEFINE_MAKE_SHARED(CLASS) \
275 struct MakeSharedAllocator_ : std::allocator<CLASS> { \
276 template<typename ...Args> \
277 static void construct (CLASS *p, Args &&...args) \
278 { ::new (p) CLASS (std::forward<Args> (args)...); } \
279 static void destroy (CLASS *p) \
280 { p->~CLASS (); } \
281 template<typename U> struct rebind { \
282 using other = \
283 std::conditional_t<std::is_same<U, CLASS>::value, \
284 MakeSharedAllocator_, std::allocator<U>>; \
285 }; \
286 }; \
287 template<typename ...Args> static std::shared_ptr<CLASS> \
288 make_shared (Args &&...args) \
289 { \
290 return std::allocate_shared<CLASS> (MakeSharedAllocator_(), \
291 std::forward<Args> (args)...); \
292 }
293
310template<class Target, class Source> std::shared_ptr<typename std::remove_pointer<Target>::type>
311shared_ptr_cast (Source *object)
312{
313 if (!object)
314 return nullptr;
315 // construct shared_ptr if possible
316 typedef decltype (object->shared_from_this()) ObjectP;
317 ObjectP sptr;
319 try {
320 sptr = object->shared_from_this();
321 } catch (const std::bad_weak_ptr&) {
322 return nullptr;
323 }
324 else // for non-pointers, allow bad_weak_ptr exceptions
325 sptr = object->shared_from_this();
326 // cast into target shared_ptr<> type
328}
330template<class Target, class Source> const std::shared_ptr<typename std::remove_pointer<Target>::type>
331shared_ptr_cast (const Source *object)
332{
333 return shared_ptr_cast<Target> (const_cast<Source*> (object));
334}
336template<class Target, class Source> std::shared_ptr<typename std::remove_pointer<Target>::type>
342template<class Target, class Source> const std::shared_ptr<typename std::remove_pointer<Target>::type>
344{
345 return shared_ptr_cast<Target> (const_cast<std::shared_ptr<Source>&> (sptr));
346}
347
350shared_ptr_from (Source *object)
351{
352 // ultimately calls shared_from_this()
353 return shared_ptr_cast<Source> (object);
354}
355
357template<class C> std::shared_ptr<C>
359{
360 std::shared_ptr<C> cptr = wptr.lock();
361 if (__builtin_expect (!!cptr, true))
362 return cptr; // fast path
363 std::shared_ptr<C> nptr = ctor();
364 { // C++20 has: std::atomic<std::weak_ptr<C>>::compare_exchange
365 static std::mutex mutex;
366 std::lock_guard<std::mutex> locker (mutex);
367 cptr = wptr.lock();
368 if (!cptr)
369 wptr = cptr = nptr;
370 }
371 return cptr;
372}
373
378template<class Class>
379class Persistent final {
380 static_assert (std::is_class<Class>::value, "Persistent<Class> requires class template argument");
381 uint64 mem_[(sizeof (Class) + sizeof (uint64) - 1) / sizeof (uint64)] = { 0, };
382 Class *ptr_ = nullptr;
383 void
384 initialize() ASE_NOINLINE
385 {
386 static std::mutex mtx;
388 if (ptr_ == nullptr)
389 ptr_ = new (mem_) Class(); // exclusive construction
390 }
391public:
393 constexpr Persistent () noexcept {}
395 explicit operator bool () const ASE_PURE { return ptr_ != nullptr; }
397 Class& operator* () ASE_PURE { return *operator->(); }
399 Class* operator-> () ASE_PURE
400 {
401 if (ASE_UNLIKELY (ptr_ == nullptr))
402 initialize();
403 return ptr_;
404 }
405};
406
408struct Id32 {
409 template<typename T,
410 REQUIRES< (sizeof (T) <= 4 && (std::is_integral<T>::value || std::is_enum<T>::value)) > = true>
411 constexpr
412 Id32 (T eid) :
413 id (uint32_t (eid))
414 {}
415 operator uint32_t () const noexcept { return id; }
416 bool operator== (int64_t i) const noexcept { return id == i; }
417 bool operator!= (int64_t i) const noexcept { return id != i; }
418 friend bool operator== (int64_t i, const Id32 &id) { return id.operator== (i); }
419 friend bool operator!= (int64_t i, const Id32 &id) { return id.operator!= (i); }
420 uint32_t id;
421};
422
423} // Ase
424
425#endif // __ASE_CXXAUX_HH__
Create an instance of Class on demand that is constructed and never destructed.
Definition cxxaux.hh:379
constexpr Persistent() noexcept
A constexpr constructor avoids the static initialization order fiasco.
Definition cxxaux.hh:393
Class * operator->() __attribute__((__pure__))
Retrieve pointer to Class instance, always returns the same pointer.
Definition cxxaux.hh:399
Class & operator*() __attribute__((__pure__))
Retrieve reference to Class instance, always returns the same reference.
Definition cxxaux.hh:397
#define ASE_UNLIKELY(expr)
Compiler hint to optimize for expr evaluating to false.
Definition cxxaux.hh:46
typedef int
The Anklang C++ API namespace.
Definition api.hh:9
void delete_inplace(Type &typemem)
Call inplace delete operator by automatically inferring the Type.
Definition cxxaux.hh:252
uint64_t uint64
A 64-bit unsigned integer, use PRI*64 in format strings.
Definition cxxaux.hh:25
void call_delete(T *o)
Simple way to create a standalone callback to delete an object of type T.
Definition cxxaux.hh:259
void assertion_failed(const char *msg, const char *file, int line, const char *func) noexcept
Print instructive message, handle "breakpoint", "backtrace" and "fatal-warnings" in $ASE_DEBUG.
Definition cxxaux.cc:81
int32_t int32
A 32-bit signed integer.
Definition cxxaux.hh:28
int16_t int16
A 16-bit signed integer.
Definition cxxaux.hh:27
uint8_t uint8
An 8-bit unsigned integer.
Definition cxxaux.hh:22
const char * cxx_demangle(const std::type_info &typeinfo) noexcept
Demangle a std::typeinfo.name() string into a proper C++ type name.
Definition cxxaux.cc:23
int8_t int8
An 8-bit signed integer.
Definition cxxaux.hh:26
int64_t int64
A 64-bit unsigned integer, use PRI*64 in format strings.
Definition cxxaux.hh:29
std::shared_ptr< C > weak_ptr_fetch_or_create(std::weak_ptr< C > &wptr, const std::function< std::shared_ptr< C >()> &ctor)
Fetch shared_ptr from wptr and create C with ctor if needed.
Definition cxxaux.hh:358
typename ::std::enable_if< value, bool >::type REQUIRES
REQUIRES<value> - Simplified version of std::enable_if<cond,bool>::type to use SFINAE in function tem...
Definition cxxaux.hh:265
void assertion_fatal(const char *msg, const char *file, int line, const char *func) noexcept
Print a debug message via assertion_failed() and abort the program.
Definition cxxaux.cc:74
void perror_die(const std::string &msg) noexcept
Issue a warning about an assertion error.
Definition cxxaux.cc:62
typename ::std::enable_if< value, void >::type REQUIRESv
REQUIRESv<value> - Simplified version of std::enable_if<cond,void>::type to use SFINAE in struct temp...
Definition cxxaux.hh:268
constexpr bool constexpr_equals(const char *a, const char *b, size_t n)
Test string equality at compile time.
Definition cxxaux.hh:238
std::shared_ptr< typename std::remove_pointer< Source >::type > shared_ptr_from(Source *object)
Use shared_ptr_cast<>() to convert an object pointer into a shared_ptr<>.
Definition cxxaux.hh:350
uint32_t unichar
A 32-bit unsigned integer used for Unicode characters.
Definition cxxaux.hh:30
uint16_t uint16
A 16-bit unsigned integer.
Definition cxxaux.hh:23
void new_inplace(Type &typemem, Ts &&... args)
Call inplace new operator by automatically inferring the Type.
Definition cxxaux.hh:245
uint32_t uint32
A 32-bit unsigned integer.
Definition cxxaux.hh:24
std::shared_ptr< typename std::remove_pointer< Target >::type > shared_ptr_cast(Source *object)
Shorthand for std::dynamic_pointer_cast<>(shared_from_this()).
Definition cxxaux.hh:311
uint32_t uint
Provide 'uint' as convenience type.
Definition cxxaux.hh:18
void ase_rethrow(std::exception_ptr exception)
Helper to trace rethrown exceptions.
Definition cxxaux.cc:87
T rotl(T... args)
typedef uint32_t
Helper class for integer IDs up to 32 Bit, possibly of enum type.
Definition cxxaux.hh:408
Common base type to allow casting between polymorphic classes.
Definition cxxaux.hh:220