rippled
Loading...
Searching...
No Matches
PropertyStream.h
1#ifndef BEAST_UTILITY_PROPERTYSTREAM_H_INCLUDED
2#define BEAST_UTILITY_PROPERTYSTREAM_H_INCLUDED
3
4#include <xrpl/beast/core/List.h>
5
6#include <mutex>
7#include <sstream>
8#include <string>
9
10namespace beast {
11
12//------------------------------------------------------------------------------
13
16{
17public:
18 class Map;
19 class Set;
20 class Source;
21
22 PropertyStream() = default;
23 virtual ~PropertyStream() = default;
24
25protected:
26 virtual void
27 map_begin() = 0;
28 virtual void
29 map_begin(std::string const& key) = 0;
30 virtual void
31 map_end() = 0;
32
33 virtual void
34 add(std::string const& key, std::string const& value) = 0;
35
36 void
37 add(std::string const& key, char const* value)
38 {
39 add(key, std::string(value));
40 }
41
42 template <typename Value>
43 void
44 lexical_add(std::string const& key, Value value)
45 {
47 ss << value;
48 add(key, ss.str());
49 }
50
51 virtual void
52 add(std::string const& key, bool value);
53 virtual void
54 add(std::string const& key, char value);
55 virtual void
56 add(std::string const& key, signed char value);
57 virtual void
58 add(std::string const& key, unsigned char value);
59 virtual void
60 add(std::string const& key, short value);
61 virtual void
62 add(std::string const& key, unsigned short value);
63 virtual void
64 add(std::string const& key, int value);
65 virtual void
66 add(std::string const& key, unsigned int value);
67 virtual void
68 add(std::string const& key, long value);
69 virtual void
70 add(std::string const& key, unsigned long value);
71 virtual void
72 add(std::string const& key, long long value);
73 virtual void
74 add(std::string const& key, unsigned long long value);
75 virtual void
76 add(std::string const& key, float value);
77 virtual void
78 add(std::string const& key, double value);
79 virtual void
80 add(std::string const& key, long double value);
81
82 virtual void
84 virtual void
85 array_begin(std::string const& key) = 0;
86 virtual void
87 array_end() = 0;
88
89 virtual void
90 add(std::string const& value) = 0;
91
92 void
93 add(char const* value)
94 {
95 add(std::string(value));
96 }
97
98 template <typename Value>
99 void
100 lexical_add(Value value)
101 {
103 ss << value;
104 add(ss.str());
105 }
106
107 virtual void
108 add(bool value);
109 virtual void
110 add(char value);
111 virtual void
112 add(signed char value);
113 virtual void
114 add(unsigned char value);
115 virtual void
116 add(short value);
117 virtual void
118 add(unsigned short value);
119 virtual void
120 add(int value);
121 virtual void
122 add(unsigned int value);
123 virtual void
124 add(long value);
125 virtual void
126 add(unsigned long value);
127 virtual void
128 add(long long value);
129 virtual void
130 add(unsigned long long value);
131 virtual void
132 add(float value);
133 virtual void
134 add(double value);
135 virtual void
136 add(long double value);
137
138private:
139 class Item;
140 class Proxy;
141};
142
143//------------------------------------------------------------------------------
144//
145// Item
146//
147//------------------------------------------------------------------------------
148
149class PropertyStream::Item : public List<Item>::Node
150{
151public:
152 explicit Item(Source* source);
153 Source&
154 source() const;
155 Source*
156 operator->() const;
157 Source&
158 operator*() const;
159
160private:
162};
163
164//------------------------------------------------------------------------------
165//
166// Proxy
167//
168//------------------------------------------------------------------------------
169
171{
172private:
173 Map const* m_map;
176
177public:
178 Proxy(Map const& map, std::string const& key);
179 Proxy(Proxy const& other);
180 ~Proxy();
181
182 template <typename Value>
183 Proxy&
184 operator=(Value value);
185
187 operator<<(std::ostream& manip(std::ostream&)) const;
188
189 template <typename T>
191 operator<<(T const& t) const
192 {
193 return m_ostream << t;
194 }
195};
196
197//------------------------------------------------------------------------------
198//
199// Map
200//
201//------------------------------------------------------------------------------
202
204{
205private:
207
208public:
209 explicit Map(PropertyStream& stream);
210 explicit Map(Set& parent);
211 Map(std::string const& key, Map& parent);
212 Map(std::string const& key, PropertyStream& stream);
213 ~Map();
214
215 Map(Map const&) = delete;
216 Map&
217 operator=(Map const&) = delete;
218
220 stream();
221 PropertyStream const&
222 stream() const;
223
224 template <typename Value>
225 void
226 add(std::string const& key, Value value) const
227 {
228 m_stream.add(key, value);
229 }
230
231 template <typename Key, typename Value>
232 void
233 add(Key key, Value value) const
234 {
236 ss << key;
237 add(ss.str(), value);
238 }
239
240 Proxy
241 operator[](std::string const& key);
242
243 Proxy
244 operator[](char const* key)
245 {
246 return Proxy(*this, key);
247 }
248
249 template <typename Key>
250 Proxy
251 operator[](Key key) const
252 {
254 ss << key;
255 return Proxy(*this, ss.str());
256 }
257};
258
259//--------------------------------------------------------------------------
260
261template <typename Value>
264{
265 m_map->add(m_key, value);
266 return *this;
267}
268
269//--------------------------------------------------------------------------
270//
271// Set
272//
273//------------------------------------------------------------------------------
274
276{
277private:
279
280public:
281 Set(std::string const& key, Map& map);
282 Set(std::string const& key, PropertyStream& stream);
283 ~Set();
284
285 Set(Set const&) = delete;
286 Set&
287 operator=(Set const&) = delete;
288
290 stream();
291 PropertyStream const&
292 stream() const;
293
294 template <typename Value>
295 void
296 add(Value value) const
297 {
298 m_stream.add(value);
299 }
300};
301
302//------------------------------------------------------------------------------
303//
304// Source
305//
306//------------------------------------------------------------------------------
307
310{
311private:
317
318public:
319 explicit Source(std::string const& name);
320 virtual ~Source();
321
322 Source(Source const&) = delete;
323 Source&
324 operator=(Source const&) = delete;
325
327 std::string const&
328 name() const;
329
331 void
332 add(Source& source);
333
337 template <class Derived>
338 Derived*
339 add(Derived* child)
340 {
341 add(*static_cast<Source*>(child));
342 return child;
343 }
344
346 void
347 remove(Source& child);
348
350 void
351 removeAll();
352
354 void
355 write_one(PropertyStream& stream);
356
358 void
359 write(PropertyStream& stream);
360
366 void
367 write(PropertyStream& stream, std::string const& path);
368
385 find(std::string path);
386
387 Source*
388 find_one_deep(std::string const& name);
390 find_path(std::string path);
392 find_one(std::string const& name);
393
394 static bool
395 peel_leading_slash(std::string* path);
396 static bool
397 peel_trailing_slashstar(std::string* path);
398 static std::string
399 peel_name(std::string* path);
400
401 //--------------------------------------------------------------------------
402
406 virtual void
407 onWrite(Map&);
408};
409
410} // namespace beast
411
412#endif
Intrusive doubly linked list.
Definition List.h:260
Map(Map const &)=delete
Proxy operator[](Key key) const
Proxy operator[](std::string const &key)
void add(Key key, Value value) const
void add(std::string const &key, Value value) const
Map & operator=(Map const &)=delete
Proxy operator[](char const *key)
std::ostream & operator<<(std::ostream &manip(std::ostream &)) const
Proxy & operator=(Value value)
Set(Set const &)=delete
void add(Value value) const
Set & operator=(Set const &)=delete
Subclasses can be called to write to a stream and have children.
Derived * add(Derived *child)
Add a child source by pointer.
Source(Source const &)=delete
Source & operator=(Source const &)=delete
Abstract stream with RAII containers that produce a property tree.
void add(std::string const &key, char const *value)
virtual void add(std::string const &key, std::string const &value)=0
virtual void array_end()=0
virtual void add(std::string const &value)=0
virtual ~PropertyStream()=default
virtual void map_begin()=0
void lexical_add(Value value)
virtual void array_begin(std::string const &key)=0
virtual void array_begin()=0
virtual void map_begin(std::string const &key)=0
virtual void map_end()=0
void lexical_add(std::string const &key, Value value)
void add(char const *value)
T str(T... args)