libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_macro_utils.h
Go to the documentation of this file.
1/*! \file
2 * \brief Miscellaneous macro utilities and meta generators
3 * \ingroup preprocessor
4 * \sa gimbal_macro_composition.h, gimbal_macro_sequences.h
5 *
6 * \author Falco Girgis
7 */
8
9#ifndef GIMBAL_MACRO_UTILS_H
10#define GIMBAL_MACRO_UTILS_H
11
14#include <gimbal/core/gimbal_config.h>
15#include <stdint.h>
16
17#ifdef __cplusplus
18#include <functional>
19extern "C" {
20#endif
21
22#define GBL_MIN(a, b) (((a) < (b))? (a) : (b))
23#define GBL_MAX(a, b) (((a) > (b))? (a) : (b))
24#define GBL_CEIL(numerator, denominator) ((numerator / denominator + (numerator % denominator != 0)))
25#define GBL_CLAMP(n, min, max) (((n) > (max)) ? (max) : (((n) < (min)) ? (min) : (n)))
26
27#define GBL_BIT_MASK_2(bits, offset) (((1u << bits) - 1u) << offset)
28#define GBL_BIT_MASK_1(bits) GBL_BIT_MASK_2(bits, 0)
29#define GBL_BIT_MASK(...) GBL_VA_OVERLOAD_CALL_ARGC(GBL_BIT_MASK, __VA_ARGS__)
30
31#define GBL_BCD_BYTE_PACK(n) (((n / 10) << 4) | (n % 10)) // 0-99
32#define GBL_BCD_BYTE_UNPACK(n) (unsigned)(((n >> 4) * 10) + (n & 0xf))
33
34#define GBL_CONTAINER_OF(ptr, type, member) ((type*)((char*)(ptr) - offsetof(type, member)))
35#define GBL_COUNT_OF(array) (sizeof(array) / sizeof(array[0]))
36
37#define GBL_PTR_OFFSET(...) GBL_VA_OVERLOAD_CALL_ARGC(GBL_PTR_OFFSET, __VA_ARGS__)
38#define GBL_PTR_OFFSET_3(type, ptr, bytes) ((type)(((uintptr_t)ptr) + bytes))
39#define GBL_PTR_OFFSET_2(ptr, bytes) GBL_PTR_OFFSET_3(void*, ptr, bytes)
40
41#define GBL_SWITCH_CASE_STRINGIFY(s) case s: return #s
42
43#define GBL_LABEL_EMPTY(name) name: {;}
44
45#define GBL_SWAP(x,y)
47 unsigned char swap_temp[sizeof(x) == sizeof(y) ?
48 (signed)sizeof(x) : -1];
49 memcpy(swap_temp, &y, sizeof(x));
50 memcpy(&y, &x, sizeof(x));
51 memcpy(&x, swap_temp, sizeof(x));
53
54#define GBL_ASSERT(...)
56
57
58
59#ifndef __cplusplus
60# define GBL_META_GENERIC_MACRO_GENERATE(traits, X)
61 _Generic((X),
62 GBL_MAP_TUPLES(GBL_META_GENERIC_MACRO_TRAIT_OVERLOADS_DECLARE_CASE_C_,
63 GBL_MAP_TUPLES(GBL_EVAL, GBL_META_GENERIC_MACRO_TRAIT_PROPERTY_OVERLOADS_ traits))
64 default: GBL_EVAL(GBL_META_GENERIC_MACRO_TRAIT_PROPERTY_DEFAULT traits)
65 )
66#else
67# define GBL_META_GENERIC_MACRO_GENERATE(traits, X)
68 [](auto&&... args) {
69 using GenericType = decltype(X);
70 if constexpr(false);
71 GBL_MAP_TUPLES(GBL_META_GENERIC_MACRO_TRAIT_OVERLOADS_DECLARE_CASE_CPP_,
72 GBL_MAP_TUPLES(GBL_EVAL, GBL_META_GENERIC_MACRO_TRAIT_PROPERTY_OVERLOADS_ traits))
73 else if constexpr(requires { GBL_EVAL(GBL_META_GENERIC_MACRO_TRAIT_PROPERTY_DEFAULT_ traits)
74 (std::forward<decltype(args)>(args)...); })
75 return GBL_EVAL(GBL_META_GENERIC_MACRO_TRAIT_PROPERTY_DEFAULT_ traits)(std::forward<decltype(args)>(args)...);
76 }
77#endif
78
79#define GBL_META_GENERIC_MACRO_TRAIT_PROPERTY_DEFAULT(defaultFunc, overloads) defaultFunc
80#define GBL_META_GENERIC_MACRO_NO_DEFAULT GBL_NULL
81
82// ===== Implementation ======
83///\cond
84#define GBL_META_GENERIC_MACRO_TRAIT_PROPERTY_OVERLOADS_(defaultFunc, overloads) overloads
85#define GBL_META_GENERIC_MACRO_TRAIT_OVERLOADS_PROPERTY_TYPE_(type, function) type
86#define GBL_META_GENERIC_MACRO_TRAIT_OVERLOADS_PROPERTY_FUNCTION_(type, function) function
87
88#define GBL_META_GENERIC_MACRO_TRAIT_OVERLOADS_DECLARE_CASE_C_(type, function)
89 type: function,
90
91#define GBL_META_GENERIC_MACRO_TRAIT_OVERLOADS_DECLARE_CASE_CPP_(type, function)
92 else if constexpr(std::same_as<GenericType, type>) return function(std::forward<decltype(args)>(args)...);
93///\endcond
94#ifdef __cplusplus
95}
96#endif
97
98#endif // GIMBAL_MACRO_UTILS_H
#define GBL_STMT_START
#define GBL_NULL
#define GBL_STMT_END
#define GBL_VA_OVERLOAD_SELECT(BASE, SUFFIXER,...)
#define GBL_VA_OVERLOAD_SUFFIXER_ARGC(...)
#define GBL_VA_OVERLOAD_CALL_ARGC(BASE,...)
#define GBL_MAP_TUPLES(f,...)
#define GBL_EVAL(...)
#define GBL_PTR_OFFSET(...)
#define GBL_ASSERT(...)
#define GBL_BIT_MASK_2(bits, offset)
#define GBL_BIT_MASK(...)
#define GBL_PTR_OFFSET_3(type, ptr, bytes)