GCC Code Coverage Report


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