Line data Source code
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_HTTP_PROTO_DECL
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 5501 : read(MutableBufferSequence const& bs)
85 : {
86 : static_assert(
87 : buffers::is_mutable_buffer_sequence<
88 : MutableBufferSequence>::value,
89 : "Type requirements not met");
90 :
91 5501 : 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 : virtual
157 : results
158 : on_read(
159 : buffers::mutable_buffer_span bs);
160 :
161 : private:
162 : results
163 2 : read_impl(
164 : buffers::mutable_buffer const& b)
165 : {
166 2 : return on_read(b);
167 : }
168 :
169 : results
170 5 : read_impl(
171 : buffers::mutable_buffer_span const& bs)
172 : {
173 5 : return on_read(bs);
174 : }
175 :
176 : template<class T>
177 : results
178 : read_impl(T const&);
179 : };
180 :
181 : //------------------------------------------------
182 :
183 : /** Metafunction which determines if T is a source
184 :
185 : @see
186 : @ref source.
187 : */
188 : #ifdef BOOST_HTTP_PROTO_DOCS
189 : template<class T>
190 : using is_source = __see_below__;
191 : #else
192 : template<class T>
193 : using is_source =
194 : std::is_convertible<
195 : typename std::decay<T>::type*,
196 : source*>;
197 : #endif
198 :
199 : } // http_proto
200 : } // boost
201 :
202 : #include <boost/http_proto/impl/source.hpp>
203 :
204 : #endif
|