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
13/// @brief Generic parsing helpers (`formats::parse::To` and friends).
14namespace formats::parse {
15
16template <class Value, typename T>
17boost::optional<T> Parse(const Value& value, To<boost::optional<T>>) {
18 if (value.IsMissing() || value.IsNull()) {
19 return boost::none;
20 }
21 return value.template As<T>();
22}
23
24template <class Value>
25boost::optional<std::nullptr_t> Parse(const Value&, To<boost::optional<std::nullptr_t>>) {
26 static_assert(!sizeof(Value), "optional<nullptr_t> is forbidden, check IsNull() instead");
27 return nullptr;
28}
29
30template <class Value, typename T>
31boost::optional<T> Convert(const Value& value, To<boost::optional<T>>) {
32 if (value.IsMissing() || value.IsNull()) {
33 return boost::none;
34 }
35 return value.template ConvertTo<T>();
36}
37
38template <class Value>
39boost::optional<std::nullptr_t> Convert(const Value&, To<boost::optional<std::nullptr_t>>) {
40 static_assert(!sizeof(Value), "optional<nullptr_t> is forbidden, check IsNull() instead");
41 return nullptr;
42}
43
44} // namespace formats::parse
45
46USERVER_NAMESPACE_END