userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
optional.hpp
Go to the documentation of this file.
1
#pragma once
2
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
19
USERVER_NAMESPACE_BEGIN
20
21
namespace
storages::postgres::io
{
22
23
namespace
detail {
24
25
template
<
template
<
typename
>
class
Optional,
typename
T,
bool
Categories =
false
>
26
struct
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
39
template
<
template
<
typename
>
class
Optional,
typename
T>
40
struct
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
53
template
<
template
<
typename
>
class
Optional,
typename
T>
54
struct
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
71
template
<traits::HasParser T>
72
struct
BufferParser
<boost::optional<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
79
template
<traits::HasFormatter T>
80
struct
BufferFormatter
<boost::optional<T>> : detail::OptionalValueFormatter<boost::optional, T> {
81
using
BaseType = detail::OptionalValueFormatter<boost::optional, T>;
82
using
BaseType::BaseType;
83
};
84
86
template
<traits::HasParser T>
87
struct
BufferParser
<
std
::optional<T>>
88
: detail::OptionalValueParser<std::optional, T, detail::kParserRequiresTypeCategories<T>> {
89
using
BaseType = detail::OptionalValueParser<std::optional, T, detail::kParserRequiresTypeCategories<T>>;
90
using
BaseType::BaseType;
91
};
92
94
template
<traits::HasFormatter T>
95
struct
BufferFormatter
<
std
::optional<T>> : detail::OptionalValueFormatter<std::optional, T> {
96
using
BaseType = detail::OptionalValueFormatter<std::optional, T>;
97
using
BaseType::BaseType;
98
};
99
101
template
<traits::HasFormatter T>
102
struct
BufferFormatter
<USERVER_NAMESPACE::
utils::OptionalRef
<T>>
103
: detail::OptionalValueFormatter<USERVER_NAMESPACE::utils::OptionalRef, T> {
104
using
BaseType = detail::OptionalValueFormatter<USERVER_NAMESPACE::utils::OptionalRef, T>;
105
using
BaseType::BaseType;
106
};
107
109
template
<traits::kIsMappedToPg T>
110
struct
CppToPg
<boost::optional<T>> :
CppToPg
<T> {};
111
113
template
<traits::kIsMappedToPg T>
114
struct
CppToPg
<
std
::optional<T>> :
CppToPg
<T> {};
115
117
template
<traits::kIsMappedToPg T>
118
struct
CppToPg
<USERVER_NAMESPACE::
utils::OptionalRef
<T>> :
CppToPg
<T> {};
119
120
namespace
traits
{
121
123
template
<
typename
T>
124
struct
IsNullable
<boost::optional<T>> : std::true_type {};
125
126
template
<
typename
T>
127
struct
GetSetNull
<boost::optional<T>> {
128
using
ValueType = boost::optional<T>;
129
inline
static
bool
IsNull(
const
ValueType& v) {
return
!v; }
130
inline
static
void
SetNull(ValueType& v) { v = ValueType{}; }
131
inline
static
void
SetDefault(ValueType& v) { v.emplace(); }
132
};
133
134
template
<
typename
T>
135
struct
IsMappedToPg
<boost::optional<T>> :
IsMappedToPg
<T> {};
136
template
<
typename
T>
137
struct
IsSpecialMapping
<boost::optional<T>> :
IsMappedToPg
<T> {};
138
139
template
<
typename
T>
140
struct
ParserBufferCategory
<
BufferParser
<boost::optional<T>>>
141
:
ParserBufferCategory
<typename traits::IO<T>::ParserType> {};
142
144
template
<
typename
T>
145
struct
IsNullable
<
std
::optional<T>> : std::true_type {};
146
147
template
<
typename
T>
148
struct
GetSetNull
<
std
::optional<T>> {
149
using
ValueType = std::optional<T>;
150
inline
static
bool
IsNull(
const
ValueType& v) {
return
!v; }
151
inline
static
void
SetNull(ValueType& v) { v = std::nullopt; }
152
inline
static
void
SetDefault(ValueType& v) { v.emplace(); }
153
};
154
155
template
<
typename
T>
156
struct
IsMappedToPg
<
std
::optional<T>> :
IsMappedToPg
<T> {};
157
template
<
typename
T>
158
struct
IsSpecialMapping
<
std
::optional<T>> :
IsMappedToPg
<T> {};
159
160
template
<
typename
T>
161
struct
ParserBufferCategory
<
BufferParser
<
std
::optional<T>>> :
ParserBufferCategory
<typename traits::IO<T>::ParserType> {
162
};
163
165
template
<
typename
T>
166
struct
IsNullable
<USERVER_NAMESPACE::
utils::OptionalRef
<T>> : std::true_type {};
167
168
template
<
typename
T>
169
struct
GetSetNull
<USERVER_NAMESPACE::
utils::OptionalRef
<T>> {
170
using
ValueType = USERVER_NAMESPACE::utils::OptionalRef<T>;
171
inline
static
bool
IsNull(
const
ValueType& v) {
return
!v; }
172
inline
static
void
SetNull(ValueType&) {
static_assert
(!
sizeof
(T),
"SetNull not enabled for utils::OptionalRef"
); }
173
inline
static
void
SetDefault(ValueType&) {
174
static_assert
(!
sizeof
(T),
"SetDefault not enabled for utils::OptionalRef"
);
175
}
176
};
177
178
template
<
typename
T>
179
struct
IsMappedToPg
<USERVER_NAMESPACE::
utils::OptionalRef
<T>> :
IsMappedToPg
<T> {};
180
template
<
typename
T>
181
struct
IsSpecialMapping
<USERVER_NAMESPACE::
utils::OptionalRef
<T>> :
IsMappedToPg
<T> {};
182
183
}
// namespace traits
184
185
}
// namespace storages::postgres::io
186
187
USERVER_NAMESPACE_END
userver
storages
postgres
io
optional.hpp
Generated on
for userver by
Doxygen
1.17.0