Anklang-0.3.0.dev595+g65331842 anklang-0.3.0.dev595+g65331842
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
234extern inline constexpr bool
235constexpr_equals (const char *a, const char *b, size_t n)
236{
237 return n == 0 || (a[0] == b[0] && (a[0] == 0 || constexpr_equals (a + 1, b + 1, n - 1)));
238}
239
241template<class Type, class ...Ts> ASE_ALWAYS_INLINE inline void
242new_inplace (Type &typemem, Ts &&... args)
243{
244 new (&typemem) Type (std::forward<Ts> (args)...);
245}
246
248template<class Type> ASE_ALWAYS_INLINE inline void
249delete_inplace (Type &typemem)
250{
251 typemem.~Type();
252}
253
255template<class T> void
257{
258 delete o; // automatically handles nullptr
259}
260
262template<bool value> using REQUIRES = typename ::std::enable_if<value, bool>::type;
263
265template<bool value> using REQUIRESv = typename ::std::enable_if<value, void>::type;
266
271#define ASE_DEFINE_MAKE_SHARED(CLASS) \
272 struct MakeSharedAllocator_ : std::allocator<CLASS> { \
273 template<typename ...Args> \
274 static void construct (CLASS *p, Args &&...args) \
275 { ::new (p) CLASS (std::forward<Args> (args)...); } \
276 static void destroy (CLASS *p) \
277 { p->~CLASS (); } \
278 template<typename U> struct rebind { \
279 using other = \
280 std::conditional_t<std::is_same<U, CLASS>::value, \
281 MakeSharedAllocator_, std::allocator<U>>; \
282 }; \
283 }; \
284 template<typename ...Args> static std::shared_ptr<CLASS> \
285 make_shared (Args &&...args) \
286 { \
287 return std::allocate_shared<CLASS> (MakeSharedAllocator_(), \
288 std::forward<Args> (args)...); \
289 }
290
307template<class Target, class Source> std::shared_ptr<typename std::remove_pointer<Target>::type>
308shared_ptr_cast (Source *object)
309{
310 if (!object)
311 return nullptr;
312 // construct shared_ptr if possible
313 typedef decltype (object->shared_from_this()) ObjectP;
314 ObjectP sptr;
316 try {
317 sptr = object->shared_from_this();
318 } catch (const std::bad_weak_ptr&) {
319 return nullptr;
320 }
321 else // for non-pointers, allow bad_weak_ptr exceptions
322 sptr = object->shared_from_this();
323 // cast into target shared_ptr<> type
325}
327template<class Target, class Source> const std::shared_ptr<typename std::remove_pointer<Target>::type>
328shared_ptr_cast (const Source *object)
329{
330 return shared_ptr_cast<Target> (const_cast<Source*> (object));
331}
333template<class Target, class Source> std::shared_ptr<typename std::remove_pointer<Target>::type>
339template<class Target, class Source> const std::shared_ptr<typename std::remove_pointer<Target>::type>
341{
342 return shared_ptr_cast<Target> (const_cast<std::shared_ptr<Source>&> (sptr));
343}
344
347shared_ptr_from (Source *object)
348{
349 // ultimately calls shared_from_this()
350 return shared_ptr_cast<Source> (object);
351}
352
354template<class C> std::shared_ptr<C>
356{
357 std::shared_ptr<C> cptr = wptr.lock();
358 if (__builtin_expect (!!cptr, true))
359 return cptr; // fast path
360 std::shared_ptr<C> nptr = ctor();
361 { // C++20 has: std::atomic<std::weak_ptr<C>>::compare_exchange
362 static std::mutex mutex;
363 std::lock_guard<std::mutex> locker (mutex);
364 cptr = wptr.lock();
365 if (!cptr)
366 wptr = cptr = nptr;
367 }
368 return cptr;
369}
370
375template<class Class>
376class Persistent final {
377 static_assert (std::is_class<Class>::value, "Persistent<Class> requires class template argument");
378 uint64 mem_[(sizeof (Class) + sizeof (uint64) - 1) / sizeof (uint64)] = { 0, };
379 Class *ptr_ = nullptr;
380 void
381 initialize() ASE_NOINLINE
382 {
383 static std::mutex mtx;
385 if (ptr_ == nullptr)
386 ptr_ = new (mem_) Class(); // exclusive construction
387 }
388public:
390 constexpr Persistent () noexcept {}
392 explicit operator bool () const ASE_PURE { return ptr_ != nullptr; }
394 Class& operator* () ASE_PURE { return *operator->(); }
396 Class* operator-> () ASE_PURE
397 {
398 if (ASE_UNLIKELY (ptr_ == nullptr))
399 initialize();
400 return ptr_;
401 }
402};
403
405struct Id32 {
406 template<typename T,
407 REQUIRES< (sizeof (T) <= 4 && (std::is_integral<T>::value || std::is_enum<T>::value)) > = true>
408 constexpr
409 Id32 (T eid) :
410 id (uint32_t (eid))
411 {}
412 operator uint32_t () const noexcept { return id; }
413 bool operator== (int64_t i) const noexcept { return id == i; }
414 bool operator!= (int64_t i) const noexcept { return id != i; }
415 friend bool operator== (int64_t i, const Id32 &id) { return id.operator== (i); }
416 friend bool operator!= (int64_t i, const Id32 &id) { return id.operator!= (i); }
417 uint32_t id;
418};
419
420} // Ase
421
422#endif // __ASE_CXXAUX_HH__
Create an instance of Class on demand that is constructed and never destructed.
Definition cxxaux.hh:376
constexpr Persistent() noexcept
A constexpr constructor avoids the static initialization order fiasco.
Definition cxxaux.hh:390
Class * operator->() __attribute__((__pure__))
Retrieve pointer to Class instance, always returns the same pointer.
Definition cxxaux.hh:396
Class & operator*() __attribute__((__pure__))
Retrieve reference to Class instance, always returns the same reference.
Definition cxxaux.hh:394
#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:249
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:256
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:77
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:19
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:355
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:262
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:70
void perror_die(const std::string &msg) noexcept
Issue a warning about an assertion error.
Definition cxxaux.cc:58
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:265
constexpr bool constexpr_equals(const char *a, const char *b, size_t n)
Test string equality at compile time.
Definition cxxaux.hh:235
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:347
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:242
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:308
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:405
Common base type to allow casting between polymorphic classes.
Definition cxxaux.hh:220