userver: userver/storages/postgres/io/optional.hpp Source File
Loading...
Searching...
No Matches
optional.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/postgres/io/optional.hpp
4/// @brief Optional values I/O support
5/// @ingroup userver_postgres_parse_and_format
6
7#include <optional>
8
9#include <userver/utils/assert.hpp>
10#include <userver/utils/optional_ref.hpp>
11
12#include <userver/storages/postgres/io/buffer_io.hpp>
13#include <userver/storages/postgres/io/buffer_io_base.hpp>
14#include <userver/storages/postgres/io/nullable_traits.hpp>
15#include <userver/storages/postgres/io/traits.hpp>
16
17#include <boost/optional/optional_fwd.hpp>
18
19USERVER_NAMESPACE_BEGIN
20
21namespace storages::postgres::io {
22
23namespace detail {
24
25template <template <typename> class Optional, typename T, bool Categories = false>
26struct OptionalValueParser : BufferParserBase<Optional<T>> {
27 using BaseType = BufferParserBase<Optional<T>>;
28 using ValueParser = typename traits::IO<T>::ParserType;
29
30 using BaseType::BaseType;
31
32 void operator()(const FieldBuffer& buffer) {
33 T val;
34 ValueParser{val}(buffer);
35 this->value = std::move(val);
36 }
37};
38
39template <template <typename> class Optional, typename T>
40struct OptionalValueParser<Optional, T, true> : BufferParserBase<Optional<T>> {
41 using BaseType = BufferParserBase<Optional<T>>;
42 using ValueParser = typename traits::IO<T>::ParserType;
43
44 using BaseType::BaseType;
45
46 void operator()(const FieldBuffer& buffer, const TypeBufferCategory& categories) {
47 T val;
48 ValueParser{val}(buffer, categories);
49 this->value = std::move(val);
50 }
51};
52
53template <template <typename> class Optional, typename T>
54struct OptionalValueFormatter : BufferFormatterBase<Optional<T>> {
55 using BaseType = BufferFormatterBase<Optional<T>>;
56 using ValueFormatter = typename traits::IO<T>::FormatterType;
57
58 using BaseType::BaseType;
59
60 template <typename Buffer>
61 void operator()(const UserTypes& types, Buffer& buffer) const {
62 if (this->value) {
63 ValueFormatter{*this->value}(types, buffer);
64 }
65 }
66};
67
68} // namespace detail
69
70/// Parser specialization for boost::optional
71template <typename T>
72struct BufferParser<boost::optional<T>, std::enable_if_t<traits::kHasParser<T>>>
73 : detail::OptionalValueParser<boost::optional, T, detail::kParserRequiresTypeCategories<T>> {
74 using BaseType = detail::OptionalValueParser<boost::optional, T, detail::kParserRequiresTypeCategories<T>>;
75 using BaseType::BaseType;
76};
77
78/// Formatter specialization for boost::optional
79template <typename T>
80struct BufferFormatter<boost::optional<T>, std::enable_if_t<traits::kHasFormatter<T>>>
81 : detail::OptionalValueFormatter<boost::optional, T> {
82 using BaseType = detail::OptionalValueFormatter<boost::optional, T>;
83 using BaseType::BaseType;
84};
85
86/// Parser specialization for std::optional
87template <typename T>
88struct BufferParser<std::optional<T>, std::enable_if_t<traits::kHasParser<T>>>
89 : detail::OptionalValueParser<std::optional, T, detail::kParserRequiresTypeCategories<T>> {
90 using BaseType = detail::OptionalValueParser<std::optional, T, detail::kParserRequiresTypeCategories<T>>;
91 using BaseType::BaseType;
92};
93
94/// Formatter specialization for std::optional
95template <typename T>
96struct BufferFormatter<std::optional<T>, std::enable_if_t<traits::kHasFormatter<T>>>
97 : detail::OptionalValueFormatter<std::optional, T> {
98 using BaseType = detail::OptionalValueFormatter<std::optional, T>;
99 using BaseType::BaseType;
100};
101
102/// Formatter specialization for utils::OptionalRef
103template <typename T>
104struct BufferFormatter<USERVER_NAMESPACE::utils::OptionalRef<T>, std::enable_if_t<traits::kHasFormatter<T>>>
105 : detail::OptionalValueFormatter<USERVER_NAMESPACE::utils::OptionalRef, T> {
106 using BaseType = detail::OptionalValueFormatter<USERVER_NAMESPACE::utils::OptionalRef, T>;
107 using BaseType::BaseType;
108};
109
110/// Pg mapping specialization for boost::optional
111template <typename T>
112struct CppToPg<boost::optional<T>, std::enable_if_t<traits::kIsMappedToPg<T>>> : CppToPg<T> {};
113
114/// Pg mapping specialization for std::optional
115template <typename T>
116struct CppToPg<std::optional<T>, std::enable_if_t<traits::kIsMappedToPg<T>>> : CppToPg<T> {};
117
118/// Pg mapping specialization for USERVER_NAMESPACE::utils::OptionalRef
119template <typename T>
120struct CppToPg<USERVER_NAMESPACE::utils::OptionalRef<T>, std::enable_if_t<traits::kIsMappedToPg<T>>> : CppToPg<T> {};
121
122namespace traits {
123
124/// Nullability traits for boost::optional
125template <typename T>
126struct IsNullable<boost::optional<T>> : std::true_type {};
127
128template <typename T>
129struct GetSetNull<boost::optional<T>> {
130 using ValueType = boost::optional<T>;
131 inline static bool IsNull(const ValueType& v) { return !v; }
132 inline static void SetNull(ValueType& v) { v = ValueType{}; }
133 inline static void SetDefault(ValueType& v) { v.emplace(); }
134};
135
136template <typename T>
138template <typename T>
140
141template <typename T>
143 : ParserBufferCategory<typename traits::IO<T>::ParserType> {};
144
145/// Nullability traits for std::optional
146template <typename T>
147struct IsNullable<std::optional<T>> : std::true_type {};
148
149template <typename T>
150struct GetSetNull<std::optional<T>> {
151 using ValueType = std::optional<T>;
152 inline static bool IsNull(const ValueType& v) { return !v; }
153 inline static void SetNull(ValueType& v) { v = std::nullopt; }
154 inline static void SetDefault(ValueType& v) { v.emplace(); }
155};
156
157template <typename T>
159template <typename T>
161
162template <typename T>
164};
165
166/// Nullability traits for USERVER_NAMESPACE::utils::OptionalRef
167template <typename T>
168struct IsNullable<USERVER_NAMESPACE::utils::OptionalRef<T>> : std::true_type {};
169
170template <typename T>
171struct GetSetNull<USERVER_NAMESPACE::utils::OptionalRef<T>> {
172 using ValueType = USERVER_NAMESPACE::utils::OptionalRef<T>;
173 inline static bool IsNull(const ValueType& v) { return !v; }
174 inline static void SetNull(ValueType&) { static_assert(!sizeof(T), "SetNull not enabled for utils::OptionalRef"); }
175 inline static void SetDefault(ValueType&) {
176 static_assert(!sizeof(T), "SetDefault not enabled for utils::OptionalRef");
177 }
178};
179
180template <typename T>
181struct IsMappedToPg<USERVER_NAMESPACE::utils::OptionalRef<T>> : IsMappedToPg<T> {};
182template <typename T>
183struct IsSpecialMapping<USERVER_NAMESPACE::utils::OptionalRef<T>> : IsMappedToPg<T> {};
184
185} // namespace traits
186
187} // namespace storages::postgres::io
188
189USERVER_NAMESPACE_END