Line | Branch | Exec | Source |
---|---|---|---|
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_CONST_BUFFER_HPP | ||
11 | #define BOOST_BUFFERS_CONST_BUFFER_HPP | ||
12 | |||
13 | #include <boost/buffers/detail/config.hpp> | ||
14 | #include <boost/buffers/mutable_buffer.hpp> | ||
15 | |||
16 | namespace boost { | ||
17 | namespace buffers { | ||
18 | |||
19 | /** Holds a buffer that cannot be modified. | ||
20 | */ | ||
21 | class const_buffer | ||
22 | { | ||
23 | unsigned char const* p_ = nullptr; | ||
24 | std::size_t n_ = 0; | ||
25 | |||
26 | public: | ||
27 | /** Constructor. | ||
28 | */ | ||
29 | 139809 | const_buffer() = default; | |
30 | |||
31 | /** Constructor. | ||
32 | */ | ||
33 | 690360 | const_buffer( | |
34 | void const* data, | ||
35 | std::size_t size) noexcept | ||
36 | 690360 | : p_(static_cast< | |
37 | unsigned char const*>(data)) | ||
38 | 690360 | , n_(size) | |
39 | { | ||
40 | 690360 | } | |
41 | |||
42 | /** Constructor. | ||
43 | */ | ||
44 | const_buffer( | ||
45 | const_buffer const&) = default; | ||
46 | |||
47 | /** Constructor. | ||
48 | */ | ||
49 | 9165 | const_buffer( | |
50 | mutable_buffer const& b) noexcept | ||
51 | 18330 | : p_(static_cast< | |
52 | 9165 | unsigned char const*>(b.data())) | |
53 | 9165 | , n_(b.size()) | |
54 | { | ||
55 | 9165 | } | |
56 | |||
57 | /** Assignment. | ||
58 | */ | ||
59 | const_buffer& operator=( | ||
60 | const_buffer const&) = default; | ||
61 | |||
62 | // conversion to boost::asio::const_buffer | ||
63 | template< | ||
64 | class T | ||
65 | , class = typename std::enable_if< | ||
66 | std::is_constructible<T, | ||
67 | void const*, std::size_t>::value && | ||
68 | ! std::is_same<T, mutable_buffer>::value && | ||
69 | ! std::is_same<T, const_buffer>::value | ||
70 | >::type | ||
71 | > | ||
72 | operator T() const noexcept | ||
73 | { | ||
74 | return T{ data(), size() }; | ||
75 | } | ||
76 | |||
77 | void const* | ||
78 | 1388148 | data() const noexcept | |
79 | { | ||
80 | 1388148 | return p_; | |
81 | } | ||
82 | |||
83 | std::size_t | ||
84 | 5324384 | size() const noexcept | |
85 | { | ||
86 | 5324384 | return n_; | |
87 | } | ||
88 | |||
89 | /** Remove a prefix from the buffer. | ||
90 | */ | ||
91 | const_buffer& | ||
92 | 950181 | operator+=(std::size_t n) noexcept | |
93 | { | ||
94 |
2/2✓ Branch 0 taken 26302 times.
✓ Branch 1 taken 923879 times.
|
950181 | if(n >= n_) |
95 | { | ||
96 | 26302 | p_ = p_ + n_; | |
97 | 26302 | n_ = 0; | |
98 | 26302 | return *this; | |
99 | } | ||
100 | 923879 | p_ = p_ + n; | |
101 | 923879 | n_ -= n; | |
102 | 923879 | return *this; | |
103 | } | ||
104 | |||
105 | /** Return the buffer with a prefix removed. | ||
106 | */ | ||
107 | friend | ||
108 | const_buffer | ||
109 | 950178 | operator+( | |
110 | const_buffer b, | ||
111 | std::size_t n) noexcept | ||
112 | { | ||
113 | 950178 | return b += n; | |
114 | } | ||
115 | |||
116 | /** Return the buffer with a prefix removed. | ||
117 | */ | ||
118 | friend | ||
119 | const_buffer | ||
120 | 1 | operator+( | |
121 | std::size_t n, | ||
122 | const_buffer b) noexcept | ||
123 | { | ||
124 | 1 | return b += n; | |
125 | } | ||
126 | |||
127 | friend | ||
128 | const_buffer | ||
129 | 246621 | tag_invoke( | |
130 | prefix_tag const&, | ||
131 | const_buffer const& b, | ||
132 | std::size_t n) noexcept | ||
133 | { | ||
134 |
2/2✓ Branch 1 taken 81945 times.
✓ Branch 2 taken 164676 times.
|
246621 | if(n < b.size()) |
135 | 81945 | return { b.data(), n }; | |
136 | 164676 | return b; | |
137 | } | ||
138 | |||
139 | friend | ||
140 | const_buffer | ||
141 | 279261 | tag_invoke( | |
142 | suffix_tag const&, | ||
143 | const_buffer const& b, | ||
144 | std::size_t n) noexcept | ||
145 | { | ||
146 | 279261 | auto const n0 = b.size(); | |
147 |
2/2✓ Branch 0 taken 135583 times.
✓ Branch 1 taken 143678 times.
|
279261 | if(n < n0) |
148 | 135583 | return { b.p_ + (n0 - n), n }; | |
149 | 143678 | return b; | |
150 | } | ||
151 | }; | ||
152 | |||
153 | } // buffers | ||
154 | } // boost | ||
155 | |||
156 | #endif | ||
157 |