]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | // |
2 | // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com) | |
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 | ||
13 | #include <boost/beast/core/detail/type_traits.hpp> | |
14 | #include <boost/asio/buffer.hpp> | |
15 | #include <boost/throw_exception.hpp> | |
16 | #include <algorithm> | |
17 | #include <cstring> | |
18 | #include <iterator> | |
19 | #include <stdexcept> | |
20 | ||
21 | namespace boost { | |
22 | namespace beast { | |
23 | ||
24 | /* Memory is laid out thusly: | |
25 | ||
26 | begin_ ..|.. in_ ..|.. out_ ..|.. last_ ..|.. end_ | |
27 | */ | |
28 | ||
29 | inline | |
30 | auto | |
31 | flat_static_buffer_base:: | |
32 | data() const -> | |
33 | const_buffers_type | |
34 | { | |
35 | return {in_, dist(in_, out_)}; | |
36 | } | |
37 | ||
38 | inline | |
39 | void | |
40 | flat_static_buffer_base:: | |
41 | reset() | |
42 | { | |
43 | reset_impl(); | |
44 | } | |
45 | ||
46 | inline | |
47 | auto | |
48 | flat_static_buffer_base:: | |
49 | prepare(std::size_t n) -> | |
50 | mutable_buffers_type | |
51 | { | |
52 | return prepare_impl(n); | |
53 | } | |
54 | ||
55 | inline | |
56 | void | |
57 | flat_static_buffer_base:: | |
58 | reset(void* p, std::size_t n) | |
59 | { | |
60 | reset_impl(p, n); | |
61 | } | |
62 | ||
63 | template<class> | |
64 | void | |
65 | flat_static_buffer_base:: | |
66 | reset_impl() | |
67 | { | |
68 | in_ = begin_; | |
69 | out_ = begin_; | |
70 | last_ = begin_; | |
71 | } | |
72 | ||
73 | template<class> | |
74 | void | |
75 | flat_static_buffer_base:: | |
76 | reset_impl(void* p, std::size_t n) | |
77 | { | |
78 | begin_ = | |
79 | reinterpret_cast<char*>(p); | |
80 | in_ = begin_; | |
81 | out_ = begin_; | |
82 | last_ = begin_; | |
83 | end_ = begin_ + n; | |
84 | } | |
85 | ||
86 | template<class> | |
87 | auto | |
88 | flat_static_buffer_base:: | |
89 | prepare_impl(std::size_t n) -> | |
90 | mutable_buffers_type | |
91 | { | |
92 | if(n <= dist(out_, end_)) | |
93 | { | |
94 | last_ = out_ + n; | |
95 | return {out_, n}; | |
96 | } | |
97 | auto const len = size(); | |
98 | if(n > capacity() - len) | |
99 | BOOST_THROW_EXCEPTION(std::length_error{ | |
100 | "buffer overflow"}); | |
101 | if(len > 0) | |
102 | std::memmove(begin_, in_, len); | |
103 | in_ = begin_; | |
104 | out_ = in_ + len; | |
105 | last_ = out_ + n; | |
106 | return {out_, n}; | |
107 | } | |
108 | ||
109 | template<class> | |
110 | void | |
111 | flat_static_buffer_base:: | |
112 | consume_impl(std::size_t n) | |
113 | { | |
114 | if(n >= size()) | |
115 | { | |
116 | in_ = begin_; | |
117 | out_ = in_; | |
118 | return; | |
119 | } | |
120 | in_ += n; | |
121 | } | |
122 | ||
123 | //------------------------------------------------------------------------------ | |
124 | ||
125 | template<std::size_t N> | |
126 | flat_static_buffer<N>:: | |
127 | flat_static_buffer(flat_static_buffer const& other) | |
128 | : flat_static_buffer_base(buf_, N) | |
129 | { | |
130 | using boost::asio::buffer_copy; | |
131 | this->commit(buffer_copy( | |
132 | this->prepare(other.size()), other.data())); | |
133 | } | |
134 | ||
135 | template<std::size_t N> | |
136 | auto | |
137 | flat_static_buffer<N>:: | |
138 | operator=(flat_static_buffer const& other) -> | |
139 | flat_static_buffer<N>& | |
140 | { | |
141 | using boost::asio::buffer_copy; | |
142 | this->consume(this->size()); | |
143 | this->commit(buffer_copy( | |
144 | this->prepare(other.size()), other.data())); | |
145 | return *this; | |
146 | } | |
147 | ||
148 | } // beast | |
149 | } // boost | |
150 | ||
151 | #endif |