LCOV - code coverage report
Current view: top level - boost/buffers - circular_buffer.hpp (source / functions) Coverage Total Hit
Test: coverage_filtered.info Lines: 100.0 % 17 17
Test Date: 2025-12-06 02:12:43 Functions: 100.0 % 5 5

            Line data    Source code
       1              : //
       2              : // Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com)
       3              : //
       4              : // Distributed under the Boost Software License, Version 1.0. (See accompanying
       5              : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
       6              : //
       7              : // Official repository: https://github.com/cppalliance/buffers
       8              : //
       9              : 
      10              : #ifndef BOOST_BUFFERS_CIRCULAR_BUFFER_HPP
      11              : #define BOOST_BUFFERS_CIRCULAR_BUFFER_HPP
      12              : 
      13              : #include <boost/buffers/detail/config.hpp>
      14              : #include <boost/buffers/buffer_pair.hpp>
      15              : #include <boost/buffers/detail/except.hpp>
      16              : 
      17              : namespace boost {
      18              : namespace buffers {
      19              : 
      20              : /** A circular buffer.
      21              : 
      22              :     This implements a fixed-size, circular
      23              :     buffer. Buffer sequences returned from
      24              :     @ref prepare and @ref data always have
      25              :     length two.
      26              : */
      27              : class circular_buffer
      28              : {
      29              :     unsigned char* base_ = nullptr;
      30              :     std::size_t cap_ = 0;
      31              :     std::size_t in_pos_ = 0;
      32              :     std::size_t in_len_ = 0;
      33              :     std::size_t out_size_ = 0;
      34              : 
      35              : public:
      36              :     /** The ConstBufferSequence used to
      37              :         represent the readable bytes.
      38              :     */
      39              :     using const_buffers_type =
      40              :         const_buffer_pair;
      41              : 
      42              :     /** The MutableBufferSequence used to
      43              :         represent the writable bytes.
      44              :     */
      45              :     using mutable_buffers_type =
      46              :         mutable_buffer_pair;
      47              : 
      48              :     /** Constructor.
      49              :     */
      50              :     circular_buffer() = default;
      51              : 
      52              :     /** Constructor.
      53              :     */
      54              :     circular_buffer(
      55              :         circular_buffer const&) = default;
      56              : 
      57              :     /** Constructor.
      58              :     */
      59         8213 :     circular_buffer(
      60              :         void* base,
      61              :         std::size_t capacity) noexcept
      62         8213 :         : base_(static_cast<
      63              :             unsigned char*>(base))
      64         8213 :         , cap_(capacity)
      65              :     {
      66         8213 :     }
      67              : 
      68              :     /** Constructor.
      69              :     */
      70            2 :     circular_buffer(
      71              :         void* base,
      72              :         std::size_t capacity,
      73              :         std::size_t initial_size)
      74            2 :         : base_(static_cast<
      75              :             unsigned char*>(base))
      76            2 :         , cap_(capacity)
      77            2 :         , in_len_(initial_size)
      78              :     {
      79            2 :         if(in_len_ > capacity)
      80            1 :             detail::throw_invalid_argument();
      81            1 :     }
      82              : 
      83              :     /** Assignment.
      84              :     */
      85              :     circular_buffer& operator=(
      86              :         circular_buffer const&) = default;
      87              : 
      88              :     /** Returns the number of readable bytes.
      89              :     */
      90              :     std::size_t
      91         4119 :     size() const noexcept
      92              :     {
      93         4119 :         return in_len_;
      94              :     }
      95              : 
      96              :     /** Returns the maximum sum of the input and
      97              :         output sequence sizes.
      98              :     */
      99              :     std::size_t
     100         4102 :     max_size() const noexcept
     101              :     {
     102         4102 :         return cap_;
     103              :     }
     104              : 
     105              :     /** Returns the number of writable bytes.
     106              :     */
     107              :     std::size_t
     108         8199 :     capacity() const noexcept
     109              :     {
     110         8199 :         return cap_ - in_len_;
     111              :     }
     112              : 
     113              :     /** Returns a constant buffer sequence representing
     114              :         the readable bytes.
     115              :     */
     116              :     BOOST_BUFFERS_DECL
     117              :     const_buffers_type
     118              :     data() const noexcept;
     119              : 
     120              :     /** Returns a mutable buffer sequence representing
     121              :         the writable bytes.
     122              : 
     123              :         All buffers sequences previously
     124              :         obtained using @ref prepare become
     125              :         invalid.
     126              : 
     127              :         @param n The desired number of bytes in
     128              :         the returned buffer sequence.
     129              : 
     130              :         @throw std::length_error if @ref size() + n
     131              :         exceeds @ref max_size().
     132              :     */
     133              :     BOOST_BUFFERS_DECL
     134              :     mutable_buffers_type
     135              :     prepare(std::size_t n);
     136              : 
     137              :     /** Append writable bytes to the readable bytes.
     138              : 
     139              :         Appends n bytes from the start of the
     140              :         writable bytes to the end of the
     141              :         readable bytes. The remainder of the
     142              :         writable bytes are discarded. If n is
     143              :         greater than the number of writable
     144              :         bytes, all writable bytes are appended
     145              :         to the readable bytes.
     146              : 
     147              :         All buffer sequences previously obtained
     148              :         using @ref prepare are invalidated.
     149              : 
     150              :         Buffer sequences previously obtained
     151              :         using @ref data remain valid.
     152              : 
     153              :         @param n The number of bytes to append. If
     154              :         this number is greater than the number
     155              :         of writable bytes, all writable bytes
     156              :         are appended.
     157              :     */
     158              :     BOOST_BUFFERS_DECL
     159              :     void
     160              :     commit(std::size_t n) noexcept;
     161              : 
     162              :     /** Remove bytes from beginning of the readable bytes.
     163              : 
     164              :         All buffers sequences previously
     165              :         obtained using @ref data are
     166              :         invalidated.
     167              : 
     168              :         Buffer sequences previously obtained
     169              :         using @ref prepare remain valid.
     170              : 
     171              :         @param n The number of bytes to remove.
     172              :         If this number is greater than the
     173              :         number of readable bytes, all readable
     174              :         bytes are removed.
     175              :     */
     176              :     BOOST_BUFFERS_DECL
     177              :     void
     178              :     consume(std::size_t n) noexcept;
     179              : };
     180              : 
     181              : } // buffers
     182              : } // boost
     183              : 
     184              : #endif
        

Generated by: LCOV version 2.1