2
3
4
5
6
7
9#ifndef GIMBAL_MACRO_UTILS_H
10#define GIMBAL_MACRO_UTILS_H
14#include <gimbal/core/gimbal_config.h>
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)))
27#define GBL_BIT_MASK_2(bits, offset) (((1u
<< bits) - 1u
) << offset)
31#define GBL_BCD_BYTE_PACK(n) (((n / 10
) << 4
) | (n % 10
))
32#define GBL_BCD_BYTE_UNPACK(n) (unsigned)(((n >> 4
) * 10
) + (n & 0xf
))
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
]))
38#define GBL_PTR_OFFSET_3(type, ptr, bytes) ((type)(((uintptr_t)ptr) + bytes))
41#define GBL_SWITCH_CASE_STRINGIFY(s) case s: return #s
43#define GBL_LABEL_EMPTY(name) name: {;}
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));
54#define GBL_ASSERT(...)
60# define GBL_META_GENERIC_MACRO_GENERATE(traits, 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)
67# define GBL_META_GENERIC_MACRO_GENERATE(traits, X)
69 using GenericType = decltype(X);
71 GBL_MAP_TUPLES(GBL_META_GENERIC_MACRO_TRAIT_OVERLOADS_DECLARE_CASE_CPP_
,
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)...);
79#define GBL_META_GENERIC_MACRO_TRAIT_PROPERTY_DEFAULT(defaultFunc, overloads) defaultFunc
80#define GBL_META_GENERIC_MACRO_NO_DEFAULT GBL_NULL
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
88#define GBL_META_GENERIC_MACRO_TRAIT_OVERLOADS_DECLARE_CASE_C_(type, function)
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)...);
#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_PTR_OFFSET(...)
#define GBL_BIT_MASK_2(bits, offset)
#define GBL_BIT_MASK(...)
#define GBL_PTR_OFFSET_3(type, ptr, bytes)