|
||||
File indexing completed on 2025-01-18 09:29:27
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_PREFIX_HPP 0011 #define BOOST_BEAST_BUFFERS_PREFIX_HPP 0012 0013 #include <boost/beast/core/detail/config.hpp> 0014 #include <boost/beast/core/buffer_traits.hpp> 0015 #include <boost/optional/optional.hpp> // for in_place_init_t 0016 #include <algorithm> 0017 #include <cstdint> 0018 #include <type_traits> 0019 0020 #if BOOST_WORKAROUND(BOOST_MSVC, < 1910) 0021 #include <boost/type_traits/is_convertible.hpp> 0022 #endif 0023 0024 namespace boost { 0025 namespace beast { 0026 0027 /** A buffer sequence adaptor that shortens the sequence size. 0028 0029 The class adapts a buffer sequence to efficiently represent 0030 a shorter subset of the original list of buffers starting 0031 with the first byte of the original sequence. 0032 0033 @tparam BufferSequence The buffer sequence to adapt. 0034 */ 0035 template<class BufferSequence> 0036 class buffers_prefix_view 0037 { 0038 using iter_type = 0039 buffers_iterator_type<BufferSequence>; 0040 0041 BufferSequence bs_; 0042 std::size_t size_ = 0; 0043 std::size_t remain_ = 0; 0044 iter_type end_{}; 0045 0046 void 0047 setup(std::size_t size); 0048 0049 buffers_prefix_view( 0050 buffers_prefix_view const& other, 0051 std::size_t dist); 0052 0053 public: 0054 /** The type for each element in the list of buffers. 0055 0056 If the type `BufferSequence` meets the requirements of 0057 <em>MutableBufferSequence</em>, then `value_type` is 0058 `net::mutable_buffer`. Otherwise, `value_type` is 0059 `net::const_buffer`. 0060 0061 @see buffers_type 0062 */ 0063 #if BOOST_BEAST_DOXYGEN 0064 using value_type = __see_below__; 0065 #elif BOOST_WORKAROUND(BOOST_MSVC, < 1910) 0066 using value_type = typename std::conditional< 0067 boost::is_convertible<typename 0068 std::iterator_traits<iter_type>::value_type, 0069 net::mutable_buffer>::value, 0070 net::mutable_buffer, 0071 net::const_buffer>::type; 0072 #else 0073 using value_type = buffers_type<BufferSequence>; 0074 #endif 0075 0076 #if BOOST_BEAST_DOXYGEN 0077 /// A bidirectional iterator type that may be used to read elements. 0078 using const_iterator = __implementation_defined__; 0079 0080 #else 0081 class const_iterator; 0082 0083 #endif 0084 0085 /// Copy Constructor 0086 buffers_prefix_view(buffers_prefix_view const&); 0087 0088 /// Copy Assignment 0089 buffers_prefix_view& operator=(buffers_prefix_view const&); 0090 0091 /** Construct a buffer sequence prefix. 0092 0093 @param size The maximum number of bytes in the prefix. 0094 If this is larger than the size of passed buffers, 0095 the resulting sequence will represent the entire 0096 input sequence. 0097 0098 @param buffers The buffer sequence to adapt. A copy of 0099 the sequence will be made, but ownership of the underlying 0100 memory is not transferred. The copy is maintained for 0101 the lifetime of the view. 0102 */ 0103 buffers_prefix_view( 0104 std::size_t size, 0105 BufferSequence const& buffers); 0106 0107 /** Construct a buffer sequence prefix in-place. 0108 0109 @param size The maximum number of bytes in the prefix. 0110 If this is larger than the size of passed buffers, 0111 the resulting sequence will represent the entire 0112 input sequence. 0113 0114 @param args Arguments forwarded to the contained buffer's constructor. 0115 */ 0116 template<class... Args> 0117 buffers_prefix_view( 0118 std::size_t size, 0119 boost::in_place_init_t, 0120 Args&&... args); 0121 0122 /// Returns an iterator to the first buffer in the sequence 0123 const_iterator 0124 begin() const; 0125 0126 /// Returns an iterator to one past the last buffer in the sequence 0127 const_iterator 0128 end() const; 0129 0130 #if ! BOOST_BEAST_DOXYGEN 0131 std::size_t 0132 buffer_bytes_impl() const noexcept 0133 { 0134 return size_; 0135 } 0136 #endif 0137 }; 0138 0139 //------------------------------------------------------------------------------ 0140 0141 /** Returns a prefix of a constant or mutable buffer sequence. 0142 0143 The returned buffer sequence points to the same memory as the 0144 passed buffer sequence, but with a size that is equal to or 0145 smaller. No memory allocations are performed; the resulting 0146 sequence is calculated as a lazy range. 0147 0148 @param size The maximum size of the returned buffer sequence 0149 in bytes. If this is greater than or equal to the size of 0150 the passed buffer sequence, the result will have the same 0151 size as the original buffer sequence. 0152 0153 @param buffers An object whose type meets the requirements 0154 of <em>BufferSequence</em>. The returned value will 0155 maintain a copy of the passed buffers for its lifetime; 0156 however, ownership of the underlying memory is not 0157 transferred. 0158 0159 @return A constant buffer sequence that represents the prefix 0160 of the original buffer sequence. If the original buffer sequence 0161 also meets the requirements of <em>MutableBufferSequence</em>, 0162 then the returned value will also be a mutable buffer sequence. 0163 */ 0164 template<class BufferSequence> 0165 buffers_prefix_view<BufferSequence> 0166 buffers_prefix( 0167 std::size_t size, BufferSequence const& buffers) 0168 { 0169 static_assert( 0170 net::is_const_buffer_sequence<BufferSequence>::value, 0171 "BufferSequence type requirements not met"); 0172 return buffers_prefix_view<BufferSequence>(size, buffers); 0173 } 0174 0175 /** Returns the first buffer in a buffer sequence 0176 0177 This returns the first buffer in the buffer sequence. 0178 If the buffer sequence is an empty range, the returned 0179 buffer will have a zero buffer size. 0180 0181 @param buffers The buffer sequence. If the sequence is 0182 mutable, the returned buffer sequence will also be mutable. 0183 Otherwise, the returned buffer sequence will be constant. 0184 */ 0185 template<class BufferSequence> 0186 buffers_type<BufferSequence> 0187 buffers_front(BufferSequence const& buffers) 0188 { 0189 auto const first = 0190 net::buffer_sequence_begin(buffers); 0191 if(first == net::buffer_sequence_end(buffers)) 0192 return {}; 0193 return *first; 0194 } 0195 0196 } // beast 0197 } // boost 0198 0199 #include <boost/beast/core/impl/buffers_prefix.hpp> 0200 0201 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |