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