Newer
Older
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//============================================================================//
// A string wrapper class that convert string to other data types //
// //
// Chao Peng //
// 06/07/2016 //
//============================================================================//
#include "ConfigValue.h"
#include "ConfigParser.h"
#include <climits>
#include <algorithm>
using namespace std;
//============================================================================//
// Constructors, Destructor and Assignment Operators //
//============================================================================//
ConfigValue::ConfigValue(const string &value)
: _value(value)
{}
ConfigValue::ConfigValue(string &&value)
: _value(move(value))
{}
ConfigValue::ConfigValue(const char *value)
: _value(value)
{}
ConfigValue::ConfigValue(const bool &value)
{
if(value)
_value = "1";
else
_value = "0";
}
ConfigValue::ConfigValue(const int &value)
: _value(to_string(value))
{}
ConfigValue::ConfigValue(const long &value)
: _value(to_string(value))
{}
ConfigValue::ConfigValue(const long long &value)
: _value(to_string(value))
{}
ConfigValue::ConfigValue(const unsigned &value)
: _value(to_string(value))
{}
ConfigValue::ConfigValue(const unsigned long &value)
: _value(to_string(value))
{}
ConfigValue::ConfigValue(const unsigned long long &value)
: _value(to_string(value))
{}
ConfigValue::ConfigValue(const float &value)
: _value(to_string(value))
{}
ConfigValue::ConfigValue(const double &value)
: _value(to_string(value))
{}
ConfigValue::ConfigValue(const long double &value)
: _value(to_string(value))
{}
ConfigValue &ConfigValue::operator =(const string &str)
{
(*this)._value = str;
return *this;
}
ConfigValue &ConfigValue::operator =(string &&str)
{
(*this)._value = move(str);
return *this;
}
//============================================================================//
// Public Member functions //
//============================================================================//
bool ConfigValue::Bool()
const
{
if((_value == "1") ||
(ConfigParser::case_ins_equal(_value, "T")) ||
(ConfigParser::case_ins_equal(_value, "True")) ||
(ConfigParser::case_ins_equal(_value, "Y")) ||
(ConfigParser::case_ins_equal(_value, "Yes")))
return true;
if((_value == "0") ||
(ConfigParser::case_ins_equal(_value, "F")) ||
(ConfigParser::case_ins_equal(_value, "False")) ||
(ConfigParser::case_ins_equal(_value, "N")) ||
(ConfigParser::case_ins_equal(_value, "No")))
return false;
cout << "Config Value: Failed to convert "
<< _value << " to bool type. Return false."
<< endl;
return false;
}
char ConfigValue::Char()
const
{
try {
int value = stoi(_value);
if(value > CHAR_MAX)
cout << "Config Value: Limit exceeded while converting "
<< _value << " to char." << endl;
return (char) value;
} catch (exception &e) {
cerr << e.what() << endl;
cerr << "Config Value: Failed to convert "
<< _value << " to char. 0 returned." << endl;
return 0;
}
}
unsigned char ConfigValue::UChar()
const
{
try {
unsigned long value = stoul(_value);
if(value > UCHAR_MAX)
cout << "Config Value: Limit exceeded while converting "
<< _value << " to unsigned char." << endl;
return (unsigned char) value;
} catch (exception &e) {
cerr << e.what() << endl;
cerr << "Config Value: Failed to convert "
<< _value << " to unsigned char. 0 returned." << endl;
return 0;
}
}
short ConfigValue::Short()
const
{
try {
int value = stoi(_value);
if(value > SHRT_MAX)
cout << "Config Value: Limit exceeded while converting "
<< _value << " to short." << endl;
return (short) value;
} catch (exception &e) {
cerr << e.what() << endl;
cerr << "Config Value: Failed to convert "
<< _value << " to short. 0 returned." << endl;
return 0;
}
}
unsigned short ConfigValue::UShort()
const
{
try {
unsigned long value = stoul(_value);
if(value > USHRT_MAX)
cout << "Config Value: Limit exceeded while converting "
<< _value << " to unsigned short." << endl;
return (unsigned short) value;
} catch (exception &e) {
cerr << e.what() << endl;
cerr << "Config Value: Failed to convert "
<< _value << " to unsigned short. 0 returned." << endl;
return 0;
}
}
int ConfigValue::Int()
const
{
try {
return stoi(_value);
} catch (exception &e) {
cerr << e.what() << endl;
cerr << "Config Value: Failed to convert "
<< _value << " to int. 0 returned." << endl;
return 0;
}
}
unsigned int ConfigValue::UInt()
const
{
try {
return (unsigned int)stoul(_value);
} catch (exception &e) {
cerr << e.what() << endl;
cerr << "Config Value: Failed to convert "
<< _value << " to unsigned int. 0 returned." << endl;
return 0;
}
}
long ConfigValue::Long()
const
{
try {
return stol(_value);
} catch (exception &e) {
cerr << e.what() << endl;
cerr << "Config Value: Failed to convert "
<< _value << " to long. 0 returned." << endl;
return 0;
}
}
long long ConfigValue::LongLong()
const
{
try {
return stoll(_value);
} catch (exception &e) {
cerr << e.what() << endl;
cerr << "Config Value: Failed to convert "
<< _value << " to long long. 0 returned." << endl;
return 0;
}
}
unsigned long ConfigValue::ULong()
const
{
try {
return stoul(_value);
} catch (exception &e) {
cerr << e.what() << endl;
cerr << "Config Value: Failed to convert "
<< _value << " to unsigned long. 0 returned." << endl;
return 0;
}
}
unsigned long long ConfigValue::ULongLong()
const
{
try {
return stoull(_value);
} catch (exception &e) {
cerr << e.what() << endl;
cerr << "Config Value: Failed to convert "
<< _value << " to unsigned long long. 0 returned." << endl;
return 0;
}
}
float ConfigValue::Float()
const
{
try {
return stof(_value);
} catch (exception &e) {
cerr << e.what() << endl;
cerr << "Config Value: Failed to convert "
<< _value << " to float. 0 returned." << endl;
return 0;
}
}
double ConfigValue::Double()
const
{
try {
return stod(_value);
} catch (exception &e) {
cerr << e.what() << endl;
cerr << "Config Value: Failed to convert "
<< _value << " to double. 0 returned." << endl;
return 0;
}
}
long double ConfigValue::LongDouble()
const
{
try {
return stold(_value);
} catch (exception &e) {
cerr << e.what() << endl;
cerr << "Config Value: Failed to convert "
<< _value << " to long double. 0 returned." << endl;
return 0;
}
}
const char *ConfigValue::c_str()
const
{
return _value.c_str();
}
//============================================================================//
// Other functions //
//============================================================================//
ostream &operator << (ostream &os, const ConfigValue &b)
{
return os << b.c_str();
}