userver: userver/formats/yaml/value.hpp Source File
Loading...
Searching...
No Matches
value.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <type_traits>
7
15
16USERVER_NAMESPACE_BEGIN
17
18namespace formats::yaml {
19
20class ValueBuilder;
21
22namespace impl {
23formats::yaml::Value FromStringAllowRepeatedKeys(const std::string& doc);
24} // namespace impl
25
40class Value final {
41public:
42 struct IterTraits {
43 using native_iter = YAML::const_iterator;
44 using value_type = formats::yaml::Value;
45 using reference = const formats::yaml::Value&;
46 using pointer = const formats::yaml::Value*;
47 };
49
50 using const_iterator = Iterator<IterTraits>;
51 using Exception = formats::yaml::Exception;
52 using ParseException = formats::yaml::ParseException;
53 using ExceptionWithPath = formats::yaml::ExceptionWithPath;
54 using Builder = ValueBuilder;
55
57 Value() noexcept;
58
59 // NOLINTNEXTLINE(performance-noexcept-move-constructor)
60 Value(Value&&);
61 Value(const Value&);
62 // NOLINTNEXTLINE(performance-noexcept-move-constructor)
63 Value& operator=(Value&&);
64 Value& operator=(const Value&);
65
66 template <class T>
67 Value& operator=(T&&) && {
68 static_assert(
69 !sizeof(T),
70 "You're assigning to a temporary formats::yaml::Value! Use "
71 "formats::yaml::ValueBuilder for data modifications."
72 );
73 return *this;
74 }
75
76 ~Value();
77
82 Value(Value&& other, std::string path_prefix);
83
86 Value operator[](std::string_view key) const;
87
92 Value operator[](std::size_t index) const;
93
96 const_iterator begin() const;
97
100 const_iterator end() const;
101
104 bool IsEmpty() const;
105
108 std::size_t GetSize() const;
109
112 bool operator==(const Value& other) const;
113
117 bool IsMissing() const;
118
120 bool IsNull() const noexcept;
121
123 bool IsBool() const noexcept;
124
126 bool IsInt() const noexcept;
127
129 bool IsInt64() const noexcept;
130
132 bool IsUInt64() const noexcept;
133
135 bool IsDouble() const noexcept;
136
138 bool IsString() const noexcept;
139
141 bool IsArray() const noexcept;
142
144 bool IsObject() const noexcept;
145
155 template <typename T>
156 auto As() const;
157
161 template <typename T, typename First, typename... Rest>
162 auto As(First&& default_arg, Rest&&... more_default_args) const;
163
167 template <typename T>
168 auto As(DefaultConstructed) const;
169
171 bool HasMember(std::string_view key) const;
172
174 std::string GetPath() const;
175
180 int GetColumn() const;
181
186 int GetLine() const;
187
206 std::string_view GetTag() const;
207
212 Value Clone() const;
213
215 void CheckNotMissing() const;
216
218 void CheckArray() const;
219
221 void CheckArrayOrNull() const;
222
224 void CheckObjectOrNull() const;
225
227 void CheckObject() const;
228
230 void CheckString() const;
231
234
237 void CheckInBounds(std::size_t index) const;
238
240 bool IsRoot() const noexcept;
241
244 bool DebugIsReferencingSameMemory(const Value& other) const;
245
246private:
247 class EmplaceEnabler {};
248
249public:
251 Value(EmplaceEnabler, const YAML::Node& value, const formats::yaml::Path& path, std::string_view key);
252
253 Value(EmplaceEnabler, const YAML::Node& value, const formats::yaml::Path& path, size_t index);
254
255 Value CloneWithReplacedPath(std::string&& new_path) const;
257
258private:
259 Value(const YAML::Node& root) noexcept;
260
261 static Value MakeNonRoot(const YAML::Node& value, const formats::yaml::Path& path, std::string_view key);
262 static Value MakeNonRoot(const YAML::Node& val, const formats::yaml::Path& path, size_t index);
263
264 const YAML::Node& GetNative() const;
265 YAML::Node& GetNative();
266 int GetExtendedType() const;
267
268 template <class T>
269 bool IsConvertibleToArithmetic() const;
270
271 template <class T>
272 T ValueAsArithmetic() const;
273
274 static constexpr std::size_t kNativeNodeSize = 64;
275 static constexpr std::size_t kNativeAlignment = alignof(void*);
276
279
280 friend class Iterator<IterTraits>;
281 friend class ValueBuilder;
282
283 friend bool Parse(const Value& value, parse::To<bool>);
284 friend int64_t Parse(const Value& value, parse::To<int64_t>);
285 friend uint64_t Parse(const Value& value, parse::To<uint64_t>);
286 friend double Parse(const Value& value, parse::To<double>);
287 friend std::string Parse(const Value& value, parse::To<std::string>);
288
289 friend formats::yaml::Value FromString(const std::string&);
290 friend formats::yaml::Value FromStream(std::istream&);
291 friend void Serialize(const formats::yaml::Value&, std::ostream&);
292 friend formats::yaml::Value impl::FromStringAllowRepeatedKeys(const std::string& doc);
293};
294
295template <typename T>
296auto Value::As() const {
297 static_assert(
298 formats::common::impl::HasParse<Value, T>,
299 "There is no `Parse(const Value&, formats::parse::To<T>)` in namespace of `T` or `formats::parse`. "
300 "Probably you forgot to include the <userver/formats/parse/common_containers.hpp> or you "
301 "have not provided a `Parse` function overload."
302 );
303
304 return Parse(*this, formats::parse::To<T>{});
305}
306
307bool Parse(const Value& value, parse::To<bool>);
308
309int64_t Parse(const Value& value, parse::To<int64_t>);
310
311uint64_t Parse(const Value& value, parse::To<uint64_t>);
312
313double Parse(const Value& value, parse::To<double>);
314
315std::string Parse(const Value& value, parse::To<std::string>);
316
317template <typename T, typename First, typename... Rest>
318auto Value::As(First&& default_arg, Rest&&... more_default_args) const {
319 if (IsMissing() || IsNull()) {
320 // intended raw ctor call, sometimes casts
321 // NOLINTNEXTLINE(google-readability-casting)
322 return decltype(As<T>())(std::forward<First>(default_arg), std::forward<Rest>(more_default_args)...);
323 }
324 return As<T>();
325}
326
327template <typename T>
329 return (IsMissing() || IsNull()) ? decltype(As<T>())() : As<T>();
330}
331
336
337} // namespace formats::yaml
338
341namespace formats::literals {
342
343yaml::Value operator""_yaml(const char* str, std::size_t len);
344
345} // namespace formats::literals
346
347USERVER_NAMESPACE_END