|
||||
File indexing completed on 2025-01-18 09:29:27
0001 // 0002 // Copyright (c) 2022 Klemens D. Morgenstern (klemens dot morgenstern at gmx dot net) 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 #ifndef BOOST_BEAST_BUFFER_REF_HPP 0007 #define BOOST_BEAST_BUFFER_REF_HPP 0008 0009 #include <cstddef> 0010 0011 namespace boost { 0012 namespace beast { 0013 0014 #if !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1) 0015 0016 /** The buffer ref provides a wrapper around beast buffers 0017 * to make them usable with asio dynamic_buffer v1. 0018 * 0019 * v2 is current not supported, so that 0020 * `BOOST_ASIO_NO_DYNAMIC_BUFFER_V1` mustn't be defined. 0021 * 0022 * @par Example 0023 * 0024 * @code 0025 * 0026 * asio::tcp::socket sock; 0027 * beast::flat_buffer fb; 0028 * asio::read_until(sock, ref(fb) '\n'); 0029 * 0030 * @endcode 0031 * 0032 * @tparam Buffer The underlying buffer 0033 */ 0034 template<typename Buffer> 0035 struct buffer_ref 0036 { 0037 /// The ConstBufferSequence used to represent the readable bytes. 0038 using const_buffers_type = typename Buffer::const_buffers_type; 0039 0040 /// The MutableBufferSequence used to represent the writable bytes. 0041 using mutable_buffers_type = typename Buffer::mutable_buffers_type; 0042 0043 /// Returns the number of readable bytes. 0044 std::size_t 0045 size() const noexcept 0046 { 0047 return buffer_.size(); 0048 } 0049 0050 /// Return the maximum number of bytes, both readable and writable, that can ever be held. 0051 std::size_t 0052 max_size() const noexcept 0053 { 0054 return buffer_.max_size(); 0055 } 0056 0057 /// Return the maximum number of bytes, both readable and writable, that can be held without requiring an allocation. 0058 std::size_t 0059 capacity() const noexcept 0060 { 0061 return buffer_.capacity(); 0062 } 0063 0064 /// Returns a constant buffer sequence representing the readable bytes 0065 const_buffers_type 0066 data() const noexcept 0067 { 0068 return buffer_.data(); 0069 } 0070 0071 /// Get a list of buffers that represents the output 0072 /// sequence, with the given size. 0073 /** 0074 * Ensures that the output sequence can accommodate @c n bytes, resizing the 0075 * vector object as necessary. 0076 * 0077 * @returns An object of type @c mutable_buffers_type that satisfies 0078 * MutableBufferSequence requirements, representing vector memory at the 0079 * start of the output sequence of size @c n. 0080 * 0081 * @throws std::length_error If <tt>size() + n > max_size()</tt>. 0082 * 0083 * @note The returned object is invalidated by any @c dynamic_vector_buffer 0084 * or @c vector member function that modifies the input sequence or output 0085 * sequence. 0086 */ 0087 mutable_buffers_type prepare(std::size_t n) 0088 { 0089 return buffer_.prepare(n); 0090 } 0091 0092 /// Move bytes from the output sequence to the input 0093 /// sequence. 0094 /** 0095 * @param n The number of bytes to append from the start of the output 0096 * sequence to the end of the input sequence. The remainder of the output 0097 * sequence is discarded. 0098 * 0099 * Requires a preceding call <tt>prepare(x)</tt> where <tt>x >= n</tt>, and 0100 * no intervening operations that modify the input or output sequence. 0101 * 0102 * @note If @c n is greater than the size of the output sequence, the entire 0103 * output sequence is moved to the input sequence and no error is issued. 0104 */ 0105 void commit(std::size_t n) 0106 { 0107 return buffer_.commit(n); 0108 } 0109 0110 /// Remove `n` bytes from the readable byte sequence. 0111 /** 0112 * @b DynamicBuffer_v1: Removes @c n characters from the beginning of the 0113 * input sequence. @note If @c n is greater than the size of the input 0114 * sequence, the entire input sequence is consumed and no error is issued. 0115 */ 0116 void consume(std::size_t n) 0117 { 0118 return buffer_.consume(n); 0119 } 0120 0121 /// The type of the underlying buffer. 0122 using buffer_type = Buffer; 0123 0124 /// Create a buffer reference around @c buffer. 0125 buffer_ref(Buffer & buffer) : buffer_(buffer) {} 0126 0127 /// Copy the reference. 0128 buffer_ref(const buffer_ref& buffer) = default; 0129 0130 private: 0131 Buffer &buffer_; 0132 }; 0133 0134 0135 template<class Allocator> 0136 class basic_flat_buffer; 0137 template<std::size_t N> 0138 class flat_static_buffer; 0139 template<class Allocator> 0140 class basic_multi_buffer; 0141 template<std::size_t N> 0142 class static_buffer; 0143 0144 /// Create a buffer_ref for basic_flat_buffer. 0145 template<class Allocator> 0146 inline buffer_ref<basic_flat_buffer<Allocator>> ref(basic_flat_buffer<Allocator> & buf) 0147 { 0148 return buffer_ref<basic_flat_buffer<Allocator>>(buf); 0149 } 0150 0151 /// Create a buffer_ref for flat_static_buffer. 0152 template<std::size_t N> 0153 inline buffer_ref<flat_static_buffer<N>> ref(flat_static_buffer<N> & buf) 0154 { 0155 return buffer_ref<flat_static_buffer<N>>(buf); 0156 } 0157 0158 /// Create a buffer_ref for basic_multi_buffer. 0159 template<class Allocator> 0160 inline buffer_ref<basic_multi_buffer<Allocator>> ref(basic_multi_buffer<Allocator> & buf) 0161 { 0162 return buffer_ref<basic_multi_buffer<Allocator>>(buf); 0163 } 0164 0165 /// Create a buffer_ref for static_buffer. 0166 template<std::size_t N> 0167 inline buffer_ref<static_buffer<N>> ref(static_buffer<N> & buf) 0168 { 0169 return buffer_ref<static_buffer<N>>(buf); 0170 } 0171 0172 #endif // !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1) 0173 0174 } 0175 } 0176 0177 #endif //BOOST_BEAST_BUFFER_REF_HPP
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |