#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_CLR_RESET "\033[0m" #define PK_CLR_FG_BLACK "\033[30m" #define PK_CLR_FG_RED "\033[31m" #define PK_CLR_FG_GREEN "\033[32m" #define PK_CLR_FG_YELLOW "\033[33m" #define PK_CLR_FG_BLUE "\033[34m" #define PK_CLR_FG_MAGENTA "\033[35m" #define PK_CLR_FG_CYAN "\033[36m" #define PK_CLR_FG_WHITE "\033[37m" #define PK_CLR_BG_BLACK "\033[40m" #define PK_CLR_BG_RED "\033[41m" #define PK_CLR_BG_GREEN "\033[42m" #define PK_CLR_BG_YELLOW "\033[43m" #define PK_CLR_BG_BLUE "\033[44m" #define PK_CLR_BG_MAGENTA "\033[45m" #define PK_CLR_BG_CYAN "\033[46m" #define PK_CLR_BG_WHITE "\033[47m" #define PK_CLR_FG_BRIGHT_BLACK "\033[90m" #define PK_CLR_FG_BRIGHT_RED "\033[91m" #define PK_CLR_FG_BRIGHT_GREEN "\033[92m" #define PK_CLR_FG_BRIGHT_YELLOW "\033[93m" #define PK_CLR_FG_BRIGHT_BLUE "\033[94m" #define PK_CLR_FG_BRIGHT_MAGENTA "\033[95m" #define PK_CLR_FG_BRIGHT_CYAN "\033[96m" #define PK_CLR_FG_BRIGHT_WHITE "\033[97m" #define PK_CLR_BG_BRIGHT_BLACK "\033[100m" #define PK_CLR_BG_BRIGHT_RED "\033[101m" #define PK_CLR_BG_BRIGHT_GREEN "\033[102m" #define PK_CLR_BG_BRIGHT_YELLOW "\033[103m" #define PK_CLR_BG_BRIGHT_BLUE "\033[104m" #define PK_CLR_BG_BRIGHT_MAGENTA "\033[105m" #define PK_CLR_BG_BRIGHT_CYAN "\033[106m" #define PK_CLR_BG_BRIGHT_WHITE "\033[107m" #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|(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, const TypeName& b); \ TypeName operator^=(TypeName& a, const TypeName& b); \ TypeName operator~(TypeName& a); #define TypeSafeInt2_B(TypeName, TypeName_T) \ TypeName operator+(const TypeName& a, const TypeName& b) { \ return TypeName(static_cast(a) + static_cast(b)); \ } \ TypeName operator-(const TypeName& a, const TypeName& b) { \ return TypeName(static_cast(a) - static_cast(b)); \ } \ TypeName operator*(const TypeName& a, const TypeName& b) { \ return TypeName(static_cast(a) * static_cast(b)); \ } \ TypeName operator/(const TypeName& a, const TypeName& b) { \ return TypeName(static_cast(a) / static_cast(b)); \ } \ TypeName operator&(const TypeName& a, const TypeName& b) { \ return TypeName(static_cast(a) & static_cast(b)); \ } \ TypeName operator|(const TypeName& a, const TypeName& b) { \ return TypeName(static_cast(a) | static_cast(b)); \ } \ TypeName operator^(const TypeName& a, const TypeName& b) { \ return TypeName(static_cast(a) ^ static_cast(b)); \ } \ TypeName& operator++(TypeName& a) { \ a = a + TypeName{1}; \ return a; \ } \ TypeName& operator--(TypeName& a) { \ a = a - TypeName{1}; \ return a; \ }; \ TypeName operator++(TypeName& a, int) { \ a = a + TypeName{1}; \ return a; \ } \ TypeName operator--(TypeName& a, int) { \ a = a - TypeName{1}; \ return a; \ }; \ TypeName operator<<(const TypeName& a, const TypeName& b) { \ return TypeName(static_cast(a) << static_cast(b)); \ }; \ TypeName operator>>(const TypeName& a, const TypeName& b) { \ return TypeName(static_cast(a) >> static_cast(b)); \ }; \ TypeName operator+=(TypeName& a, const TypeName& b) { \ a = TypeName{a + b}; \ return a; \ }; \ TypeName operator-=(TypeName& a, const TypeName& b) { \ a = TypeName{a - b}; \ return a; \ }; \ TypeName operator*=(TypeName& a, const TypeName& b) { \ a = TypeName{a * b}; \ return a; \ }; \ TypeName operator/=(TypeName& a, const TypeName& b) { \ a = TypeName{a / b}; \ return a; \ }; \ TypeName operator&=(TypeName& a, const TypeName& b) { \ a = TypeName{a & b}; \ return a; \ }; \ TypeName operator|=(TypeName& a, const TypeName& b) { \ a = TypeName{a | b}; \ return a; \ }; \ TypeName operator^=(TypeName& a, const TypeName& b) { \ a = TypeName{a ^ b}; \ return a; \ }; \ TypeName operator~(TypeName& a) { \ TypeName_T b{static_cast(a)}; \ return TypeName{static_cast(~b)}; \ }; #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|(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|=(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) { \ TypeName_T b{static_cast(a)}; \ return TypeName{static_cast(~b)}; \ }; #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