-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathstring_token.hpp
More file actions
485 lines (392 loc) · 11.1 KB
/
Copy pathstring_token.hpp
File metadata and controls
485 lines (392 loc) · 11.1 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//
// Copyright (c) 2021 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/url
//
#ifndef BOOST_URL_GRAMMAR_STRING_TOKEN_HPP
#define BOOST_URL_GRAMMAR_STRING_TOKEN_HPP
#include <boost/url/detail/config.hpp>
#include <boost/core/detail/string_view.hpp>
#include <boost/url/detail/except.hpp>
#include <memory>
#include <string>
namespace boost {
namespace urls {
namespace string_token {
/** Base class for string tokens, and algorithm parameters
This abstract interface provides a means
for an algorithm to generically obtain a
modifiable, contiguous character buffer
of prescribed size.
A @ref StringToken should be derived
from this class. As the author of an
algorithm using a @ref StringToken,
simply declare an rvalue reference
as a parameter type.
Instances of this type are intended only
to be used once and then destroyed.
@par Example
The declared function accepts any
temporary instance of `arg` to be
used for writing:
@code
void algorithm( string_token::arg&& dest );
@endcode
To implement the interface for your type
or use-case, derive from the class and
implement the prepare function.
*/
struct arg
{
/** Return a modifiable character buffer
This function attempts to obtain a
character buffer with space for at
least `n` characters. Upon success,
a pointer to the beginning of the
buffer is returned. Ownership is not
transferred; the caller should not
attempt to free the storage. The
buffer shall remain valid until
`this` is destroyed.
@note
This function may only be called once.
After invoking the function, the only
valid operation is destruction.
@param n The number of characters needed
@return A pointer to the buffer
*/
virtual char* prepare(std::size_t n) = 0;
/// Virtual destructor
virtual ~arg() = default;
/// Default constructor
arg() = default;
/// Default move constructor
arg(arg&&) = default;
/// Deleted copy constructor
arg(arg const&) = delete;
/// Deleted move assignment
arg& operator=(arg&&) = delete;
/// Deleted copy assignment
arg& operator=(arg const&) = delete;
};
//------------------------------------------------
namespace implementation_defined {
template<class T, class = void>
struct is_token : std::false_type {};
template<class T>
struct is_token<T, void_t<
decltype(std::declval<T&>().prepare(
std::declval<std::size_t>())),
decltype(std::declval<T&>().result())
> > : std::integral_constant<bool,
std::is_convertible<decltype(
std::declval<T&>().result()),
typename T::result_type>::value &&
std::is_same<decltype(
std::declval<T&>().prepare(0)),
char*>::value &&
std::is_base_of<arg, T>::value &&
std::is_convertible<T const volatile*,
arg const volatile*>::value
>
{
};
} // implementation_defined
/** Trait to determine if a type is a string token
This trait returns `true` if `T` is a valid
@ref StringToken type, and `false` otherwise.
@par Example
@code
static_assert( string_token::is_token<T>::value );
@endcode
*/
template<class T>
using is_token = implementation_defined::is_token<T>;
#ifdef BOOST_URL_HAS_CONCEPTS
/** Concept for a string token
This concept is satisfied if `T` is a
valid string token type.
A string token is an rvalue passed to a function template
which customizes the return type of the function and also
controls how a modifiable character buffer is obtained and presented.
The string token's lifetime extends only for the duration of the
function call in which it appears as a parameter.
A string token cannot be copied, moved, or assigned, and must be
destroyed when the function returns or throws.
@par Semantics
`T::result_type` determines the return type of functions
that accept a string token.
The `prepare()` function overrides the virtual function
in the base class @ref arg. It must return a pointer to
a character buffer of at least size `n`, otherwise
throw an exception. This function is called only
once or not at all.
The `result()` function is invoked by the algorithm
to receive the result from the string token.
It is only invoked if `prepare()` returned
successfully and the string token was not destroyed.
It is only called after `prepare()` returns
successfully, and the string token is destroyed
when the algorithm completes or if an exception
is thrown.
String tokens cannot be reused.
@par Exemplars
String token prototype:
@code
struct StringToken : string_token::arg
{
using result_type = std::string;
char* prepare( std::size_t n ) override;
result_type result();
};
@endcode
Algorithm prototype:
@code
namespace detail {
// Algorithm implementation may be placed
// out of line, and written as an ordinary
// function (no template required).
void algorithm_impl( string_token::arg& token )
{
std::size_t n = 0;
// calculate space needed in n
// ...
// acquire a destination buffer
char* dest = token.prepare( n );
// write the characters to the buffer
}
} // detail
// public interface is a function template,
// defaulting to return std::string.
template< class StringToken = string_token::return_string >
auto
algorithm( StringToken&& token = {} ) ->
typename StringToken::result_type
{
// invoke the algorithm with the token
algorithm_impl( token );
// return the result from the token
return token.result();
}
@endcode
@par Models
The following classes and functions implement and
generate string tokens.
@li @ref return_string
@li @ref assign_to
@li @ref preserve_size
*/
template <class T>
concept StringToken =
std::derived_from<T, string_token::arg> &&
requires (T t, std::size_t n)
{
typename T::result_type;
{ t.prepare(n) } -> std::same_as<char*>;
{ t.result() } -> std::convertible_to<typename T::result_type>;
};
#endif
//------------------------------------------------
namespace implementation_defined {
struct return_string
: arg
{
using result_type = std::string;
char*
prepare(std::size_t n) override
{
s_.resize(n);
return &s_[0];
}
result_type
result() noexcept
{
return std::move(s_);
}
private:
result_type s_;
};
} // implementation_defined
/** A string token for returning a plain string
This @ref StringToken is used to customize
a function to return a plain string.
This is default token type used by
the methods of @ref url_view_base
that return decoded strings.
*/
using return_string = implementation_defined::return_string;
//------------------------------------------------
namespace implementation_defined {
template<class Alloc>
struct append_to_t
: arg
{
using string_type = std::basic_string<
char, std::char_traits<char>,
Alloc>;
using result_type = string_type&;
explicit
append_to_t(
string_type& s) noexcept
: s_(s)
{
}
char*
prepare(std::size_t n) override
{
std::size_t n0 = s_.size();
if(n > s_.max_size() - n0)
urls::detail::throw_length_error();
s_.resize(n0 + n);
return &s_[n0];
}
result_type
result() noexcept
{
return s_;
}
private:
string_type& s_;
};
} // implementation_defined
/** Create a string token for appending to a plain string
This function creates a @ref StringToken
which appends to an existing plain string.
Functions using this token will append
the result to the existing string and
return a reference to it.
@param s The string to append
@return A string token
*/
template<
class Alloc =
std::allocator<char>>
implementation_defined::append_to_t<Alloc>
append_to(
std::basic_string<
char,
std::char_traits<char>,
Alloc>& s)
{
return implementation_defined::append_to_t<Alloc>(s);
}
//------------------------------------------------
namespace implementation_defined {
template<class Alloc>
struct assign_to_t
: arg
{
using string_type = std::basic_string<
char, std::char_traits<char>,
Alloc>;
using result_type = string_type&;
explicit
assign_to_t(
string_type& s) noexcept
: s_(s)
{
}
char*
prepare(std::size_t n) override
{
s_.resize(n);
return &s_[0];
}
result_type
result() noexcept
{
return s_;
}
private:
string_type& s_;
};
} // implementation_defined
/** Create a string token for assigning to a plain string
This function creates a @ref StringToken
which assigns to an existing plain string.
Functions using this token will assign
the result to the existing string and
return a reference to it.
@param s The string to assign
@return A string token
*/
template<
class Alloc =
std::allocator<char>>
implementation_defined::assign_to_t<Alloc>
assign_to(
std::basic_string<
char,
std::char_traits<char>,
Alloc>& s)
{
return implementation_defined::assign_to_t<Alloc>(s);
}
//------------------------------------------------
namespace implementation_defined {
template<class Alloc>
struct preserve_size_t
: arg
{
using result_type = core::string_view;
using string_type = std::basic_string<
char, std::char_traits<char>,
Alloc>;
explicit
preserve_size_t(
string_type& s) noexcept
: s_(s)
{
}
char*
prepare(std::size_t n) override
{
n_ = n;
// preserve size() to
// avoid value-init
if(s_.size() < n)
s_.resize(n);
return &s_[0];
}
result_type
result() noexcept
{
return core::string_view(
s_.data(), n_);
}
private:
string_type& s_;
std::size_t n_ = 0;
};
} // implementation_defined
/** Create a string token for a durable core::string_view
This function creates a @ref StringToken
which assigns to an existing plain string.
Functions using this token will assign
the result to the existing string and
return a `core::string_view` to it.
@param s The string to preserve
@return A string token
*/
template<
class Alloc =
std::allocator<char>>
implementation_defined::preserve_size_t<Alloc>
preserve_size(
std::basic_string<
char,
std::char_traits<char>,
Alloc>& s)
{
return implementation_defined::preserve_size_t<Alloc>(s);
}
} // string_token
namespace grammar {
namespace string_token = ::boost::urls::string_token;
} // grammar
} // urls
} // boost
#endif