userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
value_builder.hpp
Go to the documentation of this file.
1
#pragma once
2
5
6
#include <userver/compiler/impl/nodebug.hpp>
7
#include <
userver/formats/common/transfer_tag.hpp
>
8
#include <
userver/formats/serialize/to.hpp
>
9
#include <
userver/formats/yaml/value.hpp
>
10
#include <
userver/utils/strong_typedef.hpp
>
11
12
USERVER_NAMESPACE_BEGIN
13
14
namespace
formats::yaml
{
15
32
class
ValueBuilder
final {
33
public
:
34
struct
IterTraits
{
35
using
native_iter = YAML::iterator;
36
using
value_type =
formats::yaml::ValueBuilder
;
37
using
reference =
formats::yaml::ValueBuilder
&;
38
using
pointer =
formats::yaml::ValueBuilder
*;
39
};
40
41
using
iterator =
Iterator<IterTraits>
;
42
44
ValueBuilder
();
45
47
ValueBuilder
(
formats::common::Type
type);
48
49
ValueBuilder
(
const
ValueBuilder
& other);
50
// NOLINTNEXTLINE(performance-noexcept-move-constructor)
51
ValueBuilder
(
ValueBuilder
&& other);
52
ValueBuilder
& operator=(
const
ValueBuilder
& other);
53
// NOLINTNEXTLINE(performance-noexcept-move-constructor)
54
ValueBuilder
& operator=(
ValueBuilder
&& other);
55
56
ValueBuilder
(
const
formats::yaml::Value
& other);
57
ValueBuilder
(
formats::yaml::Value
&& other);
58
61
ValueBuilder
(std::nullptr_t)
62
:
ValueBuilder
()
63
{}
64
ValueBuilder
(
bool
t);
65
ValueBuilder
(
const
char
* str);
66
ValueBuilder
(
char
* str);
67
ValueBuilder
(
const
std::string& str);
68
ValueBuilder
(std::string_view str);
69
ValueBuilder
(
int
t);
70
ValueBuilder
(
unsigned
int
t);
71
ValueBuilder
(
long
t);
72
ValueBuilder
(
unsigned
long
t);
73
ValueBuilder
(
long
long
t);
74
ValueBuilder
(
unsigned
long
long
t);
75
ValueBuilder
(
float
t);
76
ValueBuilder
(
double
t);
78
81
ValueBuilder
(
common::TransferTag
,
ValueBuilder
&&) noexcept;
82
84
template <typename T>
85
ValueBuilder
(const T& t)
86
:
ValueBuilder
(DoSerialize(t))
87
{}
88
91
ValueBuilder
operator[]
(
const
std::string& key);
95
ValueBuilder
operator[]
(std::size_t index);
98
template
<
typename
Tag, utils::StrongTypedefOps Ops>
99
requires
(utils::IsStrongTypedefLoggable(Ops))
100
ValueBuilder
operator
[](
const
utils::StrongTypedef<Tag, std::string, Ops>
& key) {
101
return
(*
this
)[key.GetUnderlying()];
102
}
103
106
void
Remove
(
const
std::string& key);
107
108
iterator begin();
109
iterator end();
110
113
bool
IsEmpty
()
const
;
114
116
bool
IsNull
() const noexcept;
117
119
bool
IsBool
() const noexcept;
120
122
bool
IsInt
() const noexcept;
123
125
bool
IsInt64
() const noexcept;
126
128
bool
IsUInt64
() const noexcept;
129
131
bool
IsDouble
() const noexcept;
132
134
bool
IsString
() const noexcept;
135
137
bool
IsArray
() const noexcept;
138
140
bool
IsObject
() const noexcept;
141
144
std
::
size_t
GetSize
() const;
145
147
bool
HasMember
(const
char
* key) const;
148
150
bool
HasMember
(const
std
::
string
& key) const;
151
155
void
Resize
(
std
::
size_t
size);
156
159
void
PushBack
(
ValueBuilder
&& bld);
160
165
formats
::
yaml
::
Value
ExtractValue
();
166
167
private:
168
class EmplaceEnabler {};
169
170
public
:
172
ValueBuilder
(EmplaceEnabler,
const
YAML::Node& value,
const
formats::yaml::Path
& path,
const
std::string& key);
173
174
ValueBuilder
(EmplaceEnabler,
const
YAML::Node& value,
const
formats::yaml::Path
& path,
size_t
index);
176
177
private
:
178
static
ValueBuilder
MakeNonRoot(
const
YAML::Node& val,
const
formats::yaml::Path
& path,
const
std::string& key);
179
static
ValueBuilder
MakeNonRoot(
const
YAML::Node& val,
const
formats::yaml::Path
& path,
size_t
index);
180
181
void
Copy(
const
ValueBuilder
& from);
182
void
Move(
ValueBuilder
&& from);
183
void
NodeDataAssign(
const
formats::yaml::Value
& from);
184
185
template
<
typename
T>
186
USERVER_IMPL_NODEBUG_INLINE_FUNC
static
Value
DoSerialize(
const
T& t) {
187
static_assert
(
188
formats::common::impl::HasSerialize<Value, T>,
189
"There is no `Serialize(const T&, formats::serialize::To<yaml::Value>)` "
190
"in namespace of `T` or `formats::serialize`. "
191
""
192
"Probably you forgot to include the <userver/formats/serialize/common_containers.hpp> or you "
193
"have not provided a `Serialize` function overload."
194
);
195
196
return
Serialize
(t,
formats::serialize::To<Value>
());
197
}
198
199
formats::yaml::Value
value_;
200
201
friend
class
Iterator<IterTraits>;
202
};
203
204
template
<
typename
T>
205
requires
(std::is_integral<T>::value &&
sizeof
(T) <=
sizeof
(
long
long))
206
Value
Serialize
(T value,
formats::serialize::To<Value>
) {
207
using
Type
= std::conditional_t<std::is_signed<T>::value,
long
long,
unsigned
long
long
>;
208
return
yaml::ValueBuilder(
static_cast<
Type
>
(value)).ExtractValue();
209
}
210
211
}
// namespace formats::yaml
212
213
USERVER_NAMESPACE_END
userver
formats
yaml
value_builder.hpp
Generated on
for userver by
Doxygen
1.17.0