/
buffer.hpp
585 lines (517 loc) · 11.8 KB
/
buffer.hpp
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.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/CPPAlliance/http_proto
//
#ifndef BOOST_HTTP_PROTO_BUFFER_HPP
#define BOOST_HTTP_PROTO_BUFFER_HPP
#include <boost/http_proto/detail/config.hpp>
#include <boost/assert.hpp>
#include <boost/config/workaround.hpp>
#include <boost/type_traits/make_void.hpp>
#include <cstdlib>
#include <cstring>
#include <type_traits>
#include <utility>
namespace boost {
namespace http_proto {
//------------------------------------------------
/** Determine if T is a possibly-const buffer.
*/
#if BOOST_HTTP_PROTO_DOCS
template<bool isConst, class T>
struct is_buffer
: std::integral_constant<bool, ...>{};
#else
template<
bool isConst, class T,
class = void>
struct is_buffer : std::false_type {};
template<
bool isConst, class T>
struct is_buffer<
isConst, T, boost::void_t<decltype(
std::declval<std::size_t&>() =
std::declval<T const&>().size()
),
typename std::enable_if<
std::is_same<
void*, decltype(
std::declval<T const&>().data())
>::value || (
std::is_same<
void const*, decltype(
std::declval<T const&>().data())
>::value && isConst)
>::type
>> : std::is_copy_constructible<T>
{
};
#endif
/** Determine if T is a const buffer.
*/
template<class T>
using is_const_buffer = is_buffer<true, T>;
/** Determine if T is a mutable buffer.
*/
template<class T>
using is_mutable_buffer = is_buffer<false, T>;
//------------------------------------------------
/** Determine if T is a buffer sequence.
*/
#if BOOST_HTTP_PROTO_DOCS
template<bool isConst, class T>
struct is_buffers
: std::integral_constant<bool, ...>{};
#else
template<
bool isConst, class T,
class = void>
struct is_buffers
: is_buffer<isConst, T>
{
};
template<
bool isConst, class T>
struct is_buffers<
isConst, T, boost::void_t<
typename std::enable_if<
is_buffer<
isConst, decltype(
*std::declval<T const&>().begin())
>::value &&
is_buffer<
isConst, decltype(
*std::declval<T const&>().end())
>::value
>::type
>> : std::is_move_constructible<T>
{
};
#endif
/** Determine if T is a const buffers.
*/
template<class T>
using is_const_buffers = is_buffers<true, T>;
/** Determine if T is a mutable buffers.
*/
template<class T>
using is_mutable_buffers = is_buffers<false, T>;
//------------------------------------------------
class mutable_buffer
{
void* p_ = nullptr;
std::size_t n_ = 0;
public:
using const_iterator =
mutable_buffer const*;
mutable_buffer() = default;
mutable_buffer(
mutable_buffer const&) = default;
mutable_buffer& operator=(
mutable_buffer const&) = default;
mutable_buffer(
void* data,
std::size_t size) noexcept
: p_(data)
, n_(size)
{
}
template<
class MutableBuffer
#ifndef BOOST_HTTP_PROTO_DOCS
,class = typename std::enable_if<
is_mutable_buffer<MutableBuffer
>::value>::type
#endif
>
mutable_buffer(
MutableBuffer const& b) noexcept
: p_(b.data())
, n_(b.size())
{
}
void*
data() const noexcept
{
return p_;
}
std::size_t
size() const noexcept
{
return n_;
}
const_iterator
begin() const noexcept
{
return this;
}
const_iterator
end() const noexcept
{
return this + 1;
}
mutable_buffer&
operator+=(std::size_t n) noexcept
{
if(n >= n_)
{
p_ = static_cast<
char*>(p_) + n_;
n_ = 0;
return *this;
}
p_ = static_cast<
char*>(p_) + n;
n_ -= n;
return *this;
}
friend
mutable_buffer
operator+(
mutable_buffer const& b,
std::size_t n) noexcept
{
if(n < b.size())
return {
static_cast<char*>(
b.data()) + n,
b.size() - n };
return {
static_cast<char*>(
b.data()) + b.size(),
0 };
}
friend
mutable_buffer
operator+(
std::size_t n,
mutable_buffer const& b) noexcept
{
if(n < b.size())
return {
static_cast<char*>(
b.data()) + n,
b.size() - n };
return {
static_cast<char*>(
b.data()) + b.size(),
0 };
}
};
//------------------------------------------------
class const_buffer
{
void const* p_ = nullptr;
std::size_t n_ = 0;
public:
using const_iterator =
const_buffer const*;
const_buffer() = default;
const_buffer(
const_buffer const&) = default;
const_buffer& operator=(
const_buffer const&) = default;
const_buffer(
void const* data,
std::size_t size) noexcept
: p_(data)
, n_(size)
{
}
template<
class ConstBuffer
#ifndef BOOST_HTTP_PROTO_DOCS
,class = typename std::enable_if<
is_const_buffer<ConstBuffer
>::value>::type
#endif
>
const_buffer(
ConstBuffer const& b) noexcept
: p_(b.data())
, n_(b.size())
{
}
void const*
data() const noexcept
{
return p_;
}
std::size_t
size() const noexcept
{
return n_;
}
const_iterator
begin() const noexcept
{
return this;
}
const_iterator
end() const noexcept
{
return this + 1;
}
const_buffer&
operator+=(std::size_t n) noexcept
{
if(n >= n_)
{
p_ = static_cast<
char const*>(p_) + n_;
n_ = 0;
return *this;
}
p_ = static_cast<
char const*>(p_) + n;
n_ -= n;
return *this;
}
friend
const_buffer
operator+(
const_buffer const& b,
std::size_t n) noexcept
{
if(n < b.size())
return {
static_cast<char const*>(
b.data()) + n,
b.size() - n };
return {
static_cast<char const*>(
b.data()) + b.size(),
0 };
}
friend
const_buffer
operator+(
std::size_t n,
const_buffer const& b) noexcept
{
if(n < b.size())
return {
static_cast<char const*>(
b.data()) + n,
b.size() - n };
return {
static_cast<char const*>(
b.data()) + b.size(),
0 };
}
};
//------------------------------------------------
#if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
# pragma warning (push)
# pragma warning (disable: 4521) // multiple copy constructors specified
# pragma warning (disable: 4522) // multiple assignment operators specified
#endif
template<bool isConst>
class buffers_pair
{
public:
// VFALCO: This type is public otherwise
// asio::buffers_iterator won't compile.
using value_type = typename
std::conditional<isConst,
const_buffer,
mutable_buffer>::type;
using const_iterator = value_type const*;
buffers_pair() = default;
#if defined(BOOST_HTTP_PROTO_DOCS) || ( \
! BOOST_WORKAROUND(BOOST_MSVC, < 1910))
buffers_pair(
buffers_pair const& other) = default;
buffers_pair& operator=(
buffers_pair const& other) = default;
#else
buffers_pair(
buffers_pair const& other)
: buffers_pair(
*other.begin(),
*(other.begin() + 1))
{
}
buffers_pair&
operator=(buffers_pair const& other)
{
b_[0] = other.b_[0];
b_[1] = other.b_[1];
return *this;
}
#endif
// const pair construction
// from mutable mutable pair
template<
bool isConst_ = isConst,
class = typename std::enable_if<
isConst_>::type>
buffers_pair(
buffers_pair<false> const& other)
: buffers_pair(
other.b_[0],
other.b_[1])
{
}
// const pair assignment
// from mutable mutable pair
template<
bool isConst_ = isConst,
class = typename std::enable_if<
isConst_>::type>
buffers_pair&
operator=(
buffers_pair<false> const& other)
{
b_[0] = other.b_[0];
b_[1] = other.b_[1];
return *this;
}
buffers_pair(
value_type b0,
value_type b1) noexcept
{
if(b0.size() > 0)
{
b_[0] = b0;
b_[1] = b1;
}
else
{
b_[0] = b1;
}
}
const_buffer
operator[](
std::size_t i) const noexcept
{
BOOST_ASSERT(i < 2);
return b_[i];
}
const_iterator
begin() const noexcept
{
return b_;
}
const_iterator
end() const noexcept
{
if(b_[1].size() > 0)
return &b_[2];
if(b_[0].size() > 0)
return &b_[1];
return b_;
}
private:
value_type b_[2];
};
#if BOOST_WORKAROUND(BOOST_MSVC, < 1910)
# pragma warning (pop)
#endif
/** A mutable buffers pair
*/
using mutable_buffers_pair =
buffers_pair<false>;
/** A const buffers pair
*/
using const_buffers_pair =
buffers_pair<true>;
//------------------------------------------------
/** Return the total octets in a buffer sequence
*/
template<
class ConstBuffers
#ifndef BOOST_HTTP_PROTO_DOCS
, class = typename std::enable_if<
is_const_buffers<ConstBuffers>::value
>::type
#endif
>
std::size_t
buffer_size(
ConstBuffers const& buffers) noexcept
{
std::size_t n = 0;
for(const_buffer b : buffers)
n += b.size();
return n;
}
//------------------------------------------------
/** Copy buffer contents
*/
template<
class MutableBuffers,
class ConstBuffers>
std::size_t
buffer_copy(
MutableBuffers const& to,
ConstBuffers const& from,
std::size_t at_most =
std::size_t(-1)) noexcept
{
// If you get a compile error here it
// means that one or both of your types
// do not meet the requirements.
static_assert(
is_mutable_buffers<MutableBuffers>::value &&
is_const_buffers<ConstBuffers>::value,
"Type requirements not met");
std::size_t total = 0;
std::size_t pos0 = 0;
std::size_t pos1 = 0;
auto const end0 = from.end();
auto const end1 = to.end();
auto it0 = from.begin();
auto it1 = to.begin();
while(
total < at_most &&
it0 != end0 &&
it1 != end1)
{
const_buffer b0 =
const_buffer(*it0) + pos0;
mutable_buffer b1 =
mutable_buffer(*it1) + pos1;
std::size_t amount =
[&]
{
std::size_t n = b0.size();
if( n > b1.size())
n = b1.size();
if( n > at_most - total)
n = at_most - total;
std::memcpy(
b1.data(),
b0.data(),
n);
return n;
}();
total += amount;
if(amount == b1.size())
{
++it1;
pos1 = 0;
}
else
{
pos1 += amount;
}
if(amount == b0.size())
{
++it0;
pos0 = 0;
}
else
{
pos0 += amount;
}
}
return total;
}
} // http_proto
} // boost
#endif