From 887a9f559e71d8a788fbfe210f126eda5ba7969b Mon Sep 17 00:00:00 2001 From: Jonathan Bradley Date: Fri, 11 Oct 2024 18:41:47 -0400 Subject: initial commit: macros, memory, tests --- pkmacros.h | 216 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 pkmacros.h (limited to 'pkmacros.h') diff --git a/pkmacros.h b/pkmacros.h new file mode 100644 index 0000000..0bda595 --- /dev/null +++ b/pkmacros.h @@ -0,0 +1,216 @@ +#ifndef PK_MACROS_H +#define PK_MACROS_H + +#ifndef PK_LOG_OVERRIDE +# ifdef NDEBUG +# define PK_LOG_ERR(str) (void)str +# define PK_LOG_INF(str) (void)str +# define PK_LOGV_ERR(str, ...) (void)str +# define PK_LOGV_INF(str, ...) (void)str +# else +# define PK_LOG_ERR(str, ...) fprintf(stderr, str) +# define PK_LOG_INF(str, ...) fprintf(stdout, str) +# define PK_LOGV_ERR(str, ...) fprintf(stderr, str, __VA_ARGS__) +# define PK_LOGV_INF(str, ...) fprintf(stdout, str, __VA_ARGS__) +# endif +#endif + +#define PK_Q(x) #x +#define PK_QUOTE(x) PK_Q(x) +#define PK_CONCAT2(x, y) x##y +#define PK_CONCAT(x, y) PK_CONCAT2(x, y) + +#define PK_HAS_FLAG(val, flag) ((val & flag) == flag) +#define PK_CLAMP(val, min, max) (val < min ? min : val > max ? max : val) +#define PK_MIN(val, min) (val < min ? val : min) +#define PK_MAX(val, max) (val > max ? val : max) + +#define PK_TO_BIN_PAT PK_Q(%c%c%c%c%c%c%c%c) +#define PK_TO_BIN_PAT_8 PK_TO_BIN_PAT +#define PK_TO_BIN_PAT_16 PK_TO_BIN_PAT PK_TO_BIN_PAT +#define PK_TO_BIN_PAT_32 PK_TO_BIN_PAT_16 PK_TO_BIN_PAT_16 +#define PK_TO_BIN_PAT_64 PK_TO_BIN_PAT_32 PK_TO_BIN_PAT_32 +#define PK_TO_BIN(byte) \ + ((byte) & 0x80 ? '1' : '0'), \ + ((byte) & 0x40 ? '1' : '0'), \ + ((byte) & 0x20 ? '1' : '0'), \ + ((byte) & 0x10 ? '1' : '0'), \ + ((byte) & 0x08 ? '1' : '0'), \ + ((byte) & 0x04 ? '1' : '0'), \ + ((byte) & 0x02 ? '1' : '0'), \ + ((byte) & 0x01 ? '1' : '0') +#define PK_TO_BIN_8(u8) PK_TO_BIN(u8) +#define PK_TO_BIN_16(u16) PK_TO_BIN((u16 >> 8)), PK_TO_BIN(u16 & 0x00FF) +#define PK_TO_BIN_32(u32) PK_TO_BIN_16((u32 >> 16)), PK_TO_BIN_16(u32 & 0x0000FFFF) +#define PK_TO_BIN_64(u64) PK_TO_BIN_32((u64 >> 32)), PK_TO_BIN_32(u64 & 0x00000000FFFFFFFF) + +#if defined(__cplusplus) +# define CAFE_BABE(T) reinterpret_cast(0xCAFEBABE) +#else +# define CAFE_BABE(T) (T *)(0xCAFEBABE) +#endif + +#define NULL_CHAR_ARR(v, len) char v[len]; v[0] = '\0'; v[len-1] = '\0'; + +#define IS_CONSTRUCTIBLE(T) constexpr(std::is_default_constructible::value && !std::is_integral::value && !std::is_floating_point::value) +#define IS_DESTRUCTIBLE(T) constexpr(std::is_destructible::value && !std::is_integral::value && !std::is_floating_point::value && !std::is_array::value) + +#define TypeSafeInt2_H(TypeName, Type, Max, TypeName_T, TypeName_MAX, TypeName_T_MAX) \ + using TypeName_T = Type; \ + enum class TypeName : TypeName_T; \ + constexpr TypeName_T TypeName_T_MAX = TypeName_T{Max}; \ + constexpr TypeName TypeName_MAX = TypeName{TypeName_T_MAX}; \ + TypeName operator+(const TypeName& a, const TypeName& b); \ + TypeName operator-(const TypeName& a, const TypeName& b); \ + TypeName operator&(const TypeName& a, const TypeName& b); \ + TypeName operator|(const TypeName& a, const TypeName& b); \ + TypeName operator^(const TypeName& a, const TypeName& b); \ + TypeName& operator++(TypeName& a); \ + TypeName& operator--(TypeName& a); \ + TypeName operator++(TypeName& a, int); \ + TypeName operator--(TypeName& a, int); \ + TypeName operator<<(const TypeName& a, const TypeName& b); \ + TypeName operator>>(const TypeName& a, const TypeName& b); \ + TypeName operator+=(TypeName& a, const TypeName& b); \ + TypeName operator-=(TypeName& a, const TypeName& b); \ + TypeName operator&=(TypeName& a, const TypeName& b); \ + TypeName operator|=(TypeName& a, const TypeName& b); \ + TypeName operator^=(TypeName& a, const TypeName& b); \ + TypeName operator~(TypeName& a); +#define TypeSafeInt2_B(TypeName, TypeName_T) \ + inline TypeName operator+(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) + static_cast(b)); \ + } \ + inline TypeName operator-(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) - static_cast(b)); \ + } \ + inline TypeName operator&(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) & static_cast(b)); \ + } \ + inline TypeName operator|(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) | static_cast(b)); \ + } \ + inline TypeName operator^(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) ^ static_cast(b)); \ + } \ + inline TypeName& operator++(TypeName& a) { \ + a = a + TypeName{1}; \ + return a; \ + } \ + inline TypeName& operator--(TypeName& a) { \ + a = a - TypeName{1}; \ + return a; \ + }; \ + inline TypeName operator++(TypeName& a, int) { \ + a = a + TypeName{1}; \ + return a; \ + } \ + inline TypeName operator--(TypeName& a, int) { \ + a = a - TypeName{1}; \ + return a; \ + }; \ + inline TypeName operator<<(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) << static_cast(b)); \ + }; \ + inline TypeName operator>>(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) >> static_cast(b)); \ + }; \ + inline TypeName operator+=(TypeName& a, const TypeName& b) { \ + a = TypeName{a + b}; \ + return a; \ + }; \ + inline TypeName operator-=(TypeName& a, const TypeName& b) { \ + a = TypeName{a - b}; \ + return a; \ + }; \ + inline TypeName operator&=(TypeName& a, const TypeName& b) { \ + a = TypeName{a & b}; \ + return a; \ + }; \ + inline TypeName operator|=(TypeName& a, const TypeName& b) { \ + a = TypeName{a | b}; \ + return a; \ + }; \ + inline TypeName operator^=(TypeName& a, const TypeName& b) { \ + a = TypeName{a ^ b}; \ + return a; \ + }; \ + inline TypeName operator~(TypeName& a) { \ + a = static_cast(~static_cast(a)); \ + return a; \ + }; +#define TypeSafeInt_H(TypeName, Type, Max) \ + TypeSafeInt2_H(TypeName, Type, Max, PK_CONCAT(TypeName, _T), PK_CONCAT(TypeName, _MAX), PK_CONCAT(TypeName, _T_MAX)) +#define TypeSafeInt_B(TypeName) \ + TypeSafeInt2_B(TypeName, PK_CONCAT(TypeName, _T)) + +#define TypeSafeInt2_H_constexpr(TypeName, Type, Max, TypeName_T, TypeName_MAX, TypeName_T_MAX) \ + using TypeName_T = Type; \ + enum class TypeName : TypeName_T; \ + constexpr TypeName_T TypeName_T_MAX = TypeName_T{Max}; \ + constexpr TypeName TypeName_MAX = TypeName{TypeName_T_MAX}; \ + constexpr TypeName operator+(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) + static_cast(b)); \ + } \ + constexpr TypeName operator-(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) - static_cast(b)); \ + } \ + constexpr TypeName operator&(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) & static_cast(b)); \ + } \ + constexpr TypeName operator|(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) | static_cast(b)); \ + } \ + constexpr TypeName operator^(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) ^ static_cast(b)); \ + } \ + constexpr TypeName& operator++(TypeName& a) { \ + a = a + TypeName{1}; \ + return a; \ + } \ + constexpr TypeName& operator--(TypeName& a) { \ + a = a - TypeName{1}; \ + return a; \ + }; \ + constexpr TypeName operator++(TypeName& a, int) { \ + a = a + TypeName{1}; \ + return a; \ + } \ + constexpr TypeName operator--(TypeName& a, int) { \ + a = a - TypeName{1}; \ + return a; \ + }; \ + constexpr TypeName operator<<(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) << static_cast(b)); \ + }; \ + constexpr TypeName operator>>(const TypeName& a, const TypeName& b) { \ + return TypeName(static_cast(a) >> static_cast(b)); \ + }; \ + constexpr TypeName operator+=(TypeName& a, const TypeName& b) { \ + a = TypeName{a + b}; \ + return a; \ + }; \ + constexpr TypeName operator-=(TypeName& a, const TypeName& b) { \ + a = TypeName{a - b}; \ + return a; \ + }; \ + constexpr TypeName operator&=(TypeName& a, const TypeName& b) { \ + a = TypeName{a & b}; \ + return a; \ + }; \ + constexpr TypeName operator|=(TypeName& a, const TypeName& b) { \ + a = TypeName{a | b}; \ + return a; \ + }; \ + constexpr TypeName operator^=(TypeName& a, const TypeName& b) { \ + a = TypeName{a ^ b}; \ + return a; \ + }; \ + constexpr TypeName operator~(const TypeName& a) { \ + return static_cast(~static_cast(a)); \ + }; +#define TypeSafeInt_constexpr(TypeName, Type, Max) \ + TypeSafeInt2_H_constexpr(TypeName, Type, Max, PK_CONCAT(TypeName, _T), PK_CONCAT(TypeName, _MAX), PK_CONCAT(TypeName, _T_MAX)) + +#endif /* PK_MACROS_H */ +// vim: ts=2:sw=2:expandtab -- cgit v1.2.3