Anklang 0.3.0-460-gc4ef46ba
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
97#define ASE_ASSERT_PARANOID(expr) do { if (expr) [[likely]] break; ::Ase::assertion_failed (#expr); } while (0)
98
100#define ASE_DIE(msg) do { errno = 0; ::Ase::perror_die (msg); } while (0)
101
103#define ASE_PERROR_DIE(msg) do { ::Ase::perror_die (msg); } while (0)
104
106#define ASE_CLASS_NON_COPYABLE(ClassName) \
107 /*copy-ctor*/ ClassName (const ClassName&) = delete; \
108 ClassName& operator= (const ClassName&) = delete
109
111#define ASE_STRUCT_DECLS(Klass) \
112 struct Klass; \
113 using ASE_CPP_PASTE2 (Klass, P) = ::std::shared_ptr<Klass>; \
114 using ASE_CPP_PASTE2 (Klass, S) = ::std::vector<Klass>;
115
117#define ASE_CLASS_DECLS(Klass) \
118 class Klass; \
119 using ASE_CPP_PASTE2 (Klass, W) = ::std::weak_ptr<Klass>; \
120 using ASE_CPP_PASTE2 (Klass, P) = ::std::shared_ptr<Klass>; \
121 using ASE_CPP_PASTE2 (Klass, S) = ::std::vector<ASE_CPP_PASTE2 (Klass, P)>;
122
123// == Operations on flags enum classes ==
124#define ASE_DEFINE_ENUM_EQUALITY(Enum) \
125 constexpr bool operator== (Enum v, int64_t n) { return int64_t (v) == n; } \
126 constexpr bool operator== (int64_t n, Enum v) { return n == int64_t (v); } \
127 constexpr bool operator!= (Enum v, int64_t n) { return int64_t (v) != n; } \
128 constexpr bool operator!= (int64_t n, Enum v) { return n != int64_t (v); }
129#define ASE_DEFINE_FLAGS_ARITHMETIC(Enum) \
130 constexpr int64_t operator>> (Enum v, int64_t n) { return int64_t (v) >> n; } \
131 constexpr int64_t operator<< (Enum v, int64_t n) { return int64_t (v) << n; } \
132 constexpr int64_t operator^ (Enum v, int64_t n) { return int64_t (v) ^ n; } \
133 constexpr int64_t operator^ (int64_t n, Enum v) { return n ^ int64_t (v); } \
134 constexpr Enum operator^ (Enum v, Enum w) { return Enum (int64_t (v) ^ w); } \
135 constexpr int64_t operator| (Enum v, int64_t n) { return int64_t (v) | n; } \
136 constexpr int64_t operator| (int64_t n, Enum v) { return n | int64_t (v); } \
137 constexpr Enum operator| (Enum v, Enum w) { return Enum (int64_t (v) | w); } \
138 constexpr int64_t operator& (Enum v, int64_t n) { return int64_t (v) & n; } \
139 constexpr int64_t operator& (int64_t n, Enum v) { return n & int64_t (v); } \
140 constexpr Enum operator& (Enum v, Enum w) { return Enum (int64_t (v) & w); } \
141 constexpr int64_t operator~ (Enum v) { return ~int64_t (v); } \
142 constexpr int64_t operator+ (Enum v) { return +int64_t (v); } \
143 constexpr int64_t operator- (Enum v) { return -int64_t (v); } \
144 constexpr int64_t operator+ (Enum v, int64_t n) { return int64_t (v) + n; } \
145 constexpr int64_t operator+ (int64_t n, Enum v) { return n + int64_t (v); } \
146 constexpr int64_t operator- (Enum v, int64_t n) { return int64_t (v) - n; } \
147 constexpr int64_t operator- (int64_t n, Enum v) { return n - int64_t (v); } \
148 constexpr int64_t operator* (Enum v, int64_t n) { return int64_t (v) * n; } \
149 constexpr int64_t operator* (int64_t n, Enum v) { return n * int64_t (v); } \
150 constexpr int64_t operator/ (Enum v, int64_t n) { return int64_t (v) / n; } \
151 constexpr int64_t operator/ (int64_t n, Enum v) { return n / int64_t (v); } \
152 constexpr int64_t operator% (Enum v, int64_t n) { return int64_t (v) % n; } \
153 constexpr int64_t operator% (int64_t n, Enum v) { return n % int64_t (v); } \
154 constexpr Enum& operator^= (Enum &e, int64_t n) { e = Enum (e ^ n); return e; } \
155 constexpr Enum& operator|= (Enum &e, int64_t n) { e = Enum (e | n); return e; } \
156 constexpr Enum& operator&= (Enum &e, int64_t n) { e = Enum (e & n); return e; } \
157 constexpr Enum& operator+= (Enum &e, int64_t n) { e = Enum (e + n); return e; } \
158 constexpr Enum& operator-= (Enum &e, int64_t n) { e = Enum (e - n); return e; } \
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 ASE_DEFINE_ENUM_EQUALITY (Enum)
163
165template<typename UInt> static inline constexpr UInt
166rotr (UInt bits, uint32_t offset)
167{
168 const uint32_t wrapped_bits = sizeof (bits) * 8 - offset;
169 return (bits >> offset) | (bits << wrapped_bits);
170}
171
173template<typename UInt> static inline constexpr UInt
174rotl (UInt bits, uint32_t offset)
175{
176 const uint32_t wrapped_bits = sizeof (bits) * 8 - offset;
177 return (bits << offset) | (bits >> wrapped_bits);
178}
179
181template<typename T> static inline constexpr T
182divmod (T dividend, T divisor, T *reminderp)
183{
184 *reminderp = dividend % divisor; // uses single DIV or IDIV for %
185 return dividend / divisor; // and / in gcc and clang with -O1
186}
187
189const char* string_demangle_cxx (const char *mangled_identifier) noexcept;
190
192template<class T> ASE_PURE static inline const char*
193typeid_name()
194{
195 return string_demangle_cxx (typeid (T).name());
196}
197
199template<class T> ASE_PURE static inline const char*
200typeid_name (T &obj)
201{
202 return string_demangle_cxx (typeid (obj).name());
203}
204
206template<typename T> static T*
207unalias_ptr (T *ptr)
208{
209 asm ("" : "+r" (ptr)); // accept ptr and yield ptr
210 return ptr;
211}
212
215protected:
216 virtual ~VirtualBase() noexcept = 0;
217};
219
221void perror_die (const std::string &msg) noexcept ASE_NORETURN;
222void assertion_failed (const char *msg = nullptr, const char *file = __builtin_FILE(),
223 int line = __builtin_LINE(), const char *func = __builtin_FUNCTION()) noexcept;
224void assertion_fatal (const char *msg = nullptr, const char *file = __builtin_FILE(),
225 int line = __builtin_LINE(), const char *func = __builtin_FUNCTION()) noexcept ASE_NORETURN;
226extern bool assertion_failed_fatal;
227
229std::string backtrace_command ();
230
232extern inline constexpr bool
233constexpr_equals (const char *a, const char *b, size_t n)
234{
235 return n == 0 || (a[0] == b[0] && (a[0] == 0 || constexpr_equals (a + 1, b + 1, n - 1)));
236}
237
239template<class Type, class ...Ts> __attribute__ ((always_inline)) inline void
240new_inplace (Type &typemem, Ts &&... args)
241{
242 new (&typemem) Type (std::forward<Ts> (args)...);
243}
244
246template<class Type> __attribute__ ((always_inline)) inline void
247delete_inplace (Type &typemem)
248{
249 typemem.~Type();
250}
251
253template<class T> void
255{
256 delete o; // automatically handles nullptr
257}
258
260template<bool value> using REQUIRES = typename ::std::enable_if<value, bool>::type;
261
263template<bool value> using REQUIRESv = typename ::std::enable_if<value, void>::type;
264
269#define ASE_DEFINE_MAKE_SHARED(CLASS) \
270 struct MakeSharedAllocator_ : std::allocator<CLASS> { \
271 template<typename ...Args> \
272 static void construct (CLASS *p, Args &&...args) \
273 { ::new (p) CLASS (std::forward<Args> (args)...); } \
274 static void destroy (CLASS *p) \
275 { p->~CLASS (); } \
276 template<typename U> struct rebind { \
277 using other = \
278 std::conditional_t<std::is_same<U, CLASS>::value, \
279 MakeSharedAllocator_, std::allocator<U>>; \
280 }; \
281 }; \
282 template<typename ...Args> static std::shared_ptr<CLASS> \
283 make_shared (Args &&...args) \
284 { \
285 return std::allocate_shared<CLASS> (MakeSharedAllocator_(), \
286 std::forward<Args> (args)...); \
287 }
288
305template<class Target, class Source> std::shared_ptr<typename std::remove_pointer<Target>::type>
306shared_ptr_cast (Source *object)
307{
308 if (!object)
309 return nullptr;
310 // construct shared_ptr if possible
311 typedef decltype (object->shared_from_this()) ObjectP;
312 ObjectP sptr;
314 try {
315 sptr = object->shared_from_this();
316 } catch (const std::bad_weak_ptr&) {
317 return nullptr;
318 }
319 else // for non-pointers, allow bad_weak_ptr exceptions
320 sptr = object->shared_from_this();
321 // cast into target shared_ptr<> type
323}
325template<class Target, class Source> const std::shared_ptr<typename std::remove_pointer<Target>::type>
326shared_ptr_cast (const Source *object)
327{
328 return shared_ptr_cast<Target> (const_cast<Source*> (object));
329}
331template<class Target, class Source> std::shared_ptr<typename std::remove_pointer<Target>::type>
337template<class Target, class Source> const std::shared_ptr<typename std::remove_pointer<Target>::type>
339{
340 return shared_ptr_cast<Target> (const_cast<std::shared_ptr<Source>&> (sptr));
341}
342
345shared_ptr_from (Source *object)
346{
347 // ultimately calls shared_from_this()
348 return shared_ptr_cast<Source> (object);
349}
350
352template<class C> std::shared_ptr<C>
354{
355 std::shared_ptr<C> cptr = wptr.lock();
356 if (__builtin_expect (!!cptr, true))
357 return cptr; // fast path
358 std::shared_ptr<C> nptr = ctor();
359 { // C++20 has: std::atomic<std::weak_ptr<C>>::compare_exchange
360 static std::mutex mutex;
361 std::lock_guard<std::mutex> locker (mutex);
362 cptr = wptr.lock();
363 if (!cptr)
364 wptr = cptr = nptr;
365 }
366 return cptr;
367}
368
373template<class Class>
374class Persistent final {
375 static_assert (std::is_class<Class>::value, "Persistent<Class> requires class template argument");
376 uint64 mem_[(sizeof (Class) + sizeof (uint64) - 1) / sizeof (uint64)] = { 0, };
377 Class *ptr_ = nullptr;
378 void
379 initialize() ASE_NOINLINE
380 {
381 static std::mutex mtx;
383 if (ptr_ == nullptr)
384 ptr_ = new (mem_) Class(); // exclusive construction
385 }
386public:
388 constexpr Persistent () noexcept {}
390 explicit operator bool () const ASE_PURE { return ptr_ != nullptr; }
392 Class& operator* () ASE_PURE { return *operator->(); }
394 Class* operator-> () ASE_PURE
395 {
396 if (ASE_UNLIKELY (ptr_ == nullptr))
397 initialize();
398 return ptr_;
399 }
400};
401
403struct Id32 {
404 template<typename T,
405 REQUIRES< (sizeof (T) <= 4 && (std::is_integral<T>::value || std::is_enum<T>::value)) > = true>
406 constexpr
407 Id32 (T eid) :
408 id (uint32_t (eid))
409 {}
410 operator uint32_t () const noexcept { return id; }
411 bool operator== (int64_t i) const noexcept { return id == i; }
412 bool operator!= (int64_t i) const noexcept { return id != i; }
413 friend bool operator== (int64_t i, const Id32 &id) { return id.operator== (i); }
414 friend bool operator!= (int64_t i, const Id32 &id) { return id.operator!= (i); }
415 uint32_t id;
416};
417
418} // Ase
419
420#endif // __ASE_CXXAUX_HH__
constexpr Persistent() noexcept
A constexpr constructor avoids the static initialization order fiasco.
Definition cxxaux.hh:388
Class * operator->() __attribute__((__pure__))
Retrieve pointer to Class instance, always returns the same pointer.
Definition cxxaux.hh:394
Class & operator*() __attribute__((__pure__))
Retrieve reference to Class instance, always returns the same reference.
Definition cxxaux.hh:392
#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
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:254
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:122
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
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:353
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:260
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:110
void perror_die(const std::string &msg) noexcept
Issue a warning about an assertion error.
Definition cxxaux.cc:94
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:263
constexpr bool constexpr_equals(const char *a, const char *b, size_t n)
Test string equality at compile time.
Definition cxxaux.hh:233
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:345
uint32_t unichar
A 32-bit unsigned integer used for Unicode characters.
Definition cxxaux.hh:30
const char * string_demangle_cxx(const char *mangled_identifier) noexcept
Demangle identifier via libcc.
Definition cxxaux.cc:18
std::string backtrace_command()
Find GDB and construct command line.
Definition cxxaux.cc:44
bool assertion_failed_fatal
Global flag to force aborting on assertion warnings.
Definition cxxaux.cc:106
uint16_t uint16
A 16-bit unsigned integer.
Definition cxxaux.hh:23
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)
Definition cxxaux.hh:306
uint32_t uint
Provide 'uint' as convenience type.
Definition cxxaux.hh:18
T rotl(T... args)
typedef uint32_t
Helper class for integer IDs up to 32 Bit, possibly of enum type.
Definition cxxaux.hh:403
Common base type to allow casting between polymorphic classes.
Definition cxxaux.hh:214