-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_line.hpp
More file actions
331 lines (295 loc) · 10.6 KB
/
Copy pathcommand_line.hpp
File metadata and controls
331 lines (295 loc) · 10.6 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
/// This header file defines a very low-cost and error-safe library for basic storage and treatment of
/// program command line arguments.
/// mailto:farid.mehrabi@gmail.com
#ifndef COMMAND_LINE_HPP
#define COMMAND_LINE_HPP
#include <span>
#include <string_view>
/// <summary>
/// this namespace is upposed to be replaced with 'boost' or 'std', iff adopted as part the above
/// libs - by proper authorities. For the time being the 'fm' suffix in the namespace identifier
/// refers the signature and initials of the author.
/// </summary>
namespace lib_fm {
//using namespace std;
using std::dynamic_extent;
using std::char_traits;
using std::span;
using std::basic_string_view;
/// <summary>
/// this class is supposed to wrap arguments of the main function in an ideally NOOP fashion and provide
/// a smooth and as error-free as possible API
/// </summary>
/// <param name="ch_type">
/// character type to be used
/// </param>
/// <param name="ext">
/// defines the extent(count) of arguments. By default ('std::dynamic_extent') the count is evaluated
/// at runtime.
/// </param>
/// <param name="traits">
/// coddec and conversions used by std::basic_string_view
/// </param>
template<typename ch_type, size_t ext = dynamic_extent, typename traits = char_traits<ch_type>>
struct basic_command_line
: span<ch_type* const, ext>
{
//important type aliases:
using base_type = span<ch_type* const, ext>;
using char_type = ch_type;
using char_traits = traits;
using legacy_reference = typename base_type::reference;
using legacy_element_type = typename base_type::element_type;
using legacy_pointer = typename base_type::pointer;
using command_pointer = char_type**;
typedef const char_type *const*pointer, *const*const_pointer;
/// <summary>
/// ='std::basic_string_view'.
/// Result of 'front', 'back', 'operator[index]' and dereferencing 'basic_command_line::iterator'
/// </summary>
typedef const basic_string_view<char_type, char_traits> argument_type, value_type, element_type, const_element_type;
template <typename base_iterator> struct basic_iterator;
using iterator = basic_iterator<typename base_type::iterator>;
using reverse_iterator = basic_iterator<typename base_type::reverse_iterator>;
//unaffected 'span' members:
using typename base_type::difference_type;
using typename base_type::size_type;
using base_type::extent;
using base_type::size;
using base_type::empty;
using base_type::size_bytes;
//forwarding to 'span::span' constructors:
using span<ch_type* const, ext>::span;
explicit(extent != dynamic_extent)
/// <summary>
/// The only none forwarded constructor defined within the class
/// </summary>
/// <param name="count">reads the count 'argc' of arguments from 'main' function </param>
/// <param name="first">reads the'argv' argument list from 'main' function</param>
/// <returns>creates a light-weight object to mannage command line argumnts</returns>
constexpr basic_command_line(int count, command_pointer first) noexcept
: base_type{ first, static_cast<size_t>(count) }
{};
//iterator to first command line argument:
constexpr iterator begin() const noexcept
{
return { this->base_type::begin() };
};
//iterator to first invalid location past the last command line argument:
constexpr iterator end() const noexcept
{
return { this->base_type::end() };
};
//reverse iterator to last command line argument:
constexpr reverse_iterator rbegin() const noexcept
{
return { this->base_type::rbegin() };
};
//reverse iterator to first invalid location before the first command line argument:
constexpr reverse_iterator rend() const noexcept
{
return { this->base_type::rend() };
};
friend auto as_bytes(basic_command_line cmd)noexcept
{
return static_cast<base_type>(cmd);
};
/// <summary>
/// Retrives the last argument from command line
/// </summary>
/// <returns>
/// A veiw of 'basic_command_line::argument_type' to the last command line argument
/// </returns>
constexpr argument_type back() const noexcept
{
return { this->base_type::back() };
};
/// <summary>
/// Retrives the first argument from command line
/// </summary>
/// <returns>
/// A veiw of 'basic_command_line::argument_type' to the first command line argument
/// </returns>
constexpr argument_type front() const noexcept
{
return { this->base_type::front() };
};
constexpr auto operator[](size_type offset) const
{
return begin()[offset];
};
constexpr const_pointer data() const noexcept
{
return this->base_type::data();
};
///<summary> Hides 'span::subspann'.</summary>
///<param name="offset">
///Beginning argument in the new view with respect to current view.</param>
///<param name="count">
///Requested count of arguments in new view. Defaults to max('std::dynamic_extent')
///</param>
///<returns>
///A new list of 'count' command line arguments beginning at 'offset' from this
///</returns>
constexpr auto subspan(size_type offset, size_type count = dynamic_extent) const noexcept
{
return from_span(this->base_type::subspan(offset, count));
};
///<summary> Hides 'span::subspann'.</summary>
///<param name="offset">
///'constexpr' start argument in the new view with respect to current view.</param>
///<param name="count">
///'constexpr' requested count of arguments in new view.
/// Default is max('std::dynamic_extent')
///</param>
///<returns>
///A new list of 'count' command line arguments beginning at 'offset' from this
///</returns>
template <size_t offset, size_t count = dynamic_extent>
constexpr auto subspan() const noexcept
{
return from_span(this->base_type::template subspan<offset, count>());
};
///<summary> Hides 'span::first'.</summary>
///<param name="count">
///Requested count of arguments in new view.
///</param>
///<returns>
///A new list of 'count' left-most command line arguments
///</returns>
constexpr auto first(size_type count) const noexcept
{
return from_span(this->base_type::first(count));
};
///<summary> Hides 'span::first'.</summary>
///<param name="count">
///'constexpr' requested count of arguments in new view.
///</param>
///<returns>
///A new list of 'count' left-most command line arguments
///</returns>
template <size_t count>
constexpr auto first() const noexcept
{
return from_span(this->base_type::template first<count>());
};
///<summary> Hides 'span::last'.</summary>
///<param name="count">
///Requested count of arguments in new view.
///</param>
///<returns>
///A new list of 'count' right-most command line arguments
///</returns>
constexpr auto last(size_type count) const noexcept
{
return from_span(this->base_type::last(count));
};
///<summary> Hides 'span::last'.</summary>
///<param name="count">
///'constexpr' requested count of arguments in new view.
///</param>
///<returns>
///A new list of 'count' right-most command line arguments
///</returns>
template <size_t count>
constexpr auto last() const noexcept
{
return from_span(this->base_type::template last<count>());
};
/// <summary>
/// Wraps and decorates iterators inherited from 'std::span'
/// This is actually a hack and should be modified
/// </summary>
/// <param name="base_iterator">
/// ='std::span::iterator' or 'std::span::const_iterator'
/// </param>
template <typename base_iterator>
struct basic_iterator
: base_iterator
{
typedef const argument_type value_type, reference_type, const_reference_type;
struct pointer;
using const_pointer=pointer;
auto operator<=>(basic_iterator const& right)const noexcept = default;
constexpr auto operator[](size_type offset) const
{
return *(*this + offset);
};
constexpr const_reference_type operator*() const
{
return { base_iterator::operator*() };
};
constexpr auto operator->() const
{
return pointer{ .value{**this} };
};
constexpr auto& operator++() noexcept
{
base_iterator::operator++();
return *this;
};
constexpr basic_iterator operator--() noexcept
{
base_iterator::operator--();
return *this;
};
constexpr basic_iterator operator++(int) noexcept
{
return { base_iterator::operator++(0) };
};
constexpr basic_iterator operator--(int) noexcept
{
return { base_iterator::operator--(0) };
};
constexpr basic_iterator operator+=(size_type add) noexcept
{
return { base_iterator {*this} += add };
};
constexpr basic_iterator operator-=(size_type sub) noexcept
{
return { base_iterator {*this} -= sub };
};
constexpr basic_iterator operator+(size_type add) const noexcept
{
return { base_iterator {*this} + add };
};
constexpr basic_iterator operator-(size_type sub) const noexcept
{
return { base_iterator {*this} - sub };
};
/// <summary>
/// a smart ptr that converts null-terminated strings to 'std::string_view's on the fly. A fancy ptr to be used
/// as the result of indirection ('basic_iterator::operator->').
/// <TODO>Move this outside the 'basic_comand_line' to reduce compiler load; It doesn't depent on 'extent'</TODO>
/// </summary>
struct pointer {
argument_type const value;
constexpr auto operator->()const noexcept
{
return &value;
};
};
private:
friend struct basic_command_line;
constexpr basic_iterator(base_iterator val) noexcept
:base_iterator{ move(val) }
{};
};//!basic_iterator
private:
using typename base_type::reference;
using typename base_type::const_reference;
/// <summary>
/// Changes the view from a list of null-terminated strings to a list of 'std::basic_string_view'
/// <TODO>Move this outside the 'basic_comand_line' to reduce compiler load; It doesn't depent on 'this->extent'</TODO>
/// </summary>
/// <param name="source"> result of 'std::span::first', 'std::span::last', 'std::span::subspan' </param>
/// <returns>A subview of 'std::basic_string_view' to the command line arguments</returns>
template<size_t oth_ext>
static constexpr auto from_span(span<legacy_element_type, oth_ext> source) noexcept
{
return basic_command_line<char_type,oth_ext,char_traits> { source.data(),source.size() };
}
};//!basic_command_line
using command_line = basic_command_line<char>;
};//!lib_fm
#endif //! COMMAND_LINE_HPP