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
|
#ifndef PKE_MACROS_HPP
#define PKE_MACROS_HPP
#define Q(x) #x
#define QUOTE(x) Q(x)
#define CONCAT2(x, y) x##y
#define CONCAT(x, y) CONCAT2(x, y)
#define CAFE_BABE(T) reinterpret_cast<T *>(0xCAFEBABE)
#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_T &i); \
TypeName operator-(const TypeName &a, const TypeName_T &i); \
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<<(const TypeName &a, const TypeName &b); \
TypeName operator>>(const TypeName &a, const TypeName &b);
#define TypeSafeInt2_B(TypeName, TypeName_T) \
inline TypeName operator+(const TypeName &a, const TypeName_T &i) { \
return TypeName(static_cast<TypeName_T>(a) + i); \
} \
inline TypeName operator-(const TypeName &a, const TypeName_T &i) { \
return TypeName(static_cast<TypeName_T>(a) - i); \
} \
inline TypeName operator+(const TypeName &a, const TypeName &b) { \
return TypeName(static_cast<TypeName_T>(a) + static_cast<TypeName_T>(b)); \
} \
inline TypeName operator-(const TypeName &a, const TypeName &b) { \
return TypeName(static_cast<TypeName_T>(a) - static_cast<TypeName_T>(b)); \
} \
inline TypeName operator&(const TypeName &a, const TypeName &b) { \
return TypeName(static_cast<TypeName_T>(a) & static_cast<TypeName_T>(b)); \
} \
inline TypeName operator|(const TypeName &a, const TypeName &b) { \
return TypeName(static_cast<TypeName_T>(a) | static_cast<TypeName_T>(b)); \
} \
inline TypeName operator^(const TypeName &a, const TypeName &b) { \
return TypeName(static_cast<TypeName_T>(a) ^ static_cast<TypeName_T>(b)); \
} \
inline TypeName &operator++(TypeName &a) { \
a = a + 1; \
return a; \
} \
inline TypeName &operator--(TypeName &a) { \
a = a - 1; \
return a; \
}; \
inline TypeName operator<<(const TypeName &a, const TypeName &b) { \
return TypeName(static_cast<TypeName_T>(a) << static_cast<TypeName_T>(b)); \
}; \
inline TypeName operator>>(const TypeName &a, const TypeName &b) { \
return TypeName(static_cast<TypeName_T>(a) >> static_cast<TypeName_T>(b)); \
};
#define TypeSafeInt_H(TypeName, Type, Max) \
TypeSafeInt2_H(TypeName, Type, Max, CONCAT(TypeName, _T), CONCAT(TypeName, _MAX), CONCAT(TypeName, _T_MAX))
#define TypeSafeInt_B(TypeName) \
TypeSafeInt2_B(TypeName, 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_T &b) { \
return TypeName(static_cast<TypeName_T>(a) + static_cast<TypeName_T>(b)); \
} \
constexpr TypeName operator-(const TypeName &a, const TypeName_T &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<<(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)); \
};
#define TypeSafeInt_Const_Expr(TypeName, Type, Max) \
TypeSafeInt2_H_constexpr(TypeName, Type, Max, CONCAT(TypeName, _T), CONCAT(TypeName, _MAX), CONCAT(TypeName, _T_MAX))
#endif /* PKE_MACROS_HPP */
|