-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
290 lines (236 loc) · 9.37 KB
/
main.cpp
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
// ----------------------------------------------------------------------------
// MIT License
//
// Copyright (c) 2022-2023 niXman (github dot nixman at pm dot me)
// This file is part of TinyArgs(github.com/niXman/named-args) project.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ----------------------------------------------------------------------------
#include <named-args/named-args.hpp>
#include <iostream>
#include <string>
#include <cassert>
const char *k_fname = "1.txt";
const int k_fsize = 1234;
const char k_fmode = 'r';
const char *k_ipaddr = "192.168.1.101";
/*************************************************************************************************/
struct noncopyable {
noncopyable(const noncopyable &) = delete;
noncopyable& operator= (const noncopyable &) = delete;
noncopyable(noncopyable &&) = default;
noncopyable& operator= (noncopyable &&) = default;
using type = std::string;
type v;
template<typename U>
noncopyable operator=(U &&u) const {
return {std::forward<U>(u)};
}
};
// declaration of args-group with it's members
struct {
NAMEDARGS_ARG(fname, std::string);
NAMEDARGS_ARG(fsize, int);
NAMEDARGS_ARG(fmode, char);
NAMEDARGS_ARG(ipaddr, noncopyable);
} const args;
/*************************************************************************************************/
// fname - required
// fsize - required
// fmode - optional
// variadic packed as tuple
template<typename ...Args>
int process_file_0(Args && ...a) {
// pack variadic-list into a tuple.
auto tuple = std::make_tuple(std::forward<Args>(a)...);
// get as required.
// if the 'fmode' was not passed to this function - it will leads to compile-time error!
auto fname = namedargs::get(args.fname, tuple);
auto fsize = namedargs::get(args.fsize, tuple);
// get as optional.
// it the option was not passed to the function, then 'r' will be used for 'fmode'.
auto fmode = namedargs::get(args.fmode, args.fmode = 'w', tuple);
assert(fname == k_fname);
assert(fsize == k_fsize);
if ( namedargs::details::position<decltype(args.fmode), Args...>::value != -1 ) {
assert(fmode == k_fmode);
} else {
assert(fmode == 'w');
}
return fmode;
}
// fname - required
// fsize - required
// fmode - optional
// variadic NOT packed as tuple
template<typename ...Args>
int process_file_1(Args && ...a) {
// get as required.
// if the 'fmode' was not passed to this function - it will leads to compile-time error!
auto fname = namedargs::get(args.fname, std::forward<Args>(a)...);
auto fsize = namedargs::get(args.fsize, std::forward<Args>(a)...);
// get as optional.
// it the option was not passed to the function, then 'r' will be used for 'fmode'.
auto fmode = namedargs::get(args.fmode, args.fmode = 'w', std::forward<Args>(a)...);
assert(fname == k_fname);
assert(fsize == k_fsize);
if ( namedargs::details::position<decltype(args.fmode), Args...>::value != -1 ) {
assert(fmode == k_fmode);
} else {
assert(fmode == 'w');
}
return fmode;
}
/*************************************************************************************************/
// non-copyable test
// fname - required
// fmode - optional
// ipaddr - optional
// variadic packed as tuple
template<typename ...Args>
noncopyable process_file_2(Args && ...a) {
// pack variadic-list into a tuple.
auto tuple = std::make_tuple(std::forward<Args>(a)...);
// get as required.
// if the 'fmode' was not passed to this function - it will leads to compile-time error!
auto fname = namedargs::get(args.fname, tuple);
// get as optional.
// it the option was not passed to the function, then 'r' will be used for 'fmode'.
auto fmode = namedargs::get(args.fmode, args.fmode = 'w', tuple);
// the same as for above, but for non-copyable-but-movable type
auto ipaddr = namedargs::get(args.ipaddr, args.ipaddr = "192.168.1.102", tuple);
assert(fname == k_fname);
if ( namedargs::details::position<decltype(args.fmode), Args...>::value != -1 ) {
assert(fmode == k_fmode);
} else {
assert(fmode == 'w');
}
if ( namedargs::details::position<decltype(args.ipaddr), Args...>::value != -1 ) {
assert(ipaddr.v == k_ipaddr);
} else {
assert(ipaddr.v == "192.168.1.102");
}
return ipaddr;
}
/*************************************************************************************************/
// overloading example
template<typename ...Args>
NAMEDARGS_FUNC_ENABLE(Args..., args.ipaddr)
(int) overloaded(Args && .../*args*/) {
return 0;
}
template<typename ...Args>
NAMEDARGS_FUNC_DISABLE(Args..., args.ipaddr)
(int) overloaded(Args && .../*args*/) {
return 1;
}
/*************************************************************************************************/
template<std::size_t I, typename T, typename ...Args>
constexpr bool tuple_elem_equal(Args && ...) {
using elem = typename namedargs::details::tuple_element<I, Args...>::type;
return std::is_same<T, elem>::value;
}
/*************************************************************************************************/
// usage
int main(int argc, char **) {
static_assert(namedargs::details::position<int>::value == -1, "");
static_assert(namedargs::details::position<int, char, long>::value == -1, "");
static_assert(namedargs::details::position<int, char, int>::value == 1, "");
static_assert(namedargs::details::position<int, int, char>::value == 0, "");
using types = namedargs::details::types_list<int, char, float, long>;
constexpr bool ok0 = namedargs::details::multi_contains<types, double, short, int>::value;
static_assert(ok0, "");
constexpr bool ok1 = namedargs::details::multi_contains<types, double, short>::value;
static_assert(!ok1, "");
using tuple = std::tuple<char, int &, const double, const long &>;
using Elem0 = namedargs::details::tuple_element<0, tuple>::type;
static_assert(std::is_same<char, Elem0>::value, "");
using Elem1 = namedargs::details::tuple_element<1, tuple>::type;
static_assert(std::is_same<int &, Elem1>::value, "");
using Elem2 = namedargs::details::tuple_element<2, tuple>::type;
static_assert(std::is_same<const double, Elem2>::value, "");
using Elem3 = namedargs::details::tuple_element<3, tuple>::type;
static_assert(std::is_same<const long &, Elem3>::value, "");
static_assert(tuple_elem_equal<0, int>(3) == true, "");
static_assert(tuple_elem_equal<0, int &>(argc) == true, "");
static_assert(tuple_elem_equal<0, const int &>(static_cast<const int &>(argc)) == true, "");
static_assert(tuple_elem_equal<0, int>(std::move(argc)) == true, "");
static_assert(tuple_elem_equal<0, int>(std::forward<int>(argc)) == true, "");
// variadic packed as tuple
// the order of the specified arguments doesn't matter!
int r = process_file_0(
args.fname = k_fname
,args.fsize = k_fsize
,args.fmode = k_fmode
);
assert(r == 'r');
r = process_file_0(
args.fname = k_fname
,args.fmode = k_fmode
,args.fsize = k_fsize
);
assert(r == 'r');
r = process_file_0(
args.fname = k_fname
,args.fsize = k_fsize
);
assert(r == 'w');
r = overloaded(args.fname = "");
assert(r == 1);
r = overloaded(args.ipaddr = "");
assert(r == 0);
// variadic NOT packed as tuple
r = process_file_1(
args.fname = k_fname
,args.fsize = k_fsize
,args.fmode = k_fmode
);
assert(r == 'r');
r = process_file_1(
args.fname = k_fname
,args.fmode = k_fmode
,args.fsize = k_fsize
);
assert(r == 'r');
r = process_file_1(
args.fname = k_fname
,args.fsize = k_fsize
);
assert(r == 'w');
// non-copyable test
auto nc0 = process_file_2(
args.fname = k_fname
,args.ipaddr= k_ipaddr
,args.fmode = k_fmode
);
assert(nc0.v == k_ipaddr);
auto nc1 = process_file_2(
args.fname = k_fname
,args.fmode = k_fmode
,args.ipaddr= k_ipaddr
);
assert(nc1.v == k_ipaddr);
auto nc2 = process_file_2(
args.fname = k_fname
,args.fmode = k_fmode
);
assert(nc2.v == "192.168.1.102");
return r;
}
/*************************************************************************************************/