libs/capy/src/buffers/circular_dynamic_buffer.cpp

75.8% Lines (25/33) 100.0% Functions (4/4) 66.7% Branches (8/12)
libs/capy/src/buffers/circular_dynamic_buffer.cpp
Line Branch Hits 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/capy
8 //
9
10 #include <boost/capy/buffers/circular_dynamic_buffer.hpp>
11 #include <boost/capy/detail/except.hpp>
12
13 namespace boost {
14 namespace capy {
15
16 auto
17 166 circular_dynamic_buffer::
18 data() const noexcept ->
19 const_buffers_type
20 {
21
1/2
✓ Branch 0 taken 166 times.
✗ Branch 1 not taken.
166 if(in_pos_ + in_len_ <= cap_)
22 return {{
23 166 const_buffer{ base_ + in_pos_, in_len_ },
24 166 const_buffer{ base_, 0} }};
25 return {{
26 const_buffer{ base_ + in_pos_, cap_ - in_pos_},
27 const_buffer{ base_, in_len_- (cap_ - in_pos_)} }};
28 }
29
30 auto
31 248 circular_dynamic_buffer::
32 prepare(std::size_t n) ->
33 mutable_buffers_type
34 {
35 // Buffer is too small for n
36
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 247 times.
248 if(n > cap_ - in_len_)
37 1 detail::throw_length_error();
38
39 247 out_size_ = n;
40 247 auto const pos = (
41 247 in_pos_ + in_len_) % cap_;
42
1/2
✓ Branch 0 taken 247 times.
✗ Branch 1 not taken.
247 if(pos + n <= cap_)
43 return {{
44 247 mutable_buffer{ base_ + pos, n },
45 247 mutable_buffer{ base_, 0 } }};
46 return {{
47 mutable_buffer{ base_ + pos, cap_ - pos },
48 mutable_buffer{ base_, n - (cap_ - pos) } }};
49 }
50
51 void
52 207 circular_dynamic_buffer::
53 commit(
54 std::size_t n) noexcept
55 {
56
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 144 times.
207 if(n < out_size_)
57 63 in_len_ += n;
58 else
59 144 in_len_ += out_size_;
60 207 out_size_ = 0;
61 207 }
62
63 void
64 164 circular_dynamic_buffer::
65 consume(
66 std::size_t n) noexcept
67 {
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 164 times.
164 if(n < in_len_)
69 {
70 in_pos_ = (in_pos_ + n) % cap_;
71 in_len_ -= n;
72 }
73 else
74 {
75 // preserve in_pos_ if there is
76 // a prepared buffer
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 164 times.
164 if(out_size_ != 0)
78 {
79 in_pos_ = (in_pos_ + in_len_) % cap_;
80 in_len_ = 0;
81 }
82 else
83 {
84 // make prepare return a
85 // bigger single buffer
86 164 in_pos_ = 0;
87 164 in_len_ = 0;
88 }
89 }
90 164 }
91
92 } // capy
93 } // boost
94