GCC Code Coverage Report


Directory: libs/http_proto/
File: include/boost/http_proto/source.hpp
Date: 2025-06-14 21:08:33
Exec Total Coverage
Lines: 6 6 100.0%
Functions: 5 5 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 //
2 // Copyright (c) 2019 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/http_proto
8 //
9
10 #ifndef BOOST_HTTP_PROTO_SOURCE_HPP
11 #define BOOST_HTTP_PROTO_SOURCE_HPP
12
13 #include <boost/http_proto/detail/config.hpp>
14 #include <boost/buffers/mutable_buffer_span.hpp>
15 #include <boost/buffers/type_traits.hpp>
16 #include <boost/system/error_code.hpp>
17 #include <cstddef>
18 #include <type_traits>
19
20 namespace boost {
21 namespace http_proto {
22
23 /** An algorithm for producing buffers of data.
24
25 This interface abstracts the production of
26 a finite stream of data, returned by writing
27 into caller-provided buffers until there
28 is no more output data.
29
30 @par Thread Safety
31 Non-const member functions may not be
32 called concurrently on the same instance.
33 */
34 struct BOOST_SYMBOL_VISIBLE
35 source
36 {
37 /** The results of producing data.
38 */
39 struct results
40 {
41 /** The error, if any occurred.
42 */
43 system::error_code ec;
44
45 /** The number of bytes produced in the output.
46 */
47 std::size_t bytes = 0;
48
49 /** True if there will be no more output.
50 */
51 bool finished = false;
52
53 /** Accumulate results.
54 */
55 results&
56 operator+=(
57 results const& rv) noexcept;
58 };
59
60 /** Produce data.
61
62 This function attempts to read from the
63 source, placing the data into the given
64 mutable buffer sequence.
65 The return value indicates the number of
66 bytes placed into the buffers, the error
67 if any occurred, and a `bool` indicating
68 whether or not there is more data
69 remaining in the source.
70
71 @par Preconditions
72 @li @ref init was called, and
73 @li There is more data remaining.
74
75 @return The result of the operation.
76
77 @param bs The buffers to use.
78 Each buffer in the sequence will
79 be filled completely before data
80 is placed in the next buffer.
81 */
82 template<class MutableBufferSequence>
83 results
84 5512 read(MutableBufferSequence const& bs)
85 {
86 static_assert(
87 buffers::is_mutable_buffer_sequence<
88 MutableBufferSequence>::value,
89 "Type requirements not met");
90
91 5512 return read_impl(bs);
92 }
93
94 #ifdef BOOST_HTTP_PROTO_DOCS
95 protected:
96 #else
97 private:
98 #endif
99 /** Derived class override.
100
101 This pure virtual function is called by
102 the implementation and must be overriden.
103 The callee should attempt to place data
104 into the given mutable buffer.
105 The return value must be set to indicate
106 the number of bytes placed into the
107 buffers, the error if any occurred,
108 and a `bool` indicating whether or
109 not there is more data remaining
110 in the source.
111
112 @par Preconditions
113 @li @ref init was called, and
114 @li There is more data remaining.
115
116 @return The result of the operation.
117
118 @param b The buffer to use.
119 If this is not filled completely,
120 then the result must indicate failure
121 or that no more data remains (or both).
122 */
123 virtual
124 results
125 on_read(
126 buffers::mutable_buffer b) = 0;
127
128 /** Derived class override.
129
130 This pure virtual function is called by
131 the implementation and must be overriden.
132 The callee should attempt to place data
133 into the given mutable buffer sequence.
134 The return value must be set to indicate
135 the number of bytes placed into the
136 buffers, the error if any occurred,
137 and a `bool` indicating whether or
138 not there is more data remaining
139 in the source.
140
141 @par Preconditions
142 @li @ref init was called, and
143 @li There is more data remaining.
144
145 @return The result of the operation.
146
147 @param bs The buffer sequence to use.
148 Each buffer in the sequence must
149 be filled completely before data
150 is placed in the next buffer.
151 If the buffers are not filled
152 completely, then the result must
153 indicate failure or that no more
154 data remains (or both).
155 */
156 BOOST_HTTP_PROTO_DECL
157 virtual
158 results
159 on_read(
160 buffers::mutable_buffer_span bs);
161
162 private:
163 results
164 2 read_impl(
165 buffers::mutable_buffer const& b)
166 {
167 2 return on_read(b);
168 }
169
170 results
171 5 read_impl(
172 buffers::mutable_buffer_span const& bs)
173 {
174 5 return on_read(bs);
175 }
176
177 template<class T>
178 results
179 read_impl(T const&);
180 };
181
182 //------------------------------------------------
183
184 /** Metafunction which determines if T is a source
185
186 @see
187 @ref source.
188 */
189 #ifdef BOOST_HTTP_PROTO_DOCS
190 template<class T>
191 using is_source = __see_below__;
192 #else
193 template<class T>
194 using is_source =
195 std::is_convertible<
196 typename std::decay<T>::type*,
197 source*>;
198 #endif
199
200 } // http_proto
201 } // boost
202
203 #include <boost/http_proto/impl/source.hpp>
204
205 #endif
206