Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:28

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_STATIC_BUFFER_HPP
0011 #define BOOST_BEAST_STATIC_BUFFER_HPP
0012 
0013 #include <boost/beast/core/detail/config.hpp>
0014 #include <boost/beast/core/detail/buffers_pair.hpp>
0015 #include <boost/asio/buffer.hpp>
0016 #include <cstddef>
0017 
0018 namespace boost {
0019 namespace beast {
0020 
0021 /** A dynamic buffer providing a fixed size, circular buffer.
0022 
0023     A dynamic buffer encapsulates memory storage that may be
0024     automatically resized as required, where the memory is
0025     divided into two regions: readable bytes followed by
0026     writable bytes. These memory regions are internal to
0027     the dynamic buffer, but direct access to the elements
0028     is provided to permit them to be efficiently used with
0029     I/O operations.
0030 
0031     Objects of this type meet the requirements of <em>DynamicBuffer</em>
0032     and have the following additional properties:
0033 
0034     @li A mutable buffer sequence representing the readable
0035     bytes is returned by @ref data when `this` is non-const.
0036 
0037     @li Buffer sequences representing the readable and writable
0038     bytes, returned by @ref data and @ref prepare, may have
0039     length up to two.
0040 
0041     @li All operations execute in constant time.
0042 
0043     @li Ownership of the underlying storage belongs to the
0044     derived class.
0045 
0046     @note Variables are usually declared using the template class
0047     @ref static_buffer; however, to reduce the number of template
0048     instantiations, objects should be passed `static_buffer_base&`.
0049 
0050     @see static_buffer
0051 */
0052 class static_buffer_base
0053 {
0054     char* begin_;
0055     std::size_t in_off_ = 0;
0056     std::size_t in_size_ = 0;
0057     std::size_t out_size_ = 0;
0058     std::size_t capacity_;
0059 
0060     static_buffer_base(static_buffer_base const& other) = delete;
0061     static_buffer_base& operator=(static_buffer_base const&) = delete;
0062 
0063 public:
0064     /** Constructor
0065 
0066         This creates a dynamic buffer using the provided storage area.
0067 
0068         @param p A pointer to valid storage of at least `n` bytes.
0069 
0070         @param size The number of valid bytes pointed to by `p`.
0071     */
0072     BOOST_BEAST_DECL
0073     static_buffer_base(void* p, std::size_t size) noexcept;
0074 
0075     /** Clear the readable and writable bytes to zero.
0076 
0077         This function causes the readable and writable bytes
0078         to become empty. The capacity is not changed.
0079 
0080         Buffer sequences previously obtained using @ref data or
0081         @ref prepare become invalid.
0082 
0083         @esafe
0084 
0085         No-throw guarantee.
0086     */
0087     BOOST_BEAST_DECL
0088     void
0089     clear() noexcept;
0090 
0091     //--------------------------------------------------------------------------
0092 
0093 #if BOOST_BEAST_DOXYGEN
0094     /// The ConstBufferSequence used to represent the readable bytes.
0095     using const_buffers_type = __implementation_defined__;
0096 
0097     /// The MutableBufferSequence used to represent the writable bytes.
0098     using mutable_buffers_type = __implementation_defined__;
0099 #else
0100     using const_buffers_type   = detail::buffers_pair<false>;
0101 
0102     using mutable_buffers_type = detail::buffers_pair<true>;
0103 #endif
0104 
0105     /// Returns the number of readable bytes.
0106     std::size_t
0107     size() const noexcept
0108     {
0109         return in_size_;
0110     }
0111 
0112     /// Return the maximum number of bytes, both readable and writable, that can ever be held.
0113     std::size_t
0114     max_size() const noexcept
0115     {
0116         return capacity_;
0117     }
0118 
0119     /// Return the maximum number of bytes, both readable and writable, that can be held without requiring an allocation.
0120     std::size_t
0121     capacity() const noexcept
0122     {
0123         return capacity_;
0124     }
0125 
0126     /// Returns a constant buffer sequence representing the readable bytes
0127     BOOST_BEAST_DECL
0128     const_buffers_type
0129     data() const noexcept;
0130 
0131     /// Returns a constant buffer sequence representing the readable bytes
0132     const_buffers_type
0133     cdata() const noexcept
0134     {
0135         return data();
0136     }
0137 
0138     /// Returns a mutable buffer sequence representing the readable bytes
0139     BOOST_BEAST_DECL
0140     mutable_buffers_type
0141     data() noexcept;
0142 
0143     /** Returns a mutable buffer sequence representing writable bytes.
0144 
0145         Returns a mutable buffer sequence representing the writable
0146         bytes containing exactly `n` bytes of storage.
0147 
0148         All buffers sequences previously obtained using
0149         @ref data or @ref prepare may be invalidated.
0150 
0151         @param n The desired number of bytes in the returned buffer
0152         sequence.
0153 
0154         @throws std::length_error if `size() + n` exceeds `max_size()`.
0155 
0156         @esafe
0157 
0158         Strong guarantee.
0159     */
0160     BOOST_BEAST_DECL
0161     mutable_buffers_type
0162     prepare(std::size_t n);
0163 
0164     /** Append writable bytes to the readable bytes.
0165 
0166         Appends n bytes from the start of the writable bytes to the
0167         end of the readable bytes. The remainder of the writable bytes
0168         are discarded. If n is greater than the number of writable
0169         bytes, all writable bytes are appended to the readable bytes.
0170 
0171         All buffers sequences previously obtained using
0172         @ref data or @ref prepare are invalidated.
0173 
0174         @param n The number of bytes to append. If this number
0175         is greater than the number of writable bytes, all
0176         writable bytes are appended.
0177 
0178         @esafe
0179 
0180         No-throw guarantee.
0181     */
0182     BOOST_BEAST_DECL
0183     void
0184     commit(std::size_t n) noexcept;
0185 
0186     /** Remove bytes from beginning of the readable bytes.
0187 
0188         Removes n bytes from the beginning of the readable bytes.
0189 
0190         All buffers sequences previously obtained using
0191         @ref data or @ref prepare are invalidated.
0192 
0193         @param n The number of bytes to remove. If this number
0194         is greater than the number of readable bytes, all
0195         readable bytes are removed.
0196 
0197         @esafe
0198 
0199         No-throw guarantee.
0200     */
0201     BOOST_BEAST_DECL
0202     void
0203     consume(std::size_t n) noexcept;
0204 };
0205 
0206 //------------------------------------------------------------------------------
0207 
0208 /** A dynamic buffer providing a fixed size, circular buffer.
0209 
0210     A dynamic buffer encapsulates memory storage that may be
0211     automatically resized as required, where the memory is
0212     divided into two regions: readable bytes followed by
0213     writable bytes. These memory regions are internal to
0214     the dynamic buffer, but direct access to the elements
0215     is provided to permit them to be efficiently used with
0216     I/O operations.
0217 
0218     Objects of this type meet the requirements of <em>DynamicBuffer</em>
0219     and have the following additional properties:
0220 
0221     @li A mutable buffer sequence representing the readable
0222     bytes is returned by @ref data when `this` is non-const.
0223 
0224     @li Buffer sequences representing the readable and writable
0225     bytes, returned by @ref data and @ref prepare, may have
0226     length up to two.
0227 
0228     @li All operations execute in constant time.
0229 
0230     @tparam N The number of bytes in the internal buffer.
0231 
0232     @note To reduce the number of template instantiations when passing
0233     objects of this type in a deduced context, the signature of the
0234     receiving function should use @ref static_buffer_base instead.
0235 
0236     @see static_buffer_base
0237 */
0238 template<std::size_t N>
0239 class static_buffer : public static_buffer_base
0240 {
0241     char buf_[N];
0242 
0243 public:
0244     /// Constructor
0245     static_buffer() noexcept
0246         : static_buffer_base(buf_, N)
0247     {
0248     }
0249 
0250     /// Constructor
0251     static_buffer(static_buffer const&) noexcept;
0252 
0253     /// Assignment
0254     static_buffer& operator=(static_buffer const&) noexcept;
0255 
0256     /// Returns the @ref static_buffer_base portion of this object
0257     static_buffer_base&
0258     base() noexcept
0259     {
0260         return *this;
0261     }
0262 
0263     /// Returns the @ref static_buffer_base portion of this object
0264     static_buffer_base const&
0265     base() const noexcept
0266     {
0267         return *this;
0268     }
0269 
0270     /// Return the maximum sum of the input and output sequence sizes.
0271     std::size_t constexpr
0272     max_size() const noexcept
0273     {
0274         return N;
0275     }
0276 
0277     /// Return the maximum sum of input and output sizes that can be held without an allocation.
0278     std::size_t constexpr
0279     capacity() const noexcept
0280     {
0281         return N;
0282     }
0283 };
0284 
0285 } // beast
0286 } // boost
0287 
0288 #include <boost/beast/core/impl/static_buffer.hpp>
0289 #ifdef BOOST_BEAST_HEADER_ONLY
0290 #include <boost/beast/core/impl/static_buffer.ipp>
0291 #endif
0292 
0293 #endif