rippled
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 <ripple/beast/core/List.h>
24 
25 #include <cstdint>
26 #include <mutex>
27 #include <sstream>
28 #include <string>
29 #include <utility>
30 
31 namespace beast {
32 
33 //------------------------------------------------------------------------------
34 
37 {
38 public:
39  class Map;
40  class Set;
41  class Source;
42 
43  PropertyStream() = default;
44  virtual ~PropertyStream() = default;
45 
46 protected:
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, wchar_t value);
82 #if 0
83  virtual void add (std::string const& key, char16_t value);
84  virtual void add (std::string const& key, char32_t value);
85 #endif
86  virtual void
87  add(std::string const& key, short value);
88  virtual void
89  add(std::string const& key, unsigned short value);
90  virtual void
91  add(std::string const& key, int value);
92  virtual void
93  add(std::string const& key, unsigned int value);
94  virtual void
95  add(std::string const& key, long value);
96  virtual void
97  add(std::string const& key, unsigned long value);
98  virtual void
99  add(std::string const& key, long long value);
100  virtual void
101  add(std::string const& key, unsigned long long value);
102  virtual void
103  add(std::string const& key, float value);
104  virtual void
105  add(std::string const& key, double value);
106  virtual void
107  add(std::string const& key, long double value);
108 
109  virtual void
110  array_begin() = 0;
111  virtual void
112  array_begin(std::string const& key) = 0;
113  virtual void
114  array_end() = 0;
115 
116  virtual void
117  add(std::string const& value) = 0;
118 
119  void
120  add(char const* value)
121  {
122  add(std::string(value));
123  }
124 
125  template <typename Value>
126  void
127  lexical_add(Value value)
128  {
130  ss << value;
131  add(ss.str());
132  }
133 
134  virtual void
135  add(bool value);
136  virtual void
137  add(char value);
138  virtual void
139  add(signed char value);
140  virtual void
141  add(unsigned char value);
142  virtual void
143  add(wchar_t value);
144 #if 0
145  virtual void add (char16_t value);
146  virtual void add (char32_t value);
147 #endif
148  virtual void
149  add(short value);
150  virtual void
151  add(unsigned short value);
152  virtual void
153  add(int value);
154  virtual void
155  add(unsigned int value);
156  virtual void
157  add(long value);
158  virtual void
159  add(unsigned long value);
160  virtual void
161  add(long long value);
162  virtual void
163  add(unsigned long long value);
164  virtual void
165  add(float value);
166  virtual void
167  add(double value);
168  virtual void
169  add(long double value);
170 
171 private:
172  class Item;
173  class Proxy;
174 };
175 
176 //------------------------------------------------------------------------------
177 //
178 // Item
179 //
180 //------------------------------------------------------------------------------
181 
182 class PropertyStream::Item : public List<Item>::Node
183 {
184 public:
185  explicit Item(Source* source);
186  Source&
187  source() const;
188  Source*
189  operator->() const;
190  Source&
191  operator*() const;
192 
193 private:
195 };
196 
197 //------------------------------------------------------------------------------
198 //
199 // Proxy
200 //
201 //------------------------------------------------------------------------------
202 
204 {
205 private:
206  Map const* m_map;
209 
210 public:
211  Proxy(Map const& map, std::string const& key);
212  Proxy(Proxy const& other);
213  ~Proxy();
214 
215  template <typename Value>
216  Proxy&
217  operator=(Value value);
218 
219  std::ostream&
220  operator<<(std::ostream& manip(std::ostream&)) const;
221 
222  template <typename T>
223  std::ostream&
224  operator<<(T const& t) const
225  {
226  return m_ostream << t;
227  }
228 };
229 
230 //------------------------------------------------------------------------------
231 //
232 // Map
233 //
234 //------------------------------------------------------------------------------
235 
237 {
238 private:
240 
241 public:
242  explicit Map(PropertyStream& stream);
243  explicit Map(Set& parent);
244  Map(std::string const& key, Map& parent);
245  Map(std::string const& key, PropertyStream& stream);
246  ~Map();
247 
248  Map(Map const&) = delete;
249  Map&
250  operator=(Map const&) = delete;
251 
253  stream();
254  PropertyStream const&
255  stream() const;
256 
257  template <typename Value>
258  void
259  add(std::string const& key, Value value) const
260  {
261  m_stream.add(key, value);
262  }
263 
264  template <typename Key, typename Value>
265  void
266  add(Key key, Value value) const
267  {
269  ss << key;
270  add(ss.str(), value);
271  }
272 
273  Proxy
274  operator[](std::string const& key);
275 
276  Proxy
277  operator[](char const* key)
278  {
279  return Proxy(*this, key);
280  }
281 
282  template <typename Key>
283  Proxy
284  operator[](Key key) const
285  {
287  ss << key;
288  return Proxy(*this, ss.str());
289  }
290 };
291 
292 //--------------------------------------------------------------------------
293 
294 template <typename Value>
297 {
298  m_map->add(m_key, value);
299  return *this;
300 }
301 
302 //--------------------------------------------------------------------------
303 //
304 // Set
305 //
306 //------------------------------------------------------------------------------
307 
309 {
310 private:
312 
313 public:
314  Set(std::string const& key, Map& map);
315  Set(std::string const& key, PropertyStream& stream);
316  ~Set();
317 
318  Set(Set const&) = delete;
319  Set&
320  operator=(Set const&) = delete;
321 
323  stream();
324  PropertyStream const&
325  stream() const;
326 
327  template <typename Value>
328  void
329  add(Value value) const
330  {
331  m_stream.add(value);
332  }
333 };
334 
335 //------------------------------------------------------------------------------
336 //
337 // Source
338 //
339 //------------------------------------------------------------------------------
340 
343 {
344 private:
350 
351 public:
352  explicit Source(std::string const& name);
353  virtual ~Source();
354 
355  Source(Source const&) = delete;
356  Source&
357  operator=(Source const&) = delete;
358 
360  std::string const&
361  name() const;
362 
364  void
365  add(Source& source);
366 
370  template <class Derived>
371  Derived*
372  add(Derived* child)
373  {
374  add(*static_cast<Source*>(child));
375  return child;
376  }
377 
379  void
380  remove(Source& child);
381 
383  void
384  removeAll();
385 
387  void
388  write_one(PropertyStream& stream);
389 
391  void
392  write(PropertyStream& stream);
393 
399  void
400  write(PropertyStream& stream, std::string const& path);
401 
418  find(std::string path);
419 
420  Source*
421  find_one_deep(std::string const& name);
423  find_path(std::string path);
425  find_one(std::string const& name);
426 
427  static bool
428  peel_leading_slash(std::string* path);
429  static bool
430  peel_trailing_slashstar(std::string* path);
431  static std::string
432  peel_name(std::string* path);
433 
434  //--------------------------------------------------------------------------
435 
439  virtual void
440  onWrite(Map&);
441 };
442 
443 } // namespace beast
444 
445 #endif
sstream
std::string
STL class.
utility
beast::PropertyStream::map_end
virtual void map_end()=0
beast::PropertyStream::Map
Definition: PropertyStream.h:236
beast::PropertyStream::Map::stream
PropertyStream & stream()
Definition: beast_PropertyStream.cpp:118
beast::PropertyStream::Item::Item
Item(Source *source)
Definition: beast_PropertyStream.cpp:34
std::pair
beast::PropertyStream::Source::add
Derived * add(Derived *child)
Add a child source by pointer.
Definition: PropertyStream.h:372
beast::PropertyStream::Source
Subclasses can be called to write to a stream and have children.
Definition: PropertyStream.h:342
beast::PropertyStream::map_begin
virtual void map_begin()=0
beast::PropertyStream::Item::operator*
Source & operator*() const
Definition: beast_PropertyStream.cpp:51
std::stringstream
STL class.
beast::PropertyStream::add
void add(std::string const &key, char const *value)
Definition: PropertyStream.h:58
std::recursive_mutex
STL class.
beast::PropertyStream::Proxy::~Proxy
~Proxy()
Definition: beast_PropertyStream.cpp:72
beast::PropertyStream::Item::m_source
Source * m_source
Definition: PropertyStream.h:194
beast::PropertyStream::Source::parent_
Source * parent_
Definition: PropertyStream.h:348
beast::PropertyStream::Proxy::m_map
Map const * m_map
Definition: PropertyStream.h:206
beast::PropertyStream::Set
Definition: PropertyStream.h:308
beast::PropertyStream::Map::operator[]
Proxy operator[](Key key) const
Definition: PropertyStream.h:284
beast::PropertyStream::Map::add
void add(Key key, Value value) const
Definition: PropertyStream.h:266
beast::PropertyStream::Set::m_stream
PropertyStream & m_stream
Definition: PropertyStream.h:311
beast::PropertyStream::Item::operator->
Source * operator->() const
Definition: beast_PropertyStream.cpp:45
beast::PropertyStream::PropertyStream
PropertyStream()=default
beast::PropertyStream::Source::m_name
const std::string m_name
Definition: PropertyStream.h:345
beast::PropertyStream::Proxy::operator<<
std::ostream & operator<<(std::ostream &manip(std::ostream &)) const
Definition: beast_PropertyStream.cpp:80
beast::PropertyStream::Item::source
Source & source() const
Definition: beast_PropertyStream.cpp:39
beast::PropertyStream::add
virtual void add(std::string const &key, std::string const &value)=0
beast::PropertyStream::Source::lock_
std::recursive_mutex lock_
Definition: PropertyStream.h:346
beast::PropertyStream::Map::operator[]
Proxy operator[](char const *key)
Definition: PropertyStream.h:277
beast::PropertyStream::Map::add
void add(std::string const &key, Value value) const
Definition: PropertyStream.h:259
std::ostream
STL class.
beast::PropertyStream::Set::add
void add(Value value) const
Definition: PropertyStream.h:329
beast::PropertyStream::array_begin
virtual void array_begin()=0
beast::PropertyStream::~PropertyStream
virtual ~PropertyStream()=default
beast::PropertyStream::Proxy
Definition: PropertyStream.h:203
cstdint
beast::PropertyStream::array_end
virtual void array_end()=0
beast::PropertyStream::Source::item_
Item item_
Definition: PropertyStream.h:347
std::ostringstream
STL class.
beast::PropertyStream::Proxy::m_key
std::string m_key
Definition: PropertyStream.h:207
beast::PropertyStream::Map::operator[]
Proxy operator[](std::string const &key)
Definition: beast_PropertyStream.cpp:130
beast::PropertyStream::Map::Map
Map(PropertyStream &stream)
Definition: beast_PropertyStream.cpp:91
beast::PropertyStream::Source::children_
List< Item > children_
Definition: PropertyStream.h:349
mutex
std::stringstream::str
T str(T... args)
beast::PropertyStream::Map::operator=
Map & operator=(Map const &)=delete
beast::PropertyStream::Proxy::operator=
Proxy & operator=(Value value)
beast::PropertyStream::lexical_add
void lexical_add(Value value)
Definition: PropertyStream.h:127
beast::PropertyStream::Proxy::m_ostream
std::ostringstream m_ostream
Definition: PropertyStream.h:208
beast::PropertyStream::Map::~Map
~Map()
Definition: beast_PropertyStream.cpp:112
beast::PropertyStream::add
void add(char const *value)
Definition: PropertyStream.h:120
beast::PropertyStream
Abstract stream with RAII containers that produce a property tree.
Definition: PropertyStream.h:36
beast::PropertyStream::Map::m_stream
PropertyStream & m_stream
Definition: PropertyStream.h:239
beast::PropertyStream::Proxy::Proxy
Proxy(Map const &map, std::string const &key)
Definition: beast_PropertyStream.cpp:62
beast::PropertyStream::lexical_add
void lexical_add(std::string const &key, Value value)
Definition: PropertyStream.h:65
beast::List
Intrusive doubly linked list.
Definition: List.h:29
beast::PropertyStream::Item
Definition: PropertyStream.h:182
beast
Definition: base_uint.h:585
string