Line data Source code
1 : //
2 : // Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com)
3 : // Copyright (c) 2025 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/http_proto
9 : //
10 :
11 : #ifndef BOOST_HTTP_PROTO_FIELDS_VIEW_HPP
12 : #define BOOST_HTTP_PROTO_FIELDS_VIEW_HPP
13 :
14 : #include <boost/http_proto/detail/config.hpp>
15 : #include <boost/http_proto/fields_view_base.hpp>
16 : #include <boost/assert.hpp>
17 :
18 : namespace boost {
19 : namespace http_proto {
20 :
21 : /** A view to a valid HTTP headers section.
22 :
23 : Objects of this type represent a view to
24 : an HTTP fields container. That is, it acts
25 : like a `core::string_view` in terms of
26 : ownership. The caller is responsible for
27 : ensuring that the lifetime of the underlying
28 : buffer extends until it is no
29 : longer referenced.
30 :
31 : @see
32 : @ref fields,
33 : @ref static_fields,
34 : */
35 : class fields_view
36 : : public fields_view_base
37 : {
38 : friend class fields;
39 : template<std::size_t>
40 : friend class static_fields;
41 :
42 8 : fields_view(
43 : detail::header const* ph) noexcept
44 8 : : fields_view_base(ph)
45 : {
46 8 : BOOST_ASSERT(ph_->kind ==
47 : detail::kind::fields);
48 8 : }
49 :
50 : public:
51 :
52 : //--------------------------------------------
53 : //
54 : // Special Members
55 : //
56 : //--------------------------------------------
57 :
58 : /** Constructor.
59 :
60 : A default-constructed fields views refer to
61 : a valid HTTP headers section that contains
62 : no name-value pairs, which always remains
63 : valid.
64 :
65 : @par Example
66 : @code
67 : fields fs;
68 : @endcode
69 :
70 : @par Postconditions
71 : @code
72 : this->buffer() == "\r\n"
73 : @endcode
74 :
75 : @par Complexity
76 : Constant.
77 : */
78 7 : fields_view() noexcept
79 7 : : fields_view_base(
80 : detail::header::get_default(
81 7 : detail::kind::fields))
82 : {
83 7 : }
84 :
85 : /** Constructor.
86 :
87 : After construction, both fields views
88 : reference the same underlying buffer.
89 : Ownership is not transferred.
90 :
91 : @par Postconditions
92 : @code
93 : this->buffer().data() == other.buffer().data()
94 : @endcode
95 :
96 : @par Complexity
97 : Constant.
98 :
99 : @param other The other view.
100 : */
101 : fields_view(
102 : fields_view const& other) noexcept = default;
103 :
104 : /** Assignment.
105 :
106 : After assignment, both fields views
107 : reference the same underlying buffer.
108 : Ownership is not transferred.
109 :
110 : @par Postconditions
111 : @code
112 : this->buffer().data() == other.buffer().data()
113 : @endcode
114 :
115 : @par Complexity
116 : Constant.
117 :
118 : @return A reference to this object.
119 :
120 : @param other The other view.
121 : */
122 : fields_view&
123 : operator=(
124 : fields_view const& other) noexcept = default;
125 :
126 : /** Destructor
127 :
128 : Any reference, iterator, or other view
129 : which reference the same underlying
130 : buffer remain valid.
131 : */
132 : ~fields_view() = default;
133 :
134 : //--------------------------------------------
135 :
136 : /** Swap.
137 :
138 : Exchanges the view with that of `other`.
139 : All iterators and references remain valid.
140 :
141 : If `this == &other`, this function call has no effect.
142 :
143 : @par Example
144 : @code
145 : fields f1;
146 : f1.set(field::accept, "text/html");
147 : fields f2;
148 : f2.set(field::connection, "keep-alive");
149 : fields_view v1 = f1;
150 : fields_view v2 = f2;
151 : v1.swap(v2);
152 : assert(v1.buffer() == "Connection: keep-alive\r\n\r\n" );
153 : assert(v2.buffer() == "Accept: text/html\r\n\r\n" );
154 : @endcode
155 :
156 : @par Complexity
157 : Constant.
158 :
159 : @param other The object to swap with.
160 : */
161 : void
162 : swap(fields_view& other) noexcept
163 : {
164 : auto ph = ph_;
165 : ph_ = other.ph_;
166 : other.ph_ = ph;
167 : }
168 :
169 : /** Swap.
170 :
171 : Exchanges the view of `v0` with
172 : another `v1`. All iterators and
173 : references remain valid.
174 :
175 : If `&v0 == &v1`, this function call has no effect.
176 :
177 : @par Example
178 : @code
179 : fields f1;
180 : f1.set(field::accept, "text/html");
181 : fields f2;
182 : f2.set(field::connection, "keep-alive");
183 : fields_view v1 = f1;
184 : fields_view v2 = f2;
185 : std::swap(v1, v2);
186 : assert(v1.buffer() == "Connection: keep-alive\r\n\r\n" );
187 : assert(v2.buffer() == "Accept: text/html\r\n\r\n" );
188 : @endcode
189 :
190 : @par Effects
191 : @code
192 : v0.swap(v1);
193 : @endcode
194 :
195 : @par Complexity
196 : Constant.
197 :
198 : @param v0 The first object to swap.
199 : @param v1 The second object to swap.
200 :
201 : @see
202 : @ref fields_view::swap
203 : */
204 : friend
205 : void
206 : swap(
207 : fields_view& v0,
208 : fields_view& v1) noexcept
209 : {
210 : v0.swap(v1);
211 : }
212 : };
213 :
214 : } // http_proto
215 : } // boost
216 :
217 : #endif
|