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 <mutex>
26#include <sstream>
27#include <string>
28
29namespace beast {
30
31//------------------------------------------------------------------------------
32
35{
36public:
37 class Map;
38 class Set;
39 class Source;
40
41 PropertyStream() = default;
42 virtual ~PropertyStream() = default;
43
44protected:
45 virtual void
46 map_begin() = 0;
47 virtual void
48 map_begin(std::string const& key) = 0;
49 virtual void
50 map_end() = 0;
51
52 virtual void
53 add(std::string const& key, std::string const& value) = 0;
54
55 void
56 add(std::string const& key, char const* value)
57 {
58 add(key, std::string(value));
59 }
60
61 template <typename Value>
62 void
63 lexical_add(std::string const& key, Value value)
64 {
66 ss << value;
67 add(key, ss.str());
68 }
69
70 virtual void
71 add(std::string const& key, bool value);
72 virtual void
73 add(std::string const& key, char value);
74 virtual void
75 add(std::string const& key, signed char value);
76 virtual void
77 add(std::string const& key, unsigned char value);
78 virtual void
79 add(std::string const& key, short value);
80 virtual void
81 add(std::string const& key, unsigned short value);
82 virtual void
83 add(std::string const& key, int value);
84 virtual void
85 add(std::string const& key, unsigned int value);
86 virtual void
87 add(std::string const& key, long value);
88 virtual void
89 add(std::string const& key, unsigned long value);
90 virtual void
91 add(std::string const& key, long long value);
92 virtual void
93 add(std::string const& key, unsigned long long value);
94 virtual void
95 add(std::string const& key, float value);
96 virtual void
97 add(std::string const& key, double value);
98 virtual void
99 add(std::string const& key, long double value);
100
101 virtual void
103 virtual void
104 array_begin(std::string const& key) = 0;
105 virtual void
107
108 virtual void
109 add(std::string const& value) = 0;
110
111 void
112 add(char const* value)
113 {
114 add(std::string(value));
115 }
116
117 template <typename Value>
118 void
119 lexical_add(Value value)
120 {
122 ss << value;
123 add(ss.str());
124 }
125
126 virtual void
127 add(bool value);
128 virtual void
129 add(char value);
130 virtual void
131 add(signed char value);
132 virtual void
133 add(unsigned char value);
134 virtual void
135 add(short value);
136 virtual void
137 add(unsigned short value);
138 virtual void
139 add(int value);
140 virtual void
141 add(unsigned int value);
142 virtual void
143 add(long value);
144 virtual void
145 add(unsigned long value);
146 virtual void
147 add(long long value);
148 virtual void
149 add(unsigned long long value);
150 virtual void
151 add(float value);
152 virtual void
153 add(double value);
154 virtual void
155 add(long double value);
156
157private:
158 class Item;
159 class Proxy;
160};
161
162//------------------------------------------------------------------------------
163//
164// Item
165//
166//------------------------------------------------------------------------------
167
168class PropertyStream::Item : public List<Item>::Node
169{
170public:
171 explicit Item(Source* source);
172 Source&
173 source() const;
174 Source*
175 operator->() const;
176 Source&
177 operator*() const;
178
179private:
181};
182
183//------------------------------------------------------------------------------
184//
185// Proxy
186//
187//------------------------------------------------------------------------------
188
190{
191private:
192 Map const* m_map;
195
196public:
197 Proxy(Map const& map, std::string const& key);
198 Proxy(Proxy const& other);
199 ~Proxy();
200
201 template <typename Value>
202 Proxy&
203 operator=(Value value);
204
206 operator<<(std::ostream& manip(std::ostream&)) const;
207
208 template <typename T>
210 operator<<(T const& t) const
211 {
212 return m_ostream << t;
213 }
214};
215
216//------------------------------------------------------------------------------
217//
218// Map
219//
220//------------------------------------------------------------------------------
221
223{
224private:
226
227public:
228 explicit Map(PropertyStream& stream);
229 explicit Map(Set& parent);
230 Map(std::string const& key, Map& parent);
231 Map(std::string const& key, PropertyStream& stream);
232 ~Map();
233
234 Map(Map const&) = delete;
235 Map&
236 operator=(Map const&) = delete;
237
239 stream();
240 PropertyStream const&
241 stream() const;
242
243 template <typename Value>
244 void
245 add(std::string const& key, Value value) const
246 {
247 m_stream.add(key, value);
248 }
249
250 template <typename Key, typename Value>
251 void
252 add(Key key, Value value) const
253 {
255 ss << key;
256 add(ss.str(), value);
257 }
258
259 Proxy
260 operator[](std::string const& key);
261
262 Proxy
263 operator[](char const* key)
264 {
265 return Proxy(*this, key);
266 }
267
268 template <typename Key>
269 Proxy
270 operator[](Key key) const
271 {
273 ss << key;
274 return Proxy(*this, ss.str());
275 }
276};
277
278//--------------------------------------------------------------------------
279
280template <typename Value>
283{
284 m_map->add(m_key, value);
285 return *this;
286}
287
288//--------------------------------------------------------------------------
289//
290// Set
291//
292//------------------------------------------------------------------------------
293
295{
296private:
298
299public:
300 Set(std::string const& key, Map& map);
301 Set(std::string const& key, PropertyStream& stream);
302 ~Set();
303
304 Set(Set const&) = delete;
305 Set&
306 operator=(Set const&) = delete;
307
309 stream();
310 PropertyStream const&
311 stream() const;
312
313 template <typename Value>
314 void
315 add(Value value) const
316 {
317 m_stream.add(value);
318 }
319};
320
321//------------------------------------------------------------------------------
322//
323// Source
324//
325//------------------------------------------------------------------------------
326
329{
330private:
336
337public:
338 explicit Source(std::string const& name);
339 virtual ~Source();
340
341 Source(Source const&) = delete;
342 Source&
343 operator=(Source const&) = delete;
344
346 std::string const&
347 name() const;
348
350 void
351 add(Source& source);
352
356 template <class Derived>
357 Derived*
358 add(Derived* child)
359 {
360 add(*static_cast<Source*>(child));
361 return child;
362 }
363
365 void
366 remove(Source& child);
367
369 void
370 removeAll();
371
373 void
374 write_one(PropertyStream& stream);
375
377 void
378 write(PropertyStream& stream);
379
385 void
386 write(PropertyStream& stream, std::string const& path);
387
404 find(std::string path);
405
406 Source*
407 find_one_deep(std::string const& name);
409 find_path(std::string path);
411 find_one(std::string const& name);
412
413 static bool
414 peel_leading_slash(std::string* path);
415 static bool
416 peel_trailing_slashstar(std::string* path);
417 static std::string
418 peel_name(std::string* path);
419
420 //--------------------------------------------------------------------------
421
425 virtual void
426 onWrite(Map&);
427};
428
429} // namespace beast
430
431#endif
Intrusive doubly linked list.
Definition: List.h:279
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)