-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathconv.hpp
232 lines (203 loc) · 4.99 KB
/
conv.hpp
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
// LGPL 3 or higher Robert Burner Schadek rburners@gmail.com
#pragma once
#include <string>
#include <limits>
#include <assert.h>
#include <type_traits>
#include <stdexcept>
#include <int128.hpp>
#include <format.hpp>
#include <compare.hpp>
template<typename T, typename F>
bool convIsOk(const F f, typename std::enable_if<
std::is_signed<T>::value && std::is_signed<F>::value
>::type* = 0) {
return std::numeric_limits<T>::min() <= f &&
std::numeric_limits<T>::max() >= f;
}
template<typename T, typename F>
bool convIsOk(const F f, typename std::enable_if<
std::is_unsigned<T>::value && std::is_signed<F>::value
>::type* = 0) {
return f >= 0
&& static_cast<uint64_t>(f) <= std::numeric_limits<T>::max();
}
template<typename T, typename F>
bool convIsOk(const F f, typename std::enable_if<
std::is_signed<T>::value && std::is_unsigned<F>::value
>::type* = 0) {
return f <= std::numeric_limits<T>::max();
}
template<typename T, typename F>
bool convIsOk(const F f, typename std::enable_if<
std::is_unsigned<T>::value && std::is_unsigned<F>::value
>::type* = 0) {
return f <= std::numeric_limits<T>::max();
}
template<typename T, typename F>
bool convIsOk(const F f, typename std::enable_if<
is_sweet_int128<F>::value>::type* = 0)
{
return sweet::greaterEqual(f, std::numeric_limits<T>::min()) &&
sweet::lessEqual(f, std::numeric_limits<T>::max());
}
template<typename T>
class NumberConv {
public:
template<typename F>
T operator()(const F f,
typename std::enable_if<std::is_fundamental<F>::value, F>
::type* = 0) const
{
if(convIsOk<T,F>(f)) {
return static_cast<T>(f);
} else {
throw std::range_error(format("illegal conv %d <= %d <= %d",
std::numeric_limits<T>::min(), f, std::numeric_limits<T>::max()));
}
}
};
template<typename S>
class ConvStruct {
public:
S operator()(const std::string& s) {
return static_cast<S>(stoi(s));
}
};
template<>
class ConvStruct<char> {
public:
char operator()(const std::string& s) {
return static_cast<char>(stoi(s));
}
};
template<>
class ConvStruct<unsigned char> {
public:
unsigned char operator()(const std::string& s) {
unsigned long tmp = stoul(s);
assert(tmp <= std::numeric_limits<unsigned char>::max());
return static_cast<unsigned char>(tmp);
}
};
template<>
class ConvStruct<short> {
public:
short operator()(const std::string& s) {
return static_cast<short>(stoi(s));
}
};
template<>
class ConvStruct<unsigned short> {
public:
unsigned short operator()(const std::string& s) {
unsigned long tmp = stoul(s);
assert(tmp <= std::numeric_limits<unsigned short>::max());
return static_cast<unsigned short>(tmp);
}
};
template<>
class ConvStruct<int> {
public:
int operator()(const std::string& s) {
return stoi(s);
}
};
template<>
class ConvStruct<unsigned int> {
public:
unsigned int operator()(const std::string& s) {
unsigned long tmp = stoul(s);
assert(tmp <= std::numeric_limits<unsigned int>::max());
return static_cast<unsigned int>(tmp);
}
};
template<>
class ConvStruct<bool> {
public:
bool operator()(const std::string& s) {
unsigned long tmp = stoul(s);
assert(tmp <= std::numeric_limits<unsigned int>::max());
return s == "true";
}
};
template<>
class ConvStruct<long> {
public:
long operator()(const std::string& s) {
return stol(s);
}
};
template<>
class ConvStruct<unsigned long> {
public:
unsigned long operator()(const std::string& s) {
return stoul(s);
}
};
template<>
class ConvStruct<long long> {
public:
long long operator()(const std::string& s) {
return stoll(s);
}
};
template<>
class ConvStruct<unsigned long long> {
public:
unsigned long long operator()(const std::string& s) {
return stoull(s);
}
};
template<>
class ConvStruct<float> {
public:
float operator()(const std::string& s) {
return stof(s);
}
};
template<>
class ConvStruct<double> {
public:
double operator()(const std::string& s) {
return stod(s);
}
};
template<>
class ConvStruct<long double> {
public:
long double operator()(const std::string& s) {
return stold(s);
}
};
template<typename T>
struct is_string {
enum { value = std::is_same<std::string, T>::value || std::is_same<const char*, T>::value ||
(std::is_array<T>::value && std::is_same<char, typename std::remove_extent<T>::type>::value) };
};
template<typename S, typename T>
typename std::enable_if<is_string<T>::value && is_string<S>::value, S>::type
to(const T& in) {
return in;
}
template<typename S, typename T>
typename std::enable_if<!is_string<T>::value && is_string<S>::value, S>::type
to(const T& in) {
return std::to_string(in);
}
template<typename S, typename T>
typename std::enable_if<is_string<T>::value && !is_string<S>::value, S>::type
to(const T& in) {
ConvStruct<S> ret;
return ret(in);
}
template<typename S, typename T>
typename std::enable_if<!is_string<T>::value && !is_string<S>::value, S>::type
to(const T& in) {
if(convIsOk<S>(in)) {
return static_cast<S>(in);
} else {
throw std::range_error(format("illegal conv %d <= %d <= %d",
std::numeric_limits<T>::min(), in, std::numeric_limits<T>::max()));
}
}