userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
value.hpp
Go to the documentation of this file.
1
#pragma once
2
5
6
#include <chrono>
7
#include <cstddef>
8
#include <cstdint>
9
#include <string>
10
11
#include <
userver/formats/bson/exception.hpp
>
12
#include <
userver/formats/bson/iterator.hpp
>
13
#include <
userver/formats/bson/types.hpp
>
14
#include <
userver/formats/common/items.hpp
>
15
#include <
userver/formats/common/meta.hpp
>
16
#include <
userver/formats/parse/common.hpp
>
17
#include <
userver/formats/parse/common_containers.hpp
>
18
19
USERVER_NAMESPACE_BEGIN
20
21
namespace
formats::bson
{
22
namespace
impl {
23
class
BsonBuilder;
24
class
ValueImpl;
25
}
// namespace impl
26
27
class
Document
;
28
class
ValueBuilder
;
29
42
class
Value
{
43
public
:
44
struct
DefaultConstructed
{};
45
46
using
const_iterator =
Iterator<const Value, common::IteratorDirection::kForward>
;
47
using
const_reverse_iterator =
Iterator<const Value, common::IteratorDirection::kReverse>
;
48
using
Exception =
formats::bson::BsonException
;
49
using
ParseException =
formats::bson::ParseException
;
50
using
ExceptionWithPath =
formats::bson::ExceptionWithPath
;
51
using
Builder = ValueBuilder;
52
55
enum class
DuplicateFieldsPolicy
{ kForbid, kUseFirst, kUseLast };
56
58
Value
();
59
60
Value
(
const
Value
&) =
default
;
61
Value
(
Value
&&) noexcept = default;
62
Value
& operator=(const
Value
&) & = default;
63
Value
& operator=(
Value
&&) & noexcept = default;
64
65
template <class T>
66
Value
& operator=(T&&) && {
67
static_assert
(
68
!
sizeof
(T),
69
"You're assigning to a temporary formats::bson::Value! Use "
70
"formats::bson::ValueBuilder for data modifications."
71
);
72
return
*
this
;
73
}
74
77
explicit
Value
(impl::ValueImplPtr);
79
84
Value
operator[]
(
const
std::string& name)
const
;
85
90
Value
operator[]
(uint32_t index)
const
;
91
95
bool
HasMember
(
const
std::string& name)
const
;
96
101
const_iterator
begin
()
const
;
102
105
const_iterator
end
()
const
;
106
109
const_reverse_iterator
rbegin
()
const
;
110
113
const_reverse_iterator
rend
()
const
;
114
118
bool
IsEmpty
()
const
;
119
124
uint32_t
GetSize
()
const
;
125
127
std::string
GetPath
()
const
;
128
129
bool
operator==(
const
Value
&)
const
;
130
133
bool
IsMissing
()
const
;
134
137
bool
IsArray()
const
;
138
bool
IsDocument()
const
;
139
bool
IsNull()
const
;
140
bool
IsBool()
const
;
141
bool
IsInt()
const
;
142
bool
IsInt32()
const
;
143
bool
IsInt64()
const
;
144
bool
IsUInt64()
const
;
145
bool
IsDouble()
const
;
146
bool
IsString()
const
;
147
bool
IsDateTime()
const
;
148
bool
IsOid()
const
;
149
bool
IsBinary()
const
;
150
bool
IsDecimal128()
const
;
151
bool
IsMinKey()
const
;
152
bool
IsMaxKey()
const
;
153
bool
IsTimestamp()
const
;
154
155
bool
IsObject()
const
{
return
IsDocument(); }
157
165
template
<
typename
T>
166
auto
As
()
const
{
167
static_assert
(
168
formats::common::impl::HasParse<Value, T>,
169
"There is no `Parse(const Value&, formats::parse::To<T>)` in namespace "
170
"of `T` or `formats::parse`. "
171
"Probably you have not provided a `Parse` function overload."
172
);
173
174
return
Parse(*
this
,
formats::parse::To<T>
{});
175
}
176
179
template
<
typename
T,
typename
First,
typename
... Rest>
180
auto
As
(First&& default_arg, Rest&&... more_default_args)
const
{
181
if
(
IsMissing
() || IsNull()) {
182
// intended raw ctor call, sometimes casts
183
// NOLINTNEXTLINE(google-readability-casting)
184
return
decltype
(
As<T>
())(std::forward<First>(default_arg), std::forward<Rest>(more_default_args)...);
185
}
186
return
As<T>
();
187
}
188
192
template
<
typename
T>
193
auto
As
(
DefaultConstructed
)
const
{
194
return
(
IsMissing
() || IsNull()) ?
decltype
(
As<T>
())() :
As<T>
();
195
}
196
199
template
<
typename
T>
200
T
ConvertTo
()
const
{
201
if
constexpr
(formats::common::impl::HasConvert<Value, T>) {
202
return
Convert(*
this
,
formats::parse::To<T>
{});
203
}
else
if
constexpr
(formats::common::impl::HasParse<Value, T>) {
204
return
Parse(*
this
,
formats::parse::To<T>
{});
205
}
else
{
206
static_assert
(
207
!
sizeof
(T),
208
"There is no `Convert(const Value&, formats::parse::To<T>)` or "
209
"`Parse(const Value&, formats::parse::To<T>)` in namespace of `T` or `formats::parse`. "
210
"Probably you have not provided a `Convert` function overload."
211
);
212
}
213
}
214
217
template
<
typename
T,
typename
First,
typename
... Rest>
218
T
ConvertTo
(First&& default_arg, Rest&&... more_default_args)
const
{
219
if
(
IsMissing
() || IsNull()) {
220
// NOLINTNEXTLINE(google-readability-casting)
221
return
T(std::forward<First>(default_arg), std::forward<Rest>(more_default_args)...);
222
}
223
return
ConvertTo<T>
();
224
}
225
233
void
SetDuplicateFieldsPolicy
(
DuplicateFieldsPolicy
);
234
236
void
CheckNotMissing
()
const
;
237
240
void
CheckArrayOrNull
()
const
;
241
244
void
CheckDocumentOrNull
()
const
;
245
248
void
CheckObjectOrNull()
const
{
CheckDocumentOrNull
(); }
249
252
Document
GetInternalArrayDocument()
const
;
253
255
const
impl::BsonHolder& GetBson()
const
;
257
258
private
:
259
friend
class
ValueBuilder
;
260
friend
class
impl::BsonBuilder;
261
262
friend
bool
Parse(
const
Value
& value,
parse::To<bool>
);
263
friend
int64_t Parse(
const
Value
& value,
parse::To<int64_t>
);
264
friend
uint64_t Parse(
const
Value
& value,
parse::To<uint64_t>
);
265
friend
double
Parse(
const
Value
& value,
parse::To<double>
);
266
friend
std::string Parse(
const
Value
& value,
parse::To<std::string>
);
267
friend
std::chrono::system_clock::time_point
268
Parse(
const
Value
& value,
parse::To<std::chrono::system_clock::time_point>
);
269
friend
Oid
Parse(
const
Value
& value,
parse::To<Oid>
);
270
friend
Binary
Parse(
const
Value
& value,
parse::To<Binary>
);
271
friend
Decimal128
Parse(
const
Value
& value,
parse::To<Decimal128>
);
272
friend
Timestamp
Parse(
const
Value
& value,
parse::To<Timestamp>
);
273
friend
Document
Parse(
const
Value
& value,
parse::To<Document>
);
274
275
impl::ValueImplPtr impl_;
276
};
277
279
bool
Parse(
const
Value& value,
parse::To<bool>
);
280
281
int64_t Parse(
const
Value& value,
parse::To<int64_t>
);
282
283
uint64_t Parse(
const
Value& value,
parse::To<uint64_t>
);
284
285
double
Parse(
const
Value& value,
parse::To<double>
);
286
287
std::string Parse(
const
Value& value,
parse::To<std::string>
);
288
289
std::chrono::system_clock::time_point Parse(
const
Value& value,
parse::To<std::chrono::system_clock::time_point>
);
290
291
Oid Parse(
const
Value& value,
parse::To<Oid>
);
292
293
Binary Parse(
const
Value& value,
parse::To<Binary>
);
294
295
Decimal128 Parse(
const
Value& value,
parse::To<Decimal128>
);
296
297
Timestamp Parse(
const
Value& value,
parse::To<Timestamp>
);
298
299
Document Parse(
const
Value& value,
parse::To<Document>
);
300
301
template
<>
302
bool
Value::ConvertTo<bool>
()
const
;
303
304
template
<>
305
int64_t
Value::ConvertTo<int64_t>
()
const
;
306
307
template
<>
308
uint64_t
Value::ConvertTo<uint64_t>
()
const
;
309
310
template
<>
311
double
Value::ConvertTo<double>
()
const
;
312
313
template
<>
314
std::string Value::ConvertTo<std::string>()
const
;
316
320
using
formats::common::Items
;
321
322
}
// namespace formats::bson
323
326
namespace
formats::literals
{
327
328
bson::Value
operator
""
_bson(
const
char
* str, std::size_t len);
329
330
}
// namespace formats::literals
331
332
USERVER_NAMESPACE_END
userver
formats
bson
value.hpp
Generated on
for userver by
Doxygen
1.17.0