userver: userver/formats/parse/boost_optional.hpp Source File
Loading...
Searching...
No Matches
boost_optional.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/formats/parse/boost_optional.hpp
4/// @brief Parsers and converters for boost::optional
5/// @ingroup userver_universal userver_formats_parse
6
7#include <userver/formats/parse/to.hpp>
8
9#include <boost/optional.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace formats::parse {
14
15template <class Value, typename T>
16boost::optional<T> Parse(const Value& value, To<boost::optional<T>>) {
17 if (value.IsMissing() || value.IsNull()) {
18 return boost::none;
19 }
20 return value.template As<T>();
21}
22
23template <class Value>
24boost::optional<std::nullptr_t> Parse(const Value&,
25 To<boost::optional<std::nullptr_t>>) {
26 static_assert(!sizeof(Value),
27 "optional<nullptr_t> is forbidden, check IsNull() instead");
28 return nullptr;
29}
30
31template <class Value, typename T>
32boost::optional<T> Convert(const Value& value, To<boost::optional<T>>) {
33 if (value.IsMissing() || value.IsNull()) {
34 return boost::none;
35 }
36 return value.template ConvertTo<T>();
37}
38
39template <class Value>
40boost::optional<std::nullptr_t> Convert(const Value&,
41 To<boost::optional<std::nullptr_t>>) {
42 static_assert(!sizeof(Value),
43 "optional<nullptr_t> is forbidden, check IsNull() instead");
44 return nullptr;
45}
46
47} // namespace formats::parse
48
49USERVER_NAMESPACE_END