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
|
#ifndef TMWA_INTS_UDL_HPP
#define TMWA_INTS_UDL_HPP
// udl.hpp - user-defined literals for integers.
//
// Copyright © 2014 Ben Longbons <b.r.longbons@gmail.com>
//
// This file is part of The Mana World (Athena server)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
# include "fwd.hpp"
# include <cstdint>
# include <type_traits>
namespace ints
{
namespace
{
typedef unsigned long long ullong;
template<char C>
struct CharParser
{
constexpr static
bool is_dec = '0' <= C && C <= '9';
constexpr static
bool is_upper = 'A' <= C && C <= 'F';
constexpr static
bool is_lower = 'a' <= C && C <= 'f';
static_assert(is_dec || is_upper || is_lower, "char base");
constexpr static
ullong value = is_upper ? (C - 'A' + 10) : is_lower ? (C - 'a' + 10) : C - '0';
};
template<ullong base, ullong accum, char... C>
struct BaseParser;
template<ullong base, ullong accum, char F, char... R>
struct BaseParser<base, accum, F, R...>
{
constexpr static
ullong mulled = accum * base;
constexpr static
ullong add = CharParser<F>::value;
static_assert(add < base, "parse base");
constexpr static
ullong added = mulled + add;
static_assert(added > accum || accum == 0, "parse overflow");
constexpr static
ullong value = BaseParser<base, added, R...>::value;
};
template<ullong base, ullong accum>
struct BaseParser<base, accum>
{
constexpr static
ullong value = accum;
};
template<char... C>
struct IntParser
{
constexpr static
ullong value = BaseParser<10, 0, C...>::value;
};
template<char... C>
struct IntParser<'0', C...>
{
constexpr static
ullong value = BaseParser<8, 0, C...>::value;
};
template<char... C>
struct IntParser<'0', 'x', C...>
{
constexpr static
ullong value = BaseParser<16, 0, C...>::value;
};
template<char... C>
struct IntParser<'0', 'X', C...>
{
constexpr static
ullong value = BaseParser<16, 0, C...>::value;
};
template<char... C>
struct IntParser<'0', 'b', C...>
{
constexpr static
ullong value = BaseParser<2, 0, C...>::value;
};
template<char... C>
struct IntParser<'0', 'B', C...>
{
constexpr static
ullong value = BaseParser<2, 0, C...>::value;
};
template<bool S, ullong V>
struct SignedMagnitudeConstant
{
static constexpr
bool sign = S;
static constexpr
ullong magnitude = V;
template<class T>
constexpr
operator T() const
{
typedef typename std::make_unsigned<T>::type U;
// boo, body of constexpr function can't use variables
# define is_signed bool(T(-1) < T(0))
static_assert(is_signed >= (sign && magnitude), "signed");
# define max ullong(ullong(U(-1) >> is_signed))
static_assert(magnitude <= max || (sign && magnitude == max + 1), "magna");
# undef is_signed
# undef max
return sign ? T(ullong(-magnitude)) : T(magnitude);
}
};
template<bool S1, ullong V1, bool S2, ullong V2>
constexpr
bool operator == (SignedMagnitudeConstant<S1, V1>, SignedMagnitudeConstant<S2, V2>)
{
return V1 == V2 && (S1 == S2 || !V1);
}
template<bool S1, ullong V1, bool S2, ullong V2>
constexpr
bool operator != (SignedMagnitudeConstant<S1, V1> lhs, SignedMagnitudeConstant<S2, V2> rhs)
{
return !(lhs == rhs);
}
template<bool S, ullong V>
constexpr
SignedMagnitudeConstant<!S, V> operator -(SignedMagnitudeConstant<S, V>)
{
return {};
}
struct pint8 { int8_t value; int8_t operator +() { return value; } };
struct pint16 { int16_t value; int16_t operator +() { return value; } };
struct pint32 { int32_t value; int32_t operator +() { return value; } };
struct pint64 { int64_t value; int64_t operator +() { return value; } };
struct nint8 { int8_t value; int8_t operator -() { return value; } };
struct nint16 { int16_t value; int16_t operator -() { return value; } };
struct nint32 { int32_t value; int32_t operator -() { return value; } };
struct nint64 { int64_t value; int64_t operator -() { return value; } };
template<char... C>
constexpr
SignedMagnitudeConstant<false, IntParser<C...>::value> operator "" _const()
{ return {}; }
template<char... C>
constexpr
uint8_t operator "" _u8 () { return operator "" _const<C...>(); }
template<char... C>
constexpr
uint16_t operator "" _u16 () { return operator "" _const<C...>(); }
template<char... C>
constexpr
uint32_t operator "" _u32 () { return operator "" _const<C...>(); }
template<char... C>
constexpr
uint64_t operator "" _u64 () { return operator "" _const<C...>(); }
template<char... C>
constexpr
pint8 operator "" _p8 () { return pint8{operator "" _const<C...>()}; }
template<char... C>
constexpr
pint16 operator "" _p16 () { return pint16{operator "" _const<C...>()}; }
template<char... C>
constexpr
pint32 operator "" _p32 () { return pint32{operator "" _const<C...>()}; }
template<char... C>
constexpr
pint64 operator "" _p64 () { return pint64{operator "" _const<C...>()}; }
template<char... C>
constexpr
nint8 operator "" _n8 () { return nint8{-operator "" _const<C...>()}; }
template<char... C>
constexpr
nint16 operator "" _n16 () { return nint16{-operator "" _const<C...>()}; }
template<char... C>
constexpr
nint32 operator "" _n32 () { return nint32{-operator "" _const<C...>()}; }
template<char... C>
constexpr
nint64 operator "" _n64 () { return nint64{-operator "" _const<C...>()}; }
} // anonymous namespace
} // namespace ints
using ints::operator "" _const;
using ints::operator "" _u8;
using ints::operator "" _u16;
using ints::operator "" _u32;
using ints::operator "" _u64;
using ints::operator "" _p8;
using ints::operator "" _p16;
using ints::operator "" _p32;
using ints::operator "" _p64;
using ints::operator "" _n8;
using ints::operator "" _n16;
using ints::operator "" _n32;
using ints::operator "" _n64;
#endif // TMWA_INTS_UDL_HPP
|