2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
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)
8 #ifndef BEAST_BUFFERS_ADAPTER_HPP
9 #define BEAST_BUFFERS_ADAPTER_HPP
11 #include <beast/config.hpp>
12 #include <beast/core/buffer_concepts.hpp>
13 #include <boost/asio/buffer.hpp>
14 #include <type_traits>
18 /** Adapts a @b `MutableBufferSequence` into a @b `DynamicBuffer`.
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.
27 The size of the mutable buffer sequence determines the maximum
28 number of bytes which may be prepared and committed.
30 @tparam MutableBufferSequence The type of mutable buffer sequence to wrap.
32 template<class MutableBufferSequence>
35 static_assert(is_MutableBufferSequence<MutableBufferSequence>::value,
36 "MutableBufferSequence requirements not met");
38 using iter_type = typename MutableBufferSequence::const_iterator;
40 MutableBufferSequence bs_;
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
50 template<class Deduced>
51 buffers_adapter(Deduced&& other,
52 std::size_t nbegin, std::size_t nout,
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_)
68 /// The type used to represent the input sequence as a list of buffers.
69 using const_buffers_type = implementation_defined;
71 /// The type used to represent the output sequence as a list of buffers.
72 using mutable_buffers_type = implementation_defined;
75 class const_buffers_type;
77 class mutable_buffers_type;
82 buffers_adapter(buffers_adapter&& other);
85 buffers_adapter(buffers_adapter const& other);
88 buffers_adapter& operator=(buffers_adapter&& other);
91 buffers_adapter& operator=(buffers_adapter const&);
93 /** Construct a buffers adapter.
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
100 buffers_adapter(MutableBufferSequence const& buffers);
102 /// Returns the largest size output sequence possible.
109 /// Get the size of the input sequence.
116 /** Get a list of buffers that represents the output sequence, with the given size.
118 @throws std::length_error if the size would exceed the limit
119 imposed by the underlying mutable buffer sequence.
121 @note Buffers representing the input sequence acquired prior to
122 this call remain valid.
125 prepare(std::size_t n);
127 /** Move bytes from the output sequence to the input sequence.
129 @note Buffers representing the input sequence acquired prior to
130 this call remain valid.
133 commit(std::size_t n);
135 /** Get a list of buffers that represents the input sequence.
137 @note These buffers remain valid across subsequent calls to `prepare`.
142 /// Remove bytes from the input sequence.
144 consume(std::size_t n);
149 #include <beast/core/impl/buffers_adapter.ipp>