userver: userver/formats/yaml/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 <userver/compiler/impl/nodebug.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace formats::yaml {
15
32class ValueBuilder final {
33public:
34 struct IterTraits {
35 using native_iter = YAML::iterator;
36 using value_type = formats::yaml::ValueBuilder;
37 using reference = formats::yaml::ValueBuilder&;
38 using pointer = formats::yaml::ValueBuilder*;
39 };
40
41 using iterator = Iterator<IterTraits>;
42
45
48
49 ValueBuilder(const ValueBuilder& other);
50 // NOLINTNEXTLINE(performance-noexcept-move-constructor)
52 ValueBuilder& operator=(const ValueBuilder& other);
53 // NOLINTNEXTLINE(performance-noexcept-move-constructor)
54 ValueBuilder& operator=(ValueBuilder&& other);
55
58
61 ValueBuilder(std::nullptr_t)
62 : ValueBuilder()
63 {}
64 ValueBuilder(bool t);
65 ValueBuilder(const char* str);
66 ValueBuilder(char* str);
67 ValueBuilder(const std::string& str);
68 ValueBuilder(std::string_view str);
69 ValueBuilder(int t);
70 ValueBuilder(unsigned int t);
71 ValueBuilder(long t);
72 ValueBuilder(unsigned long t);
73 ValueBuilder(long long t);
74 ValueBuilder(unsigned long long t);
75 ValueBuilder(float t);
76 ValueBuilder(double t);
78
82
84 template <typename T>
85 ValueBuilder(const T& t)
86 : ValueBuilder(DoSerialize(t))
87 {}
88
91 ValueBuilder operator[](const std::string& key);
95 ValueBuilder operator[](std::size_t index);
98 template <typename Tag, utils::StrongTypedefOps Ops>
99 requires(utils::IsStrongTypedefLoggable(Ops))
101 return (*this)[key.GetUnderlying()];
102 }
103
106 void Remove(const std::string& key);
107
108 iterator begin();
109 iterator end();
110
113 bool IsEmpty() const;
114
116 bool IsNull() const noexcept;
117
119 bool IsBool() const noexcept;
120
122 bool IsInt() const noexcept;
123
125 bool IsInt64() const noexcept;
126
128 bool IsUInt64() const noexcept;
129
131 bool IsDouble() const noexcept;
132
134 bool IsString() const noexcept;
135
137 bool IsArray() const noexcept;
138
140 bool IsObject() const noexcept;
141
144 std::size_t GetSize() const;
145
147 bool HasMember(const char* key) const;
148
150 bool HasMember(const std::string& key) const;
151
155 void Resize(std::size_t size);
156
160
166
167private:
168 class EmplaceEnabler {};
169
170public:
172 ValueBuilder(EmplaceEnabler, const YAML::Node& value, const formats::yaml::Path& path, const std::string& key);
173
174 ValueBuilder(EmplaceEnabler, const YAML::Node& value, const formats::yaml::Path& path, size_t index);
176
177private:
178 static ValueBuilder MakeNonRoot(const YAML::Node& val, const formats::yaml::Path& path, const std::string& key);
179 static ValueBuilder MakeNonRoot(const YAML::Node& val, const formats::yaml::Path& path, size_t index);
180
181 void Copy(const ValueBuilder& from);
182 void Move(ValueBuilder&& from);
183 void NodeDataAssign(const formats::yaml::Value& from);
184
185 template <typename T>
186 USERVER_IMPL_NODEBUG_INLINE_FUNC static Value DoSerialize(const T& t) {
187 static_assert(
188 formats::common::impl::HasSerialize<Value, T>,
189 "There is no `Serialize(const T&, formats::serialize::To<yaml::Value>)` "
190 "in namespace of `T` or `formats::serialize`. "
191 ""
192 "Probably you forgot to include the <userver/formats/serialize/common_containers.hpp> or you "
193 "have not provided a `Serialize` function overload."
194 );
195
197 }
198
200
201 friend class Iterator<IterTraits>;
202};
203
204template <typename T>
205requires(std::is_integral<T>::value && sizeof(T) <= sizeof(long long))
207 using Type = std::conditional_t<std::is_signed<T>::value, long long, unsigned long long>;
208 return yaml::ValueBuilder(static_cast<Type>(value)).ExtractValue();
209}
210
211} // namespace formats::yaml
212
213USERVER_NAMESPACE_END