include/boost/burl/static_fields.hpp

100.0% Lines (5/5) 100.0% List of functions (1/1) 50.0% Branches (1/2)
static_fields.hpp
f(x) Functions (1)
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2021 Vinnie Falco ([email protected])
3 // Copyright (c) 2026 Mohammad Nejati
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/cppalliance/burl
9 //
10
11 #ifndef BOOST_BURL_STATIC_FIELDS_HPP
12 #define BOOST_BURL_STATIC_FIELDS_HPP
13
14 #include <boost/burl/detail/config.hpp>
15 #include <boost/burl/fields_base.hpp>
16
17 #include <cstddef>
18
19 namespace boost
20 {
21 namespace burl
22 {
23
24 /** A static container of HTTP fields.
25
26 This container holds a sequence of HTTP fields
27 in an externally provided buffer with fixed
28 capacity, and performs no allocations during its
29 lifetime. Modifiers throw `std::length_error`
30 when the storage is exhausted.
31
32 A newly constructed container is empty;
33 @ref buffer returns "\r\n". Fields are filled in
34 through the inherited @ref fields_base interface.
35
36 The caller is responsible for ensuring that the
37 lifetime of the storage extends until the
38 container is destroyed.
39
40 @par Example
41 @code
42 char buf[256];
43 static_fields f(buf, sizeof(buf));
44
45 f.set(http::field::host, "example.com");
46
47 assert(f.buffer() ==
48 "Host: example.com\r\n"
49 "\r\n");
50 @endcode
51
52 @see
53 @ref fields,
54 @ref fields_base.
55 */
56 class static_fields : public fields_base
57 {
58 public:
59 /** Constructor.
60
61 The container uses the given storage and
62 holds no fields.
63
64 @par Exception Safety
65 Strong guarantee.
66
67 @throw std::length_error
68 The storage cannot accommodate the empty
69 field section.
70
71 @param storage The storage to use.
72
73 @param n The size of the storage.
74 */
75 BOOST_BURL_DECL
76 static_fields(
77 char* storage,
78 std::size_t n);
79
80 /** Constructor (deleted).
81 */
82 static_fields(
83 static_fields const&) = delete;
84
85 /** Constructor.
86
87 The newly constructed object refers to the
88 storage of `other`, which is left in a valid
89 but unspecified state where the only safe
90 operation is destruction: it no longer refers
91 to the storage, and modifiers and assignment
92 throw `std::length_error`.
93
94 @par Complexity
95 Constant.
96
97 @param other The container to move from.
98 */
99 BOOST_BURL_DECL
100 static_fields(
101 static_fields&& other) noexcept;
102
103 /** Assignment.
104
105 The contents are replaced with a copy of
106 `other`. The storage is retained.
107
108 @par Complexity
109 Linear in `other.buffer().size()`.
110
111 @par Exception Safety
112 Strong guarantee.
113
114 @throw std::length_error
115 The storage cannot accommodate the contents
116 of `other`.
117
118 @param other The fields to copy.
119
120 @return A reference to this object.
121 */
122 BOOST_BURL_DECL
123 static_fields&
124 operator=(static_fields const& other);
125
126 /** Assignment.
127
128 The contents are replaced with a copy of the
129 field section of `other`. When `other` is a
130 message header, only its fields are copied;
131 the start line is discarded. The storage is
132 retained.
133
134 @par Complexity
135 Linear in `other.buffer().size()`.
136
137 @par Exception Safety
138 Strong guarantee.
139
140 @throw std::length_error
141 The storage cannot accommodate the contents
142 of `other`.
143
144 @param other The fields to copy.
145
146 @return A reference to this object.
147 */
148 BOOST_BURL_DECL
149 static_fields&
150 operator=(fields_base const& other);
151
152 /** Return the storage size needed for a header.
153
154 Returns the size of a storage which holds a
155 field section of `size` bytes and the lookup
156 table of `count` fields.
157
158 @par Example
159 @code
160 char buf[
161 static_fields::bytes_needed(1024, 32)];
162
163 static_fields f(buf, sizeof(buf));
164 @endcode
165
166 @param size The size of the field section,
167 including the final empty line.
168
169 @param count The number of fields.
170 */
171 static constexpr
172 std::size_t
173 170x bytes_needed(
174 std::size_t size,
175 std::size_t count) noexcept
176 {
177
1/2
✓ Branch 0 taken 170 times.
✗ Branch 1 not taken.
170x if(size < 2)
178 170x size = 2; // "\r\n"
179
180 170x return size + count * sizeof(entry) +
181 170x alignof(entry) - 1;
182 }
183 };
184
185 } // namespace burl
186 } // namespace boost
187
188 #endif
189