userver: userver/formats/bson/value.hpp Source File
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
18
19USERVER_NAMESPACE_BEGIN
20
21namespace formats::bson {
22namespace impl {
23class BsonBuilder;
24class ValueImpl;
25} // namespace impl
26
27class Document;
28class ValueBuilder;
29
42class Value {
43public:
45
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
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
234
236 void CheckNotMissing() const;
237
240 void CheckArrayOrNull() const;
241
245
248 void CheckObjectOrNull() const { CheckDocumentOrNull(); }
249
252 Document GetInternalArrayDocument() const;
253
255 const impl::BsonHolder& GetBson() const;
257
258private:
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
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
279bool Parse(const Value& value, parse::To<bool>);
280
281int64_t Parse(const Value& value, parse::To<int64_t>);
282
283uint64_t Parse(const Value& value, parse::To<uint64_t>);
284
285double Parse(const Value& value, parse::To<double>);
286
287std::string Parse(const Value& value, parse::To<std::string>);
288
289std::chrono::system_clock::time_point Parse(const Value& value, parse::To<std::chrono::system_clock::time_point>);
290
291Oid Parse(const Value& value, parse::To<Oid>);
292
293Binary Parse(const Value& value, parse::To<Binary>);
294
295Decimal128 Parse(const Value& value, parse::To<Decimal128>);
296
297Timestamp Parse(const Value& value, parse::To<Timestamp>);
298
299Document Parse(const Value& value, parse::To<Document>);
300
301template <>
302bool Value::ConvertTo<bool>() const;
303
304template <>
305int64_t Value::ConvertTo<int64_t>() const;
306
307template <>
308uint64_t Value::ConvertTo<uint64_t>() const;
309
310template <>
311double Value::ConvertTo<double>() const;
312
313template <>
314std::string Value::ConvertTo<std::string>() const;
316
321
322} // namespace formats::bson
323
326namespace formats::literals {
327
328bson::Value operator""_bson(const char* str, std::size_t len);
329
330} // namespace formats::literals
331
332USERVER_NAMESPACE_END