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: 2024-07-24 21:34:17 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/const_buffer_pair.hpp>
      15              : #include <boost/buffers/mutable_buffer_pair.hpp>
      16              : #include <boost/buffers/detail/except.hpp>
      17              : 
      18              : namespace boost {
      19              : namespace buffers {
      20              : 
      21              : /** A circular buffer.
      22              : 
      23              :     This implements a fixed-size, circular
      24              :     buffer. Buffer sequences returned from
      25              :     @ref prepare and @ref data always have
      26              :     length two.
      27              : */
      28              : class circular_buffer
      29              : {
      30              :     unsigned char* base_ = nullptr;
      31              :     std::size_t cap_ = 0;
      32              :     std::size_t in_pos_ = 0;
      33              :     std::size_t in_len_ = 0;
      34              :     std::size_t out_size_ = 0;
      35              : 
      36              : public:
      37              :     using const_buffers_type =
      38              :         const_buffer_pair;
      39              : 
      40              :     using mutable_buffers_type =
      41              :         mutable_buffer_pair;
      42              : 
      43              :     /** Constructor.
      44              :     */
      45              :     circular_buffer() = default;
      46              : 
      47              :     /** Constructor.
      48              :     */
      49              :     circular_buffer(
      50              :         circular_buffer const&) = default;
      51              : 
      52              : #if 0
      53              :     /** Constructor.
      54              :     */
      55              :     explicit
      56              :     circular_buffer(
      57              :         mutable_buffer b) noexcept
      58              :         : base_(static_cast<
      59              :             unsigned char*>(b.data()))
      60              :         , cap_(b.size())
      61              :     {
      62              :     }
      63              : #endif
      64              : 
      65              :     /** Constructor.
      66              :     */
      67         8197 :     circular_buffer(
      68              :         void* base,
      69              :         std::size_t capacity) noexcept
      70         8197 :         : base_(static_cast<
      71              :             unsigned char*>(base))
      72         8197 :         , cap_(capacity)
      73              :     {
      74         8197 :     }
      75              : 
      76              :     /** Constructor.
      77              :     */
      78            2 :     circular_buffer(
      79              :         void* base,
      80              :         std::size_t capacity,
      81              :         std::size_t initial_size)
      82            2 :         : base_(static_cast<
      83              :             unsigned char*>(base))
      84            2 :         , cap_(capacity)
      85            2 :         , in_len_(initial_size)
      86              :     {
      87            2 :         if(in_len_ > capacity)
      88            1 :             detail::throw_invalid_argument();
      89            1 :     }
      90              : 
      91              :     /** Assignment.
      92              :     */
      93              :     circular_buffer& operator=(
      94              :         circular_buffer const&) = default;
      95              : 
      96              :     std::size_t
      97         4103 :     size() const noexcept
      98              :     {
      99         4103 :         return in_len_;
     100              :     }
     101              : 
     102              :     std::size_t
     103         4102 :     max_size() const noexcept
     104              :     {
     105         4102 :         return cap_;
     106              :     }
     107              :     
     108              :     std::size_t
     109         8199 :     capacity() const noexcept
     110              :     {
     111         8199 :         return cap_ - in_len_;
     112              :     }
     113              : 
     114              :     BOOST_BUFFERS_DECL
     115              :     const_buffers_type
     116              :     data() const noexcept;
     117              : 
     118              :     BOOST_BUFFERS_DECL
     119              :     mutable_buffers_type
     120              :     prepare(std::size_t n);
     121              : 
     122              :     BOOST_BUFFERS_DECL
     123              :     void
     124              :     commit(std::size_t n) noexcept;
     125              : 
     126              :     BOOST_BUFFERS_DECL
     127              :     void
     128              :     consume(std::size_t n) noexcept;
     129              : };
     130              : 
     131              : } // buffers
     132              : } // boost
     133              : 
     134              : #endif
        

Generated by: LCOV version 2.1