-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptparser.h
More file actions
executable file
·408 lines (360 loc) · 12.9 KB
/
Copy pathoptparser.h
File metadata and controls
executable file
·408 lines (360 loc) · 12.9 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#ifndef OPTPARSER_H
#define OPTPARSER_H
#include <tuple>
#include <assert.h>
#include <regex>
#include <iostream>
#include <list>
namespace OPTPARSER
{
template<typename OUT> auto do_convert_dec(std::string::const_iterator beg,const std::string::const_iterator& en)
{
static_assert( std::numeric_limits<OUT>::max()>=10,"not enough space in type");
OUT ret=0;
const OUT ten(10);
const OUT rmax(std::numeric_limits<OUT>::max()/ten);
for(;beg!=en;++beg)
{
if(*beg<'0' || *beg>'9')
{
throw std::invalid_argument("wrong symbol found");
}
const OUT digit=*beg-'0';
if( ret > rmax)
{
throw std::invalid_argument("overflow on shift");
}
ret*=ten;
if( ret > std::numeric_limits<OUT>::max()-digit)
{
throw std::invalid_argument("overflow on add");
}
ret+=digit;
}
return(ret);
}
template<typename OUT> auto do_convert_hex(std::string::const_iterator beg,const std::string::const_iterator& en)
{
static_assert( std::numeric_limits<OUT>::max()>=16,"not enough space in type");
OUT ret=0;
const OUT sixteen(16);
const OUT rmax(std::numeric_limits<OUT>::max()/sixteen);
for(;beg!=en;++beg)
{
OUT digit;
if (*beg>='0' && *beg<='9')
{
digit=*beg-'0';
}
else
{
if(*beg>='A' && *beg<='F')
{
digit=*beg-'A';
}
else
{
throw std::invalid_argument("wrong symbol found");
}
}
if( ret > rmax)
{
throw std::invalid_argument("overflow on shift");
}
ret*=sixteen;
if( ret > std::numeric_limits<OUT>::max()-digit)
{
throw std::invalid_argument("overflow on add");
}
ret+=digit;
}
return(ret);
}
template<typename OUT,bool sign> struct string_conversions
{
static auto do_convert(std::string::const_iterator beg,const std::string::const_iterator& en)
{
if constexpr(sign)
{
static_assert( std::numeric_limits<OUT>::min()<=-9,"not enough space in type");
static_assert( std::numeric_limits<OUT>::max()>=10, "not enough space in type");
switch(*beg)
{
case '-':
{
if(++beg==en)
{
throw std::invalid_argument("this string is not a number");
}
OUT ret=0;
const OUT ten(10);
const OUT rmin((std::numeric_limits<OUT>::min()/ten));
for(;beg!=en;++beg)
{
if(*beg<'0' || *beg>'9')
{
throw std::invalid_argument("wrong symbol found");
}
const OUT digit=*beg-'0';
if( ret < rmin)
{
throw std::invalid_argument("overflow on shift");
}
ret*=ten;
if(ret < std::numeric_limits<OUT>::min()+digit)
{
throw std::invalid_argument("overflow on add");
}
ret-=digit;
}
return(ret);
}
case '+':
{
if(++beg==en)
{
throw std::invalid_argument("this string is not a number");
}
}
default:
{
return(string_conversions<OUT,false>::do_convert(beg,en));
}
}
}
else
{
if(*beg=='0')
{
if(++beg==en)
{
return static_cast<OUT>(0);
}
if(*beg=='x')
{
if(++beg==en)
{
throw std::invalid_argument("this string is not a number");
}
return(do_convert_hex<OUT>(beg,en));
}
}
return(do_convert_dec<OUT>(beg,en));
}
}
};
template<typename OUT> static auto string_to_number(const std::string& value)
{
static_assert( std::numeric_limits<OUT>::is_exact,"not exact type");
if(value.empty())
{
throw std::invalid_argument("empty string is given!");
}
return(string_conversions<OUT,std::numeric_limits<OUT>::is_signed>::do_convert(value.cbegin(),value.cend()));
}
using optlist=std::list<const char*>;
template<typename T> struct NoDefaultValue
{
};
template<typename T> struct HasDefaultValue
{
};
struct Boolean
{
};
template <typename T>
struct function_traits
: public function_traits<decltype(&T::operator())>
{};
// For generic types, directly use the result of the signature of its 'operator()'
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const>
// we specialize for pointers to member function
{
typedef ReturnType result_type;
};
constexpr auto makeO(const char* name,bool defvalue=false)
{
return std::make_tuple(name,Boolean(),defvalue);
}
template<typename T> constexpr auto makeO(const char* name,T dflMaker)
{
typedef function_traits<T> traits;
typedef typename traits::result_type parameter_type;
return std::make_tuple(name,HasDefaultValue<parameter_type>(),dflMaker);
}
template<typename T> constexpr auto makeO(const char* name)
{
return std::make_tuple(name,NoDefaultValue<T>(),nullptr);
}
constexpr size_t scomp(const char* a,const char* b)
{
return( (*a==*b)
? (
(*a=='\0')
? 1
: scomp(a+1,b+1)
)
: 0
);
}
std::match_results<const char *> g_walk_step(const std::string& regexString,optlist& args);
template<typename T,typename Initializer> struct walker_step
{
};
template<> struct walker_step<const Boolean,const bool >
{
static auto walk_step(const char* name,const bool init,optlist& args) //rfirst init out
{ // 0 0 1
const auto res=g_walk_step(std::string("--") + std::string(name),args); // 0 1 0
return(std::make_tuple(init==res.empty())); // 1 0 0
} // 1 1 1
static constexpr auto slice_step()
{
return std::make_tuple(bool());
}
};
template<typename T,typename Initializer> struct walker_step<const HasDefaultValue<T>,Initializer >
{
static auto walk_step(const char* name,Initializer init,optlist& args)
{
const auto res=g_walk_step(std::string("--") + std::string(name) + ("=(.*)"),args);
if(res.empty())
{
return(std::make_tuple(init()));
}
else
{
assert(res.size()==2);
return(std::make_tuple(T(res[1])));
}
}
static constexpr auto slice_step()
{
return std::make_tuple(T(""));
}
};
template<typename T,typename Initializer> struct walker_step <const NoDefaultValue<T>, Initializer >
{
static auto walk_step(const char* name,Initializer,optlist &args)
{
const auto res=g_walk_step(std::string("--") + std::string(name) + ("=(.*)"),args);
if(res.empty())
{
throw std::logic_error("Mandatory parameter is not set!");
}
else
{
assert(res.size()==2);
#ifndef NDEBUG
std::cerr << "\t\tArgument is " << res[1] <<'\n';
#endif
return(std::make_tuple(T(res.str(1).c_str())));
}
}
static constexpr auto slice_step()
{
return std::make_tuple(T(""));
}
};
template<typename A,size_t N> struct walker
{
static constexpr size_t find(A input,const char* func)
{
return(
(scomp(std::get<N>(input),func)==1)
? (N)
: walker<A,N-3>::find(input,func)
);
}
static constexpr auto walk(A input,optlist& args)
{
return tuple_cat(
walker<A,N-3>::walk(input,args),
walker_step<typename std::tuple_element<N+1,A>::type,typename std::tuple_element<N+2,A>::type>::walk_step(std::get<N>(input),std::get<N+2>(input),args)
);
}
static constexpr auto slice(A input)
{
return tuple_cat(
walker<A,N-3>::slice(input),
walker_step<typename std::tuple_element<N+1,A>::type,typename std::tuple_element<N+2,A>::type>::slice_step()
);
}
static constexpr bool checkDupes(A input)
{
return (walker<A,N-3>::find(input,std::get<N>(input))!=size_t(-1)) | walker<A,N-3>::checkDupes(input);
}
};
template<typename A> struct walker<A,0>
{
static constexpr size_t find(A input,const char* func)
{
return(
(scomp(std::get<0>(input),func)==1)
? 0
: -1
);
}
static constexpr auto walk(A input,optlist& args)
{
return walker_step<typename std::tuple_element<1,A>::type,typename std::tuple_element<2,A>::type>::walk_step(std::get<0>(input),std::get<2>(input),args);
}
static constexpr auto slice(A)
{
return walker_step<typename std::tuple_element<1,A>::type,typename std::tuple_element<2,A>::type>::slice_step();
}
static constexpr bool checkDupes(A)
{
return false;
}
};
template<bool check> class OptionsCheck
{
static constexpr bool chk()
{
static_assert(!check,"duplicate options found!");
return(true);
}
static constexpr auto checked=chk();
public:
virtual ~OptionsCheck()
{
}
};
template<class Cont> class Options :private OptionsCheck< walker<decltype(Cont::options),std::tuple_size<decltype(Cont::options)>::value-3>::checkDupes(Cont::options) >
{
public:
optlist residue_args;
private:
decltype(walker<decltype(Cont::options),std::tuple_size<decltype(Cont::options)>::value-3>::slice(Cont::options)) vals;
static size_t check_argc(int argc)
{
if(argc<0)
{
throw std::logic_error("Negative parameter count!");
}
return argc;
}
public:
Options(int argc,char* argv[]):residue_args(&argv[1],&argv[check_argc(argc)]),vals(walker<decltype(Cont::options),std::tuple_size<decltype(Cont::options)>::value-3>::walk(Cont::options,residue_args))
{
}
static constexpr size_t find(const char* func)
{
return walker<decltype(Cont::options),std::tuple_size<decltype(Cont::options)>::value-3>::find(Cont::options,func);
}
template<size_t index> const auto& extract() const
{
static_assert(index!=size_t(-1),"err not found");
return std::get<index/3>(vals);
}
template<size_t index> auto& extract()
{
static_assert(index!=size_t(-1),"err not found");
return std::get<index/3>(vals);
}
};
}
#define $(optlist,name) ((optlist).extract<std::remove_reference<decltype(optlist)>::type::find(name)>())
#endif // OPTPARSER_H