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, 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
104  array_begin() = 0;
105  virtual void
106  array_begin(std::string const& key) = 0;
107  virtual void
108  array_end() = 0;
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 
159 private:
160  class Item;
161  class Proxy;
162 };
163 
164 //------------------------------------------------------------------------------
165 //
166 // Item
167 //
168 //------------------------------------------------------------------------------
169 
170 class PropertyStream::Item : public List<Item>::Node
171 {
172 public:
173  explicit Item(Source* source);
174  Source&
175  source() const;
176  Source*
177  operator->() const;
178  Source&
179  operator*() const;
180 
181 private:
183 };
184 
185 //------------------------------------------------------------------------------
186 //
187 // Proxy
188 //
189 //------------------------------------------------------------------------------
190 
192 {
193 private:
194  Map const* m_map;
197 
198 public:
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 
207  std::ostream&
208  operator<<(std::ostream& manip(std::ostream&)) const;
209 
210  template <typename T>
211  std::ostream&
212  operator<<(T const& t) const
213  {
214  return m_ostream << t;
215  }
216 };
217 
218 //------------------------------------------------------------------------------
219 //
220 // Map
221 //
222 //------------------------------------------------------------------------------
223 
225 {
226 private:
228 
229 public:
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 
282 template <typename Value>
285 {
286  m_map->add(m_key, value);
287  return *this;
288 }
289 
290 //--------------------------------------------------------------------------
291 //
292 // Set
293 //
294 //------------------------------------------------------------------------------
295 
297 {
298 private:
300 
301 public:
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 {
332 private:
338 
339 public:
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
sstream
std::string
STL class.
utility
beast::PropertyStream::map_end
virtual void map_end()=0
beast::PropertyStream::Map
Definition: PropertyStream.h:224
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:360
beast::PropertyStream::Source
Subclasses can be called to write to a stream and have children.
Definition: PropertyStream.h:330
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:182
beast::PropertyStream::Source::parent_
Source * parent_
Definition: PropertyStream.h:336
beast::PropertyStream::Proxy::m_map
Map const * m_map
Definition: PropertyStream.h:194
beast::PropertyStream::Set
Definition: PropertyStream.h:296
beast::PropertyStream::Map::operator[]
Proxy operator[](Key key) const
Definition: PropertyStream.h:272
beast::PropertyStream::Map::add
void add(Key key, Value value) const
Definition: PropertyStream.h:254
beast::PropertyStream::Set::m_stream
PropertyStream & m_stream
Definition: PropertyStream.h:299
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:333
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:334
beast::PropertyStream::Map::operator[]
Proxy operator[](char const *key)
Definition: PropertyStream.h:265
beast::PropertyStream::Map::add
void add(std::string const &key, Value value) const
Definition: PropertyStream.h:247
std::ostream
STL class.
beast::PropertyStream::Set::add
void add(Value value) const
Definition: PropertyStream.h:317
beast::PropertyStream::array_begin
virtual void array_begin()=0
beast::PropertyStream::~PropertyStream
virtual ~PropertyStream()=default
beast::PropertyStream::Proxy
Definition: PropertyStream.h:191
cstdint
beast::PropertyStream::array_end
virtual void array_end()=0
beast::PropertyStream::Source::item_
Item item_
Definition: PropertyStream.h:335
std::ostringstream
STL class.
beast::PropertyStream::Proxy::m_key
std::string m_key
Definition: PropertyStream.h:195
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:337
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:121
beast::PropertyStream::Proxy::m_ostream
std::ostringstream m_ostream
Definition: PropertyStream.h:196
beast::PropertyStream::Map::~Map
~Map()
Definition: beast_PropertyStream.cpp:112
beast::PropertyStream::add
void add(char const *value)
Definition: PropertyStream.h:114
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:227
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:170
beast
Definition: base_uint.h:641
string