Skip to content

Commit 3ad3377

Browse files
committed
fields_view_base has at() observer method
1 parent 9b71c08 commit 3ad3377

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

include/boost/http_proto/fields_view_base.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,36 @@ class fields_view_base
208208
return ph_->count;
209209
}
210210

211+
/** Return the value of a field, or throws an exception.
212+
213+
If more than one field with the specified name exists,
214+
the first field defined by insertion order is returned.
215+
216+
@param id The field name constant.
217+
218+
@return The field value.
219+
220+
@throw std::out_of_range if the field is not found.
221+
*/
222+
BOOST_HTTP_PROTO_DECL
223+
core::string_view
224+
at(field id) const;
225+
226+
/** Return the value of a field, or throws an exception.
227+
228+
If more than one field with the specified name exists,
229+
the first field defined by insertion order is returned.
230+
231+
@param name The field name.
232+
233+
@return The field value.
234+
235+
@throw std::out_of_range if the field is not found.
236+
*/
237+
BOOST_HTTP_PROTO_DECL
238+
core::string_view
239+
at(core::string_view name) const;
240+
211241
/** Return true if a field exists
212242
*/
213243
BOOST_HTTP_PROTO_DECL

src/fields_view_base.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,30 @@ operator++() noexcept ->
163163
//
164164
//------------------------------------------------
165165

166+
core::string_view
167+
fields_view_base::
168+
at(
169+
field id) const
170+
{
171+
auto const it = find(id);
172+
if(it == end())
173+
BOOST_THROW_EXCEPTION(
174+
std::out_of_range{ "field not found" });
175+
return it->value;
176+
}
177+
178+
core::string_view
179+
fields_view_base::
180+
at(
181+
core::string_view name) const
182+
{
183+
auto const it = find(name);
184+
if(it == end())
185+
BOOST_THROW_EXCEPTION(
186+
std::out_of_range{ "field not found" });
187+
return it->value;
188+
}
189+
166190
bool
167191
fields_view_base::
168192
exists(

test/unit/fields_view_base.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,18 @@ struct fields_view_base_test
172172

173173
BOOST_TEST(f.size() == 10);
174174

175+
// at(field)
176+
// at(string_view)
177+
178+
BOOST_TEST(f.at("x") == "1");
179+
BOOST_TEST(f.at(field::set_cookie) == "a");
180+
BOOST_TEST_THROWS(
181+
f.at("accept"),
182+
std::out_of_range);
183+
BOOST_TEST_THROWS(
184+
f.at(field::accept),
185+
std::out_of_range);
186+
175187
// exists(field)
176188
// exists(string_view)
177189

0 commit comments

Comments
 (0)