rippled
Loading...
Searching...
No Matches
PropertyStream.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of Beast: https://github.com/vinniefalco/Beast
4 Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef BEAST_UTILITY_PROPERTYSTREAM_H_INCLUDED
21#define BEAST_UTILITY_PROPERTYSTREAM_H_INCLUDED
22
23#include <xrpl/beast/core/List.h>
24
25#include <cstdint>
26#include <mutex>
27#include <sstream>
28#include <string>
29#include <utility>
30
31namespace beast {
32
33//------------------------------------------------------------------------------
34
37{
38public:
39 class Map;
40 class Set;
41 class Source;
42
43 PropertyStream() = default;
44 virtual ~PropertyStream() = default;
45
46protected:
47 virtual void
48 map_begin() = 0;
49 virtual void
50 map_begin(std::string const& key) = 0;
51 virtual void
52 map_end() = 0;
53
54 virtual void
55 add(std::string const& key, std::string const& value) = 0;
56
57 void
58 add(std::string const& key, char const* value)
59 {
60 add(key, std::string(value));
61 }
62
63 template <typename Value>
64 void
65 lexical_add(std::string const& key, Value value)
66 {
68 ss << value;
69 add(key, ss.str());
70 }
71
72 virtual void
73 add(std::string const& key, bool value);
74 virtual void
75 add(std::string const& key, char value);
76 virtual void
77 add(std::string const& key, signed char value);
78 virtual void
79 add(std::string const& key, unsigned char value);
80 virtual void
81 add(std::string const& key, short value);
82 virtual void
83 add(std::string const& key, unsigned short value);
84 virtual void
85 add(std::string const& key, int value);
86 virtual void
87 add(std::string const& key, unsigned int value);
88 virtual void
89 add(std::string const& key, long value);
90 virtual void
91 add(std::string const& key, unsigned long value);
92 virtual void
93 add(std::string const& key, long long value);
94 virtual void
95 add(std::string const& key, unsigned long long value);
96 virtual void
97 add(std::string const& key, float value);
98 virtual void
99 add(std::string const& key, double value);
100 virtual void
101 add(std::string const& key, long double value);
102
103 virtual void
105 virtual void
106 array_begin(std::string const& key) = 0;
107 virtual void
109
110 virtual void
111 add(std::string const& value) = 0;
112
113 void
114 add(char const* value)
115 {
116 add(std::string(value));
117 }
118
119 template <typename Value>
120 void
121 lexical_add(Value value)
122 {
124 ss << value;
125 add(ss.str());
126 }
127
128 virtual void
129 add(bool value);
130 virtual void
131 add(char value);
132 virtual void
133 add(signed char value);
134 virtual void
135 add(unsigned char value);
136 virtual void
137 add(short value);
138 virtual void
139 add(unsigned short value);
140 virtual void
141 add(int value);
142 virtual void
143 add(unsigned int value);
144 virtual void
145 add(long value);
146 virtual void
147 add(unsigned long value);
148 virtual void
149 add(long long value);
150 virtual void
151 add(unsigned long long value);
152 virtual void
153 add(float value);
154 virtual void
155 add(double value);
156 virtual void
157 add(long double value);
158
159private:
160 class Item;
161 class Proxy;
162};
163
164//------------------------------------------------------------------------------
165//
166// Item
167//
168//------------------------------------------------------------------------------
169
170class PropertyStream::Item : public List<Item>::Node
171{
172public:
173 explicit Item(Source* source);
174 Source&
175 source() const;
176 Source*
177 operator->() const;
178 Source&
179 operator*() const;
180
181private:
183};
184
185//------------------------------------------------------------------------------
186//
187// Proxy
188//
189//------------------------------------------------------------------------------
190
192{
193private:
194 Map const* m_map;
197
198public:
199 Proxy(Map const& map, std::string const& key);
200 Proxy(Proxy const& other);
201 ~Proxy();
202
203 template <typename Value>
204 Proxy&
205 operator=(Value value);
206
208 operator<<(std::ostream& manip(std::ostream&)) const;
209
210 template <typename T>
212 operator<<(T const& t) const
213 {
214 return m_ostream << t;
215 }
216};
217
218//------------------------------------------------------------------------------
219//
220// Map
221//
222//------------------------------------------------------------------------------
223
225{
226private:
228
229public:
230 explicit Map(PropertyStream& stream);
231 explicit Map(Set& parent);
232 Map(std::string const& key, Map& parent);
233 Map(std::string const& key, PropertyStream& stream);
234 ~Map();
235
236 Map(Map const&) = delete;
237 Map&
238 operator=(Map const&) = delete;
239
241 stream();
242 PropertyStream const&
243 stream() const;
244
245 template <typename Value>
246 void
247 add(std::string const& key, Value value) const
248 {
249 m_stream.add(key, value);
250 }
251
252 template <typename Key, typename Value>
253 void
254 add(Key key, Value value) const
255 {
257 ss << key;
258 add(ss.str(), value);
259 }
260
261 Proxy
262 operator[](std::string const& key);
263
264 Proxy
265 operator[](char const* key)
266 {
267 return Proxy(*this, key);
268 }
269
270 template <typename Key>
271 Proxy
272 operator[](Key key) const
273 {
275 ss << key;
276 return Proxy(*this, ss.str());
277 }
278};
279
280//--------------------------------------------------------------------------
281
282template <typename Value>
285{
286 m_map->add(m_key, value);
287 return *this;
288}
289
290//--------------------------------------------------------------------------
291//
292// Set
293//
294//------------------------------------------------------------------------------
295
297{
298private:
300
301public:
302 Set(std::string const& key, Map& map);
303 Set(std::string const& key, PropertyStream& stream);
304 ~Set();
305
306 Set(Set const&) = delete;
307 Set&
308 operator=(Set const&) = delete;
309
311 stream();
312 PropertyStream const&
313 stream() const;
314
315 template <typename Value>
316 void
317 add(Value value) const
318 {
319 m_stream.add(value);
320 }
321};
322
323//------------------------------------------------------------------------------
324//
325// Source
326//
327//------------------------------------------------------------------------------
328
331{
332private:
338
339public:
340 explicit Source(std::string const& name);
341 virtual ~Source();
342
343 Source(Source const&) = delete;
344 Source&
345 operator=(Source const&) = delete;
346
348 std::string const&
349 name() const;
350
352 void
353 add(Source& source);
354
358 template <class Derived>
359 Derived*
360 add(Derived* child)
361 {
362 add(*static_cast<Source*>(child));
363 return child;
364 }
365
367 void
368 remove(Source& child);
369
371 void
372 removeAll();
373
375 void
376 write_one(PropertyStream& stream);
377
379 void
380 write(PropertyStream& stream);
381
387 void
388 write(PropertyStream& stream, std::string const& path);
389
406 find(std::string path);
407
408 Source*
409 find_one_deep(std::string const& name);
411 find_path(std::string path);
413 find_one(std::string const& name);
414
415 static bool
416 peel_leading_slash(std::string* path);
417 static bool
418 peel_trailing_slashstar(std::string* path);
419 static std::string
420 peel_name(std::string* path);
421
422 //--------------------------------------------------------------------------
423
427 virtual void
428 onWrite(Map&);
429};
430
431} // namespace beast
432
433#endif
Intrusive doubly linked list.
Definition: List.h:280
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
std::ostringstream m_ostream
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.
std::recursive_mutex lock_
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)