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
3
/// @file userver/formats/bson/value.hpp
4
/// @brief @copybrief formats::bson::Value
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
30
/// @brief Non-mutable BSON value representation.
31
///
32
/// Class provides non mutable access BSON value. For modification and
33
/// construction of new BSON values use formats::bson::ValueBuilder.
34
///
35
/// ## Example usage:
36
///
37
/// @snippet formats/bson/value_test.cpp Sample formats::bson::Value usage
38
///
39
/// @see @ref scripts/docs/en/userver/formats.md
40
///
41
/// To iterate over `Value` as object use formats::common::Items.
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
53
/// @brief Selectors for duplicate fields parsing behavior
54
/// @see SetDuplicateFieldsPolicy
55
enum
class
DuplicateFieldsPolicy
{ kForbid, kUseFirst, kUseLast };
56
57
/// Constructs a `null` value
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
75
/// @cond
76
/// Constructor from implementation, internal use only
77
explicit
Value(impl::ValueImplPtr);
78
/// @endcond
79
80
/// @brief Retrieves document field by name
81
/// @param name field name
82
/// @throws TypeMismatchException if value is not a missing value, a document,
83
/// or `null`
84
Value
operator
[](
const
std::string& name)
const
;
85
86
/// @brief Retrieves array element by index
87
/// @param index element index
88
/// @throws TypeMismatchException if value is not an array or `null`
89
/// @throws OutOfBoundsException if index is invalid for the array
90
Value
operator
[](uint32_t index)
const
;
91
92
/// @brief Checks whether the document has a field
93
/// @param name field name
94
/// @throws TypeMismatchExcepiton if value is not a document or `null`
95
bool
HasMember
(
const
std::string& name)
const
;
96
97
/// @brief Returns an iterator to the first array element/document field
98
/// @throws TypeMismatchException if value is not a document, array or `null`
99
///
100
/// To iterate over `Value` as object use formats::common::Items.
101
const_iterator
begin
()
const
;
102
103
/// @brief Returns an iterator following the last array element/document field
104
/// @throws TypeMismatchException if value is not a document, array or `null`
105
const_iterator
end
()
const
;
106
107
/// @brief Returns a reversed iterator to the last array element
108
/// @throws TypeMismatchException if value is not an array or `null`
109
const_reverse_iterator
rbegin
()
const
;
110
111
/// @brief Returns a reversed iterator following the first array element
112
/// @throws TypeMismatchException if value is not an array or `null`
113
const_reverse_iterator
rend
()
const
;
114
115
/// @brief Returns whether the document/array is empty
116
/// @throws TypeMismatchException if value is not a document, array or `null`
117
/// @note Returns `true` for `null`.
118
bool
IsEmpty
()
const
;
119
120
/// @brief Returns the number of elements in a document/array
121
/// @throws TypeMismatchException if value is not a document, array or `null`
122
/// @note May require linear time before the first element access.
123
/// @note Returns 0 for `null`.
124
uint32_t
GetSize
()
const
;
125
126
/// Returns value path in a document
127
std::string
GetPath
()
const
;
128
129
bool
operator==(
const
Value
&)
const
;
130
131
/// @brief Checks whether the selected element exists
132
/// @note MemberMissingException is throws on nonexisting element access
133
bool
IsMissing
()
const
;
134
135
/// @name Type checking
136
/// @{
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(); }
156
/// @}
157
158
/// Extracts the specified type with strict type checks
159
///
160
/// ## Example usage:
161
///
162
/// @snippet formats/bson/value_test.cpp Sample formats::bson::Value::As<T>() usage
163
///
164
/// @see @ref scripts/docs/en/userver/formats.md
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
177
/// Extracts the specified type with strict type checks, or constructs the
178
/// default value when the field is not present
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
189
/// @brief Returns value of *this converted to T or T() if this->IsMissing().
190
/// @throw Anything derived from std::exception.
191
/// @note Use as `value.As<T>({})`
192
template
<
typename
T>
193
auto
As
(
DefaultConstructed
)
const
{
194
return
(
IsMissing
(
)
|| IsNull()) ?
decltype
(As<T>())() : As<T>();
195
}
196
197
/// @brief Extracts the specified type with relaxed type checks.
198
/// For example, `true` may be converted to 1.0.
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
215
/// Extracts the specified type with strict type checks, or constructs the
216
/// default value when the field is not present
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
226
/// @brief Changes parsing behavior when duplicate fields are encountered.
227
/// Should not be used normally.
228
/// @details Should be called before the first field access. Only affects
229
/// documents. Default policy is to throw an exception when duplicate fields
230
/// are encountered.
231
/// @warning At most one value will be read, all others will be discarded and
232
/// cannot be serialized back!
233
void
SetDuplicateFieldsPolicy
(
DuplicateFieldsPolicy
);
234
235
/// Throws a MemberMissingException if the selected element does not exist
236
void
CheckNotMissing
()
const
;
237
238
/// @brief Throws a TypeMismatchException if the selected element
239
/// is not an array or null
240
void
CheckArrayOrNull
()
const
;
241
242
/// @brief Throws a TypeMismatchException if the selected element
243
/// is not a document or null
244
void
CheckDocumentOrNull
()
const
;
245
246
/// @cond
247
/// Same, for parsing capabilities
248
void
CheckObjectOrNull()
const
{
CheckDocumentOrNull
(
)
; }
249
250
/// @brief Returns an array as its internal representation (BSON document),
251
/// internal use only
252
Document
GetInternalArrayDocument()
const
;
253
/// @endcond
254
255
protected
:
256
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
278
/// @cond
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
;
315
/// @endcond
316
317
/// @brief Wrapper for handy python-like iteration over a map
318
///
319
/// @snippet universal/src/formats/common/items_test.cpp Items const iteration
320
using
formats::common::Items;
321
322
}
// namespace formats::bson
323
324
/// Although we provide user defined literals, please beware that
325
/// 'using namespace ABC' may contradict code style of your company.
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