forked from docopt/docopt.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocopt_value.cpp
More file actions
294 lines (244 loc) · 5.71 KB
/
Copy pathdocopt_value.cpp
File metadata and controls
294 lines (244 loc) · 5.71 KB
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
284
285
286
287
288
289
290
291
292
293
294
#include "docopt_value.h"
#include "docopt_config.h"
#include "docopt_util.h"
#include "docopt_private.h"
DOCOPT_INLINE
docopt::value::value() {}
DOCOPT_INLINE
docopt::value::value(bool v)
: kind_(Kind::Bool)
{
variant_.boolValue = v;
}
DOCOPT_INLINE
docopt::value::value(long v)
: kind_(Kind::Long)
{
variant_.longValue = v;
}
DOCOPT_INLINE
docopt::value::value(int v)
: value(static_cast<long>(v))
{
}
DOCOPT_INLINE
docopt::value::value(std::string v)
: kind_(Kind::String)
{
new (&variant_.strValue) std::string(std::move(v));
}
DOCOPT_INLINE
docopt::value::value(std::vector<std::string> v)
: kind_(Kind::StringList)
{
new (&variant_.strList) std::vector<std::string>(std::move(v));
}
DOCOPT_INLINE
docopt::value::value(value const& other)
: kind_(other.kind_)
{
switch (kind_) {
case Kind::String:
new (&variant_.strValue) std::string(other.variant_.strValue);
break;
case Kind::StringList:
new (&variant_.strList) std::vector<std::string>(other.variant_.strList);
break;
case Kind::Bool:
variant_.boolValue = other.variant_.boolValue;
break;
case Kind::Long:
variant_.longValue = other.variant_.longValue;
break;
case Kind::Empty:
default:
break;
}
}
DOCOPT_INLINE
docopt::value::value(value&& other) noexcept
: kind_(other.kind_)
{
switch (kind_) {
case Kind::String:
new (&variant_.strValue) std::string(std::move(other.variant_.strValue));
break;
case Kind::StringList:
new (&variant_.strList) std::vector<std::string>(std::move(other.variant_.strList));
break;
case Kind::Bool:
variant_.boolValue = other.variant_.boolValue;
break;
case Kind::Long:
variant_.longValue = other.variant_.longValue;
break;
case Kind::Empty:
default:
break;
}
}
DOCOPT_INLINE
docopt::value::~value()
{
switch (kind_) {
case Kind::String:
variant_.strValue.~basic_string();
break;
case Kind::StringList:
variant_.strList.~vector();
break;
case Kind::Empty:
case Kind::Bool:
case Kind::Long:
default:
// trivial dtor
break;
}
}
DOCOPT_INLINE
docopt::value& docopt::value::operator=(value const& other) {
// make a copy and move from it; way easier.
return *this = value{other};
}
DOCOPT_INLINE
docopt::value& docopt::value::operator=(value&& other) noexcept {
// move of all the types involved is noexcept, so we dont have to worry about
// these two statements throwing, which gives us a consistency guarantee.
this->~value();
new (this) value(std::move(other));
return *this;
}
DOCOPT_INLINE
docopt::Kind docopt::value::kind() const {
return kind_;
}
docopt::value::operator bool() const {
return kind_ != Kind::Empty;
}
DOCOPT_INLINE
bool docopt::value::isBool() const {
return kind_==Kind::Bool;
}
DOCOPT_INLINE
bool docopt::value::isString() const {
return kind_==Kind::String;
}
DOCOPT_INLINE
bool docopt::value::isLong() const {
return kind_==Kind::Long;
}
DOCOPT_INLINE
bool docopt::value::isStringList() const {
return kind_==Kind::StringList;
}
const char* docopt::value::kindAsString(Kind kind) {
switch (kind) {
case Kind::Empty: return "empty";
case Kind::Bool: return "bool";
case Kind::Long: return "long";
case Kind::String: return "string";
case Kind::StringList: return "string-list";
}
return "unknown";
}
void docopt::value::throwIfNotKind(Kind expected) const {
if (kind_ == expected)
return;
std::string error = "Illegal cast to ";
error += kindAsString(expected);
error += "; type is actually ";
error += kindAsString(kind_);
throw std::runtime_error(std::move(error));
}
template <class T>
void hash_combine(std::size_t& seed, const T& v);
DOCOPT_INLINE
size_t docopt::value::hash() const noexcept
{
switch (kind_) {
case Kind::String:
return std::hash<std::string>()(variant_.strValue);
case Kind::StringList: {
size_t seed = std::hash<size_t>()(variant_.strList.size());
for(auto const& str : variant_.strList) {
hash_combine(seed, str);
}
return seed;
}
case Kind::Bool:
return std::hash<bool>()(variant_.boolValue);
case Kind::Long:
return std::hash<long>()(variant_.longValue);
case Kind::Empty:
default:
return std::hash<void*>()(nullptr);
}
}
DOCOPT_INLINE
bool docopt::value::asBool() const
{
throwIfNotKind(Kind::Bool);
return variant_.boolValue;
}
DOCOPT_INLINE
long docopt::value::asLong() const
{
// Attempt to convert a string to a long
if (kind_ == Kind::String) {
const std::string& str = variant_.strValue;
std::size_t pos;
const long ret = stol(str, &pos); // Throws if it can't convert
if (pos != str.length()) {
// The string ended in non-digits.
throw std::runtime_error( str + " contains non-numeric characters.");
}
return ret;
}
throwIfNotKind(Kind::Long);
return variant_.longValue;
}
DOCOPT_INLINE
std::string const& docopt::value::asString() const
{
throwIfNotKind(Kind::String);
return variant_.strValue;
}
DOCOPT_INLINE
std::vector<std::string> const& docopt::value::asStringList() const
{
throwIfNotKind(Kind::StringList);
return variant_.strList;
}
DOCOPT_INLINE
bool docopt::operator==(docopt::value const& v1, docopt::value const& v2)
{
if (v1.kind_ != v2.kind_)
return false;
switch (v1.kind_) {
case Kind::String:
return v1.variant_.strValue==v2.variant_.strValue;
case Kind::StringList:
return v1.variant_.strList==v2.variant_.strList;
case Kind::Bool:
return v1.variant_.boolValue==v2.variant_.boolValue;
case Kind::Long:
return v1.variant_.longValue==v2.variant_.longValue;
case Kind::Empty:
default:
return true;
}
}
DOCOPT_INLINE
bool operator!=(docopt::value const& v1, docopt::value const& v2)
{
return !(v1 == v2);
}
DOCOPT_INLINE
docopt::value::value::Variant::Variant()
{
}
DOCOPT_INLINE
docopt::value::value::Variant::~Variant()
{
/* do nothing; will be destroyed by ~value */
}