GCC Code Coverage Report


Directory: libs/http_proto/
File: boost/http_proto/fields_view_base.hpp
Date: 2024-08-25 18:42:42
Exec Total Coverage
Lines: 10 10 100.0%
Functions: 4 4 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 //
2 // Copyright (c) 2021 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_FIELDS_VIEW_BASE_HPP
11 #define BOOST_HTTP_PROTO_FIELDS_VIEW_BASE_HPP
12
13 #include <boost/http_proto/detail/config.hpp>
14 #include <boost/http_proto/detail/header.hpp>
15 #include <boost/url/grammar/recycled.hpp>
16 #include <boost/url/grammar/type_traits.hpp>
17 #include <boost/core/detail/string_view.hpp>
18 #include <iterator>
19 #include <memory>
20 #include <string>
21
22 namespace boost {
23 namespace http_proto {
24
25 /** A read-only, bidirectional range of HTTP fields
26
27 This is a mix-in used to add common
28 functionality to derived classes.
29 */
30 class fields_view_base
31 {
32 detail::header const* ph_;
33
34 friend class fields;
35 friend class fields_base;
36 friend class fields_view;
37 friend class message_base;
38 friend class message_view_base;
39 friend class request;
40 friend class request_view;
41 friend class response;
42 friend class response_view;
43 friend class serializer;
44
45 explicit
46 1151 fields_view_base(
47 detail::header const* ph) noexcept
48 1151 : ph_(ph)
49 {
50 1151 }
51
52 fields_view_base(
53 fields_view_base const&) = default;
54 fields_view_base&
55 operator=(fields_view_base const&) = default;
56
57 public:
58 //--------------------------------------------
59 //
60 // Types
61 //
62 //--------------------------------------------
63
64 /** A field
65 */
66 /**@{*/
67 struct reference
68 {
69 field const id;
70 core::string_view const name;
71 core::string_view const value;
72
73 #ifndef BOOST_HTTP_PROTO_DOCS
74 reference const*
75 1861 operator->() const noexcept
76 {
77 1861 return this;
78 }
79 #endif
80 };
81
82 typedef reference const_reference;
83 /**@}*/
84
85 /** A type which can represent a field as a value
86
87 This type allows for making a copy of
88 a field where ownership is retained
89 in the copy.
90 */
91 struct value_type
92 {
93 field id;
94 std::string name;
95 std::string value;
96
97 BOOST_HTTP_PROTO_DECL
98 value_type(
99 reference const& other);
100
101 operator reference() const noexcept;
102 };
103
104 /** An unsigned integer type
105 */
106 using size_type = std::size_t;
107
108 /** A signed integer type
109 */
110 using difference_type =
111 std::ptrdiff_t;
112
113 /** A bidirectional iterator to HTTP fields
114 */
115 /**@{*/
116 #ifdef BOOST_HTTP_PROTO_DOCS
117 using iterator = __see_below__;
118 #else
119 class iterator;
120 #endif
121
122 using const_iterator = iterator;
123 /**@}*/
124
125 /** A bidirectional reverse iterator to HTTP fields
126 */
127 /**@{*/
128 #ifdef BOOST_HTTP_PROTO_DOCS
129 using reverse_iterator = __see_below__;
130 #else
131 class reverse_iterator;
132 #endif
133
134 using const_reverse_iterator = reverse_iterator;
135 /**@}*/
136
137 /** A forward range of matching fields
138
139 Objects of this type are returned by
140 the function @ref find_all.
141 */
142 #ifdef BOOST_HTTP_PROTO_DOCS
143 using subrange = __see_below__;
144 #else
145 class subrange;
146 #endif
147
148 //--------------------------------------------
149 //
150 // Observers
151 //
152 //--------------------------------------------
153
154 /** Returns the largest possible serialized message
155 */
156 static
157 constexpr
158 std::size_t
159 max_size() noexcept
160 {
161 return max_offset;
162 }
163
164 /** Return an iterator to the beginning
165 */
166 iterator
167 begin() const noexcept;
168
169 /** Return an iterator to the end
170 */
171 iterator
172 end() const noexcept;
173
174 /** Return a reverse iterator to the beginning
175 */
176 reverse_iterator
177 rbegin() const noexcept;
178
179 /** Return a reverse iterator to the end
180 */
181 reverse_iterator
182 rend() const noexcept;
183
184 //---
185
186 /** Return a string representing the serialized data
187 */
188 core::string_view
189 610 buffer() const noexcept
190 {
191 1220 return core::string_view(
192 610 ph_->cbuf, ph_->size);
193 }
194
195 /** Returns the number of fields in the container
196 */
197 std::size_t
198 184 size() const noexcept
199 {
200 184 return ph_->count;
201 }
202
203 /** Return true if a field exists
204 */
205 BOOST_HTTP_PROTO_DECL
206 bool
207 exists(field id) const noexcept;
208
209 /** Return true if a field exists
210 */
211 BOOST_HTTP_PROTO_DECL
212 bool
213 exists(
214 core::string_view name) const noexcept;
215
216 /** Return the number of matching fields
217 */
218 BOOST_HTTP_PROTO_DECL
219 std::size_t
220 count(field id) const noexcept;
221
222 /** Return the number of matching fields
223 */
224 BOOST_HTTP_PROTO_DECL
225 std::size_t
226 count(
227 core::string_view name) const noexcept;
228
229 /** Returns an iterator to the matching element if it exists
230 */
231 BOOST_HTTP_PROTO_DECL
232 iterator
233 find(field id) const noexcept;
234
235 /** Returns an iterator to the matching element if it exists
236
237 If `name` refers to a known field, it is faster
238 to call @ref find with a field id instead of a
239 string.
240 */
241 BOOST_HTTP_PROTO_DECL
242 iterator
243 find(
244 core::string_view name) const noexcept;
245
246 /** Returns an iterator to the matching element if it exists
247 */
248 BOOST_HTTP_PROTO_DECL
249 iterator
250 find(
251 iterator from,
252 field id) const noexcept;
253
254 /** Returns an iterator to the matching element if it exists
255 */
256 BOOST_HTTP_PROTO_DECL
257 iterator
258 find(
259 iterator from,
260 core::string_view name) const noexcept;
261
262 /** Returns an iterator to the matching element if it exists
263 */
264 BOOST_HTTP_PROTO_DECL
265 iterator
266 find_last(
267 iterator before,
268 field id) const noexcept;
269
270 /** Returns an iterator to the matching element if it exists
271 */
272 BOOST_HTTP_PROTO_DECL
273 iterator
274 find_last(
275 iterator before,
276 core::string_view name) const noexcept;
277
278 /** Return the value of a field
279 */
280 BOOST_HTTP_PROTO_DECL
281 core::string_view
282 value_or(
283 field id,
284 core::string_view s) const noexcept;
285
286 /** Return the value of a field
287 */
288 BOOST_HTTP_PROTO_DECL
289 core::string_view
290 value_or(
291 core::string_view name,
292 core::string_view s) const noexcept;
293
294 //---
295
296 /** Return a forward range containing values for all matching fields
297 */
298 BOOST_HTTP_PROTO_DECL
299 subrange
300 find_all(field id) const noexcept;
301
302 /** Return a forward range containing values for all matching fields
303 */
304 BOOST_HTTP_PROTO_DECL
305 subrange
306 find_all(
307 core::string_view name) const noexcept;
308 };
309
310 } // http_proto
311 } // boost
312
313 #include <boost/http_proto/impl/fields_view_base.hpp>
314
315 #endif
316