userver: userver/formats/bson/value_builder.hpp Source File
Loading...
Searching...
No Matches
value_builder.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#include <string_view>
11
20
21USERVER_NAMESPACE_BEGIN
22
23namespace formats::bson {
24
40public:
41 using iterator = Iterator<ValueBuilder>;
42 using Type = formats::common::Type;
43
46
49
52 explicit ValueBuilder(impl::ValueImplPtr);
54
55 ValueBuilder(const ValueBuilder& other);
56 // NOLINTNEXTLINE(performance-noexcept-move-constructor)
58 ValueBuilder& operator=(const ValueBuilder& other);
59 // NOLINTNEXTLINE(performance-noexcept-move-constructor)
60 ValueBuilder& operator=(ValueBuilder&& other);
61
65 ValueBuilder(const Document&);
66 // NOLINTNEXTLINE(performance-noexcept-move-constructor)
68 // NOLINTNEXTLINE(performance-noexcept-move-constructor)
71
74 /* implicit */ ValueBuilder(std::nullptr_t);
75 /* implicit */ ValueBuilder(bool);
76 /* implicit */ ValueBuilder(int);
77 /* implicit */ ValueBuilder(unsigned int);
78 /* implicit */ ValueBuilder(long);
79 /* implicit */ ValueBuilder(unsigned long);
80 /* implicit */ ValueBuilder(long long);
81 /* implicit */ ValueBuilder(unsigned long long);
82 /* implicit */ ValueBuilder(double);
83 /* implicit */ ValueBuilder(const char*);
84 /* implicit */ ValueBuilder(char*);
85 /* implicit */ ValueBuilder(std::string);
86 /* implicit */ ValueBuilder(std::string_view);
87 /* implicit */ ValueBuilder(const std::chrono::system_clock::time_point&);
88 /* implicit */ ValueBuilder(const Oid&);
89 /* implicit */ ValueBuilder(Binary);
90 /* implicit */ ValueBuilder(const Decimal128&);
91 /* implicit */ ValueBuilder(MinKey);
92 /* implicit */ ValueBuilder(MaxKey);
93 /* implicit */ ValueBuilder(const Timestamp&);
95
97 template <typename T>
98 ValueBuilder(const T& t)
99 : ValueBuilder(DoSerialize(t))
100 {}
101
105
108 ValueBuilder operator[](const std::string& name);
109
113 void EmplaceNocheck(std::string_view key, ValueBuilder value);
114
117 template <typename Tag, utils::StrongTypedefOps Ops>
118 requires(utils::IsStrongTypedefLoggable(Ops))
119 ValueBuilder operator[](const utils::StrongTypedef<Tag, std::string, Ops>& name) {
120 return (*this)[name.GetUnderlying()];
121 }
122
126 ValueBuilder operator[](uint32_t index);
127
130 void Remove(const std::string& key);
131
134 iterator begin();
135
138 iterator end();
139
143 bool IsEmpty() const;
144
146 bool IsNull() const noexcept;
147
149 bool IsBool() const noexcept;
150
152 bool IsInt() const noexcept;
153
155 bool IsInt64() const noexcept;
156
158 bool IsUInt64() const noexcept;
159
161 bool IsDouble() const noexcept;
162
164 bool IsString() const noexcept;
165
167 bool IsArray() const noexcept;
168
170 bool IsObject() const noexcept;
171
175 uint32_t GetSize() const;
176
180 bool HasMember(const std::string& name) const;
181
183 std::string GetPath() const;
184
188 void Resize(uint32_t size);
189
193 void PushBack(ValueBuilder&& elem);
194
198
199private:
200 void Assign(const impl::ValueImplPtr&);
201 void Assign(impl::ValueImplPtr&&);
202
203 template <typename T>
204 static Value DoSerialize(const T& t);
205
206 impl::ValueImplPtr impl_;
207};
208
209template <typename T>
210Value ValueBuilder::DoSerialize(const T& t) {
211 static_assert(
212 formats::common::impl::HasSerialize<Value, T>,
213 "There is no `Serialize(const T&, formats::serialize::To<bson::Value>)` "
214 "in namespace of `T` or `formats::serialize`. "
215 ""
216 "Probably you forgot to include the <userver/formats/serialize/common_containers.hpp> or you "
217 "have not provided a `Serialize` function overload."
218 );
219
220 return Serialize(t, formats::serialize::To<Value>());
221}
222
223} // namespace formats::bson
224
225USERVER_NAMESPACE_END