]>
Commit | Line | Data |
---|---|---|
b32b8144 | 1 | // |
92f5a8d4 | 2 | // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) |
b32b8144 FG |
3 | // |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
7 | // Official repository: https://github.com/boostorg/beast | |
8 | // | |
9 | ||
10 | #ifndef BOOST_BEAST_IMPL_FLAT_STATIC_BUFFER_IPP | |
11 | #define BOOST_BEAST_IMPL_FLAT_STATIC_BUFFER_IPP | |
12 | ||
92f5a8d4 | 13 | #include <boost/beast/core/flat_static_buffer.hpp> |
b32b8144 FG |
14 | #include <boost/throw_exception.hpp> |
15 | #include <algorithm> | |
16 | #include <cstring> | |
17 | #include <iterator> | |
92f5a8d4 | 18 | #include <memory> |
b32b8144 FG |
19 | #include <stdexcept> |
20 | ||
21 | namespace boost { | |
22 | namespace beast { | |
23 | ||
92f5a8d4 | 24 | /* Layout: |
b32b8144 | 25 | |
92f5a8d4 TL |
26 | begin_ in_ out_ last_ end_ |
27 | |<------->|<---------->|<---------->|<------->| | |
28 | | readable | writable | | |
b32b8144 FG |
29 | */ |
30 | ||
b32b8144 FG |
31 | void |
32 | flat_static_buffer_base:: | |
92f5a8d4 | 33 | clear() noexcept |
b32b8144 FG |
34 | { |
35 | in_ = begin_; | |
36 | out_ = begin_; | |
37 | last_ = begin_; | |
38 | } | |
39 | ||
b32b8144 FG |
40 | auto |
41 | flat_static_buffer_base:: | |
92f5a8d4 | 42 | prepare(std::size_t n) -> |
b32b8144 FG |
43 | mutable_buffers_type |
44 | { | |
45 | if(n <= dist(out_, end_)) | |
46 | { | |
47 | last_ = out_ + n; | |
48 | return {out_, n}; | |
49 | } | |
50 | auto const len = size(); | |
51 | if(n > capacity() - len) | |
52 | BOOST_THROW_EXCEPTION(std::length_error{ | |
53 | "buffer overflow"}); | |
54 | if(len > 0) | |
55 | std::memmove(begin_, in_, len); | |
56 | in_ = begin_; | |
57 | out_ = in_ + len; | |
58 | last_ = out_ + n; | |
59 | return {out_, n}; | |
60 | } | |
61 | ||
b32b8144 FG |
62 | void |
63 | flat_static_buffer_base:: | |
92f5a8d4 | 64 | consume(std::size_t n) noexcept |
b32b8144 FG |
65 | { |
66 | if(n >= size()) | |
67 | { | |
68 | in_ = begin_; | |
69 | out_ = in_; | |
70 | return; | |
71 | } | |
72 | in_ += n; | |
73 | } | |
74 | ||
92f5a8d4 TL |
75 | void |
76 | flat_static_buffer_base:: | |
77 | reset(void* p, std::size_t n) noexcept | |
b32b8144 | 78 | { |
92f5a8d4 TL |
79 | begin_ = static_cast<char*>(p); |
80 | in_ = begin_; | |
81 | out_ = begin_; | |
82 | last_ = begin_; | |
83 | end_ = begin_ + n; | |
b32b8144 FG |
84 | } |
85 | ||
86 | } // beast | |
87 | } // boost | |
88 | ||
92f5a8d4 | 89 | #endif |