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&, To<boost::optional<std::nullptr_t>>) {
25 static_assert(!sizeof(Value), "optional<nullptr_t> is forbidden, check IsNull() instead");
26 return nullptr;
27}
28
29template <class Value, typename T>
30boost::optional<T> Convert(const Value& value, To<boost::optional<T>>) {
31 if (value.IsMissing() || value.IsNull()) {
32 return boost::none;
33 }
34 return value.template ConvertTo<T>();
35}
36
37template <class Value>
38boost::optional<std::nullptr_t> Convert(const Value&, To<boost::optional<std::nullptr_t>>) {
39 static_assert(!sizeof(Value), "optional<nullptr_t> is forbidden, check IsNull() instead");
40 return nullptr;
41}
42
43} // namespace formats::parse
44
45USERVER_NAMESPACE_END