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