summaryrefslogtreecommitdiff
path: root/pkmacros.h
blob: 52fc188ff3412b7e5864f84af12bac26a6612287 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#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<T *>(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<T>::value && !std::is_integral<T>::value && !std::is_floating_point<T>::value)
#define IS_DESTRUCTIBLE(T) constexpr(std::is_destructible<T>::value && !std::is_integral<T>::value && !std::is_floating_point<T>::value && !std::is_array<T>::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<TypeName_T>(a) + static_cast<TypeName_T>(b));                      \
  }                                                                                                \
  TypeName operator-(const TypeName& a, const TypeName& b) {                                       \
    return TypeName(static_cast<TypeName_T>(a) - static_cast<TypeName_T>(b));                      \
  }                                                                                                \
  TypeName operator*(const TypeName& a, const TypeName& b) {                                       \
    return TypeName(static_cast<TypeName_T>(a) * static_cast<TypeName_T>(b));                      \
  }                                                                                                \
  TypeName operator/(const TypeName& a, const TypeName& b) {                                       \
    return TypeName(static_cast<TypeName_T>(a) / static_cast<TypeName_T>(b));                      \
  }                                                                                                \
  TypeName operator&(const TypeName& a, const TypeName& b) {                                       \
    return TypeName(static_cast<TypeName_T>(a) & static_cast<TypeName_T>(b));                      \
  }                                                                                                \
  TypeName operator|(const TypeName& a, const TypeName& b) {                                       \
    return TypeName(static_cast<TypeName_T>(a) | static_cast<TypeName_T>(b));                      \
  }                                                                                                \
  TypeName operator^(const TypeName& a, const TypeName& b) {                                       \
    return TypeName(static_cast<TypeName_T>(a) ^ static_cast<TypeName_T>(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<TypeName_T>(a) << static_cast<TypeName_T>(b));                     \
  };                                                                                               \
  TypeName operator>>(const TypeName& a, const TypeName& b) {                                      \
    return TypeName(static_cast<TypeName_T>(a) >> static_cast<TypeName_T>(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<TypeName_T>(a)};                                                      \
    return TypeName{static_cast<TypeName_T>(~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<TypeName_T>(a) + static_cast<TypeName_T>(b));                      \
  }                                                                                                \
  constexpr TypeName operator-(const TypeName& a, const TypeName& b) {                             \
    return TypeName(static_cast<TypeName_T>(a) - static_cast<TypeName_T>(b));                      \
  }                                                                                                \
  constexpr TypeName operator*(const TypeName& a, const TypeName& b) {                             \
    return TypeName(static_cast<TypeName_T>(a) * static_cast<TypeName_T>(b));                      \
  }                                                                                                \
  constexpr TypeName operator/(const TypeName& a, const TypeName& b) {                             \
    return TypeName(static_cast<TypeName_T>(a) / static_cast<TypeName_T>(b));                      \
  }                                                                                                \
  constexpr TypeName operator&(const TypeName& a, const TypeName& b) {                             \
    return TypeName(static_cast<TypeName_T>(a) & static_cast<TypeName_T>(b));                      \
  }                                                                                                \
  constexpr TypeName operator|(const TypeName& a, const TypeName& b) {                             \
    return TypeName(static_cast<TypeName_T>(a) | static_cast<TypeName_T>(b));                      \
  }                                                                                                \
  constexpr TypeName operator^(const TypeName& a, const TypeName& b) {                             \
    return TypeName(static_cast<TypeName_T>(a) ^ static_cast<TypeName_T>(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<TypeName_T>(a) << static_cast<TypeName_T>(b));                     \
  };                                                                                               \
  constexpr TypeName operator>>(const TypeName& a, const TypeName& b) {                            \
    return TypeName(static_cast<TypeName_T>(a) >> static_cast<TypeName_T>(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<TypeName_T>(a)};                                                      \
    return TypeName{static_cast<TypeName_T>(~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