]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // |
b32b8144 | 2 | // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com) |
7c673cae 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 | // | |
b32b8144 FG |
7 | // Official repository: https://github.com/boostorg/beast |
8 | // | |
7c673cae | 9 | |
b32b8144 FG |
10 | #ifndef BOOST_BEAST_BUFFERS_ADAPTER_HPP |
11 | #define BOOST_BEAST_BUFFERS_ADAPTER_HPP | |
7c673cae | 12 | |
b32b8144 FG |
13 | #include <boost/beast/core/detail/config.hpp> |
14 | #include <boost/beast/core/type_traits.hpp> | |
7c673cae FG |
15 | #include <boost/asio/buffer.hpp> |
16 | #include <type_traits> | |
17 | ||
b32b8144 | 18 | namespace boost { |
7c673cae FG |
19 | namespace beast { |
20 | ||
b32b8144 | 21 | /** Adapts a @b MutableBufferSequence into a @b DynamicBuffer. |
7c673cae | 22 | |
b32b8144 FG |
23 | This class wraps a @b MutableBufferSequence to meet the requirements |
24 | of @b DynamicBuffer. Upon construction the input and output sequences are | |
7c673cae FG |
25 | empty. A copy of the mutable buffer sequence object is stored; however, |
26 | ownership of the underlying memory is not transferred. The caller is | |
27 | responsible for making sure that referenced memory remains valid | |
28 | for the duration of any operations. | |
29 | ||
30 | The size of the mutable buffer sequence determines the maximum | |
31 | number of bytes which may be prepared and committed. | |
32 | ||
33 | @tparam MutableBufferSequence The type of mutable buffer sequence to wrap. | |
34 | */ | |
35 | template<class MutableBufferSequence> | |
36 | class buffers_adapter | |
37 | { | |
b32b8144 | 38 | static_assert(boost::asio::is_mutable_buffer_sequence<MutableBufferSequence>::value, |
7c673cae FG |
39 | "MutableBufferSequence requirements not met"); |
40 | ||
b32b8144 FG |
41 | using iter_type = typename |
42 | detail::buffer_sequence_iterator< | |
43 | MutableBufferSequence>::type; | |
7c673cae FG |
44 | |
45 | MutableBufferSequence bs_; | |
46 | iter_type begin_; | |
47 | iter_type out_; | |
48 | iter_type end_; | |
49 | std::size_t max_size_; | |
50 | std::size_t in_pos_ = 0; // offset in *begin_ | |
51 | std::size_t in_size_ = 0; // size of input sequence | |
52 | std::size_t out_pos_ = 0; // offset in *out_ | |
53 | std::size_t out_end_ = 0; // output end offset | |
54 | ||
55 | template<class Deduced> | |
56 | buffers_adapter(Deduced&& other, | |
57 | std::size_t nbegin, std::size_t nout, | |
58 | std::size_t nend) | |
59 | : bs_(std::forward<Deduced>(other).bs_) | |
60 | , begin_(std::next(bs_.begin(), nbegin)) | |
61 | , out_(std::next(bs_.begin(), nout)) | |
62 | , end_(std::next(bs_.begin(), nend)) | |
63 | , max_size_(other.max_size_) | |
64 | , in_pos_(other.in_pos_) | |
65 | , in_size_(other.in_size_) | |
66 | , out_pos_(other.out_pos_) | |
67 | , out_end_(other.out_end_) | |
68 | { | |
69 | } | |
70 | ||
71 | public: | |
b32b8144 | 72 | #if BOOST_BEAST_DOXYGEN |
7c673cae FG |
73 | /// The type used to represent the input sequence as a list of buffers. |
74 | using const_buffers_type = implementation_defined; | |
75 | ||
76 | /// The type used to represent the output sequence as a list of buffers. | |
77 | using mutable_buffers_type = implementation_defined; | |
78 | ||
79 | #else | |
80 | class const_buffers_type; | |
81 | ||
82 | class mutable_buffers_type; | |
83 | ||
84 | #endif | |
85 | ||
86 | /// Move constructor. | |
87 | buffers_adapter(buffers_adapter&& other); | |
88 | ||
89 | /// Copy constructor. | |
90 | buffers_adapter(buffers_adapter const& other); | |
91 | ||
92 | /// Move assignment. | |
93 | buffers_adapter& operator=(buffers_adapter&& other); | |
94 | ||
95 | /// Copy assignment. | |
96 | buffers_adapter& operator=(buffers_adapter const&); | |
97 | ||
98 | /** Construct a buffers adapter. | |
99 | ||
100 | @param buffers The mutable buffer sequence to wrap. A copy of | |
101 | the object will be made, but ownership of the memory is not | |
102 | transferred. | |
103 | */ | |
104 | explicit | |
105 | buffers_adapter(MutableBufferSequence const& buffers); | |
106 | ||
107 | /// Returns the largest size output sequence possible. | |
108 | std::size_t | |
109 | max_size() const | |
110 | { | |
111 | return max_size_; | |
112 | } | |
113 | ||
114 | /// Get the size of the input sequence. | |
115 | std::size_t | |
116 | size() const | |
117 | { | |
118 | return in_size_; | |
119 | } | |
b32b8144 FG |
120 | |
121 | /// Returns the maximum sum of the sizes of the input sequence and output sequence the buffer can hold without requiring reallocation. | |
122 | std::size_t | |
123 | capacity() const | |
124 | { | |
125 | return max_size_; | |
126 | } | |
7c673cae FG |
127 | |
128 | /** Get a list of buffers that represents the output sequence, with the given size. | |
129 | ||
130 | @throws std::length_error if the size would exceed the limit | |
131 | imposed by the underlying mutable buffer sequence. | |
132 | ||
133 | @note Buffers representing the input sequence acquired prior to | |
134 | this call remain valid. | |
135 | */ | |
136 | mutable_buffers_type | |
137 | prepare(std::size_t n); | |
138 | ||
139 | /** Move bytes from the output sequence to the input sequence. | |
140 | ||
141 | @note Buffers representing the input sequence acquired prior to | |
142 | this call remain valid. | |
143 | */ | |
144 | void | |
145 | commit(std::size_t n); | |
146 | ||
147 | /** Get a list of buffers that represents the input sequence. | |
148 | ||
149 | @note These buffers remain valid across subsequent calls to `prepare`. | |
150 | */ | |
151 | const_buffers_type | |
152 | data() const; | |
153 | ||
154 | /// Remove bytes from the input sequence. | |
155 | void | |
156 | consume(std::size_t n); | |
157 | }; | |
158 | ||
159 | } // beast | |
b32b8144 | 160 | } // boost |
7c673cae | 161 | |
b32b8144 | 162 | #include <boost/beast/core/impl/buffers_adapter.ipp> |
7c673cae FG |
163 | |
164 | #endif |