|
||||
File indexing completed on 2025-01-30 09:34:04
0001 // 0002 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) 0003 // 0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying 0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 0006 // 0007 // Official repository: https://github.com/boostorg/beast 0008 // 0009 0010 #ifndef BOOST_BEAST_BUFFERS_ADAPTOR_HPP 0011 #define BOOST_BEAST_BUFFERS_ADAPTOR_HPP 0012 0013 #include <boost/beast/core/detail/config.hpp> 0014 #include <boost/beast/core/buffer_traits.hpp> 0015 #include <boost/optional.hpp> 0016 #include <type_traits> 0017 0018 namespace boost { 0019 namespace beast { 0020 0021 /** Adapts a <em>MutableBufferSequence</em> into a <em>DynamicBuffer</em>. 0022 0023 This class wraps a <em>MutableBufferSequence</em> to meet the requirements 0024 of <em>DynamicBuffer</em>. Upon construction the input and output sequences 0025 are empty. A copy of the mutable buffer sequence object is stored; however, 0026 ownership of the underlying memory is not transferred. The caller is 0027 responsible for making sure that referenced memory remains valid 0028 for the duration of any operations. 0029 0030 The size of the mutable buffer sequence determines the maximum 0031 number of bytes which may be prepared and committed. 0032 0033 @tparam MutableBufferSequence The type of mutable buffer sequence to adapt. 0034 */ 0035 template<class MutableBufferSequence> 0036 class buffers_adaptor 0037 { 0038 static_assert(net::is_mutable_buffer_sequence< 0039 MutableBufferSequence>::value, 0040 "MutableBufferSequence type requirements not met"); 0041 0042 using iter_type = 0043 buffers_iterator_type<MutableBufferSequence>; 0044 0045 template<bool> 0046 class subrange; 0047 0048 MutableBufferSequence bs_; 0049 iter_type begin_; 0050 iter_type out_; 0051 iter_type end_; 0052 std::size_t max_size_; 0053 std::size_t in_pos_ = 0; // offset in *begin_ 0054 std::size_t in_size_ = 0; // size of input sequence 0055 std::size_t out_pos_ = 0; // offset in *out_ 0056 std::size_t out_end_ = 0; // output end offset 0057 0058 iter_type end_impl() const; 0059 0060 buffers_adaptor( 0061 buffers_adaptor const& other, 0062 std::size_t nbegin, 0063 std::size_t nout, 0064 std::size_t nend); 0065 0066 public: 0067 /// The type of the underlying mutable buffer sequence 0068 using value_type = MutableBufferSequence; 0069 0070 /** Construct a buffers adaptor. 0071 0072 @param buffers The mutable buffer sequence to wrap. A copy of 0073 the object will be made, but ownership of the memory is not 0074 transferred. 0075 */ 0076 explicit 0077 buffers_adaptor(MutableBufferSequence const& buffers); 0078 0079 /** Constructor 0080 0081 This constructs the buffer adaptor in-place from 0082 a list of arguments. 0083 0084 @param args Arguments forwarded to the buffers constructor. 0085 */ 0086 template<class... Args> 0087 explicit 0088 buffers_adaptor(boost::in_place_init_t, Args&&... args); 0089 0090 /// Copy Constructor 0091 buffers_adaptor(buffers_adaptor const& other); 0092 0093 /// Copy Assignment 0094 buffers_adaptor& operator=(buffers_adaptor const&); 0095 0096 /// Returns the original mutable buffer sequence 0097 value_type const& 0098 value() const 0099 { 0100 return bs_; 0101 } 0102 0103 //-------------------------------------------------------------------------- 0104 0105 #if BOOST_BEAST_DOXYGEN 0106 /// The ConstBufferSequence used to represent the readable bytes. 0107 using const_buffers_type = __implementation_defined__; 0108 0109 /// The MutableBufferSequence used to represent the writable bytes. 0110 using mutable_buffers_type = __implementation_defined__; 0111 0112 #else 0113 using const_buffers_type = subrange<false>; 0114 0115 using mutable_buffers_type = subrange<true>; 0116 #endif 0117 0118 /// Returns the number of readable bytes. 0119 std::size_t 0120 size() const noexcept 0121 { 0122 return in_size_; 0123 } 0124 0125 /// Return the maximum number of bytes, both readable and writable, that can ever be held. 0126 std::size_t 0127 max_size() const noexcept 0128 { 0129 return max_size_; 0130 } 0131 0132 /// Return the maximum number of bytes, both readable and writable, that can be held without requiring an allocation. 0133 std::size_t 0134 capacity() const noexcept 0135 { 0136 return max_size_; 0137 } 0138 0139 /// Returns a constant buffer sequence representing the readable bytes 0140 const_buffers_type 0141 data() const noexcept; 0142 0143 /// Returns a constant buffer sequence representing the readable bytes 0144 const_buffers_type 0145 cdata() const noexcept 0146 { 0147 return data(); 0148 } 0149 0150 /// Returns a mutable buffer sequence representing the readable bytes. 0151 mutable_buffers_type 0152 data() noexcept; 0153 0154 /** Returns a mutable buffer sequence representing writable bytes. 0155 0156 Returns a mutable buffer sequence representing the writable 0157 bytes containing exactly `n` bytes of storage. This function 0158 does not allocate memory. Instead, the storage comes from 0159 the underlying mutable buffer sequence. 0160 0161 All buffer sequences previously obtained using @ref prepare are 0162 invalidated. Buffer sequences previously obtained using @ref data 0163 remain valid. 0164 0165 @param n The desired number of bytes in the returned buffer 0166 sequence. 0167 0168 @throws std::length_error if `size() + n` exceeds `max_size()`. 0169 0170 @esafe 0171 0172 Strong guarantee. 0173 */ 0174 mutable_buffers_type 0175 prepare(std::size_t n); 0176 0177 /** Append writable bytes to the readable bytes. 0178 0179 Appends n bytes from the start of the writable bytes to the 0180 end of the readable bytes. The remainder of the writable bytes 0181 are discarded. If n is greater than the number of writable 0182 bytes, all writable bytes are appended to the readable bytes. 0183 0184 All buffer sequences previously obtained using @ref prepare are 0185 invalidated. Buffer sequences previously obtained using @ref data 0186 remain valid. 0187 0188 @param n The number of bytes to append. If this number 0189 is greater than the number of writable bytes, all 0190 writable bytes are appended. 0191 0192 @esafe 0193 0194 No-throw guarantee. 0195 */ 0196 void 0197 commit(std::size_t n) noexcept; 0198 0199 /** Remove bytes from beginning of the readable bytes. 0200 0201 Removes n bytes from the beginning of the readable bytes. 0202 0203 All buffers sequences previously obtained using 0204 @ref data or @ref prepare are invalidated. 0205 0206 @param n The number of bytes to remove. If this number 0207 is greater than the number of readable bytes, all 0208 readable bytes are removed. 0209 0210 @esafe 0211 0212 No-throw guarantee. 0213 */ 0214 void 0215 consume(std::size_t n) noexcept; 0216 0217 private: 0218 0219 subrange<true> 0220 make_subrange(std::size_t pos, std::size_t n); 0221 0222 subrange<false> 0223 make_subrange(std::size_t pos, std::size_t n) const; 0224 0225 #ifndef BOOST_BEAST_DOXYGEN 0226 friend struct buffers_adaptor_test_hook; 0227 #endif 0228 0229 }; 0230 0231 } // beast 0232 } // boost 0233 0234 #include <boost/beast/core/impl/buffers_adaptor.hpp> 0235 0236 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |