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 map_begin () = 0;
48  virtual void map_begin (std::string const& key) = 0;
49  virtual void map_end () = 0;
50 
51  virtual void add (std::string const& key, std::string const& value) = 0;
52 
53  void add (std::string const& key, char const* value)
54  {
55  add (key, std::string (value));
56  }
57 
58  template <typename Value>
59  void lexical_add (std::string const &key, Value value)
60  {
62  ss << value;
63  add (key, ss.str());
64  }
65 
66  virtual void add (std::string const& key, bool value);
67  virtual void add (std::string const& key, char value);
68  virtual void add (std::string const& key, signed char value);
69  virtual void add (std::string const& key, unsigned char value);
70  virtual void add (std::string const& key, wchar_t value);
71 #if 0
72  virtual void add (std::string const& key, char16_t value);
73  virtual void add (std::string const& key, char32_t value);
74 #endif
75  virtual void add (std::string const& key, short value);
76  virtual void add (std::string const& key, unsigned short value);
77  virtual void add (std::string const& key, int value);
78  virtual void add (std::string const& key, unsigned int value);
79  virtual void add (std::string const& key, long value);
80  virtual void add (std::string const& key, unsigned long value);
81  virtual void add (std::string const& key, long long value);
82  virtual void add (std::string const& key, unsigned long long value);
83  virtual void add (std::string const& key, float value);
84  virtual void add (std::string const& key, double value);
85  virtual void add (std::string const& key, long double value);
86 
87  virtual void array_begin () = 0;
88  virtual void array_begin (std::string const& key) = 0;
89  virtual void array_end () = 0;
90 
91  virtual void add (std::string const& value) = 0;
92 
93  void add (char const* value)
94  {
95  add (std::string (value));
96  }
97 
98  template <typename Value>
99  void lexical_add (Value value)
100  {
102  ss << value;
103  add (ss.str());
104  }
105 
106  virtual void add (bool value);
107  virtual void add (char value);
108  virtual void add (signed char value);
109  virtual void add (unsigned char value);
110  virtual void add (wchar_t value);
111 #if 0
112  virtual void add (char16_t value);
113  virtual void add (char32_t value);
114 #endif
115  virtual void add (short value);
116  virtual void add (unsigned short value);
117  virtual void add (int value);
118  virtual void add (unsigned int value);
119  virtual void add (long value);
120  virtual void add (unsigned long value);
121  virtual void add (long long value);
122  virtual void add (unsigned long long value);
123  virtual void add (float value);
124  virtual void add (double value);
125  virtual void add (long double value);
126 
127 private:
128  class Item;
129  class Proxy;
130 };
131 
132 //------------------------------------------------------------------------------
133 //
134 // Item
135 //
136 //------------------------------------------------------------------------------
137 
138 class PropertyStream::Item : public List <Item>::Node
139 {
140 public:
141  explicit Item (Source* source);
142  Source& source() const;
143  Source* operator-> () const;
144  Source& operator* () const;
145 private:
147 };
148 
149 //------------------------------------------------------------------------------
150 //
151 // Proxy
152 //
153 //------------------------------------------------------------------------------
154 
156 {
157 private:
158  Map const* m_map;
161 
162 public:
163  Proxy (Map const& map, std::string const& key);
164  Proxy (Proxy const& other);
165  ~Proxy ();
166 
167  template <typename Value>
168  Proxy& operator= (Value value);
169 
171 
172  template <typename T>
173  std::ostream& operator<< (T const& t) const
174  {
175  return m_ostream << t;
176  }
177 };
178 
179 //------------------------------------------------------------------------------
180 //
181 // Map
182 //
183 //------------------------------------------------------------------------------
184 
186 {
187 private:
189 
190 public:
191  explicit Map (PropertyStream& stream);
192  explicit Map (Set& parent);
193  Map (std::string const& key, Map& parent);
194  Map (std::string const& key, PropertyStream& stream);
195  ~Map ();
196 
197  Map(Map const&) = delete;
198  Map& operator= (Map const&) = delete;
199 
201  PropertyStream const& stream() const;
202 
203  template <typename Value>
204  void add (std::string const& key, Value value) const
205  {
206  m_stream.add (key, value);
207  }
208 
209  template <typename Key, typename Value>
210  void add (Key key, Value value) const
211  {
213  ss << key;
214  add (ss.str(), value);
215  }
216 
217  Proxy operator[] (std::string const& key);
218 
219  Proxy operator[] (char const* key)
220  { return Proxy (*this, key); }
221 
222  template <typename Key>
223  Proxy operator[] (Key key) const
224  {
226  ss << key;
227  return Proxy (*this, ss.str());
228  }
229 };
230 
231 //--------------------------------------------------------------------------
232 
233 template <typename Value>
235 {
236  m_map->add (m_key, value);
237  return *this;
238 }
239 
240 //--------------------------------------------------------------------------
241 //
242 // Set
243 //
244 //------------------------------------------------------------------------------
245 
247 {
248 private:
250 
251 public:
252  Set (std::string const& key, Map& map);
253  Set (std::string const& key, PropertyStream& stream);
254  ~Set ();
255 
256  Set (Set const&) = delete;
257  Set& operator= (Set const&) = delete;
258 
259  PropertyStream& stream();
260  PropertyStream const& stream() const;
261 
262  template <typename Value>
263  void add (Value value) const
264  { m_stream.add (value); }
265 };
266 
267 //------------------------------------------------------------------------------
268 //
269 // Source
270 //
271 //------------------------------------------------------------------------------
272 
275 {
276 private:
282 
283 public:
284  explicit Source (std::string const& name);
285  virtual ~Source ();
286 
287  Source (Source const&) = delete;
288  Source& operator= (Source const&) = delete;
289 
291  std::string const& name() const;
292 
294  void add (Source& source);
295 
299  template <class Derived>
300  Derived* add (Derived* child)
301  {
302  add (*static_cast <Source*>(child));
303  return child;
304  }
305 
307  void remove (Source& child);
308 
310  void removeAll ();
311 
313  void write_one (PropertyStream& stream);
314 
316  void write (PropertyStream& stream);
317 
323  void write (PropertyStream& stream, std::string const& path);
324 
340 
341  Source* find_one_deep (std::string const& name);
342  PropertyStream::Source* find_path(std::string path);
343  PropertyStream::Source* find_one(std::string const& name);
344 
345  static bool peel_leading_slash (std::string* path);
346  static bool peel_trailing_slashstar (std::string* path);
347  static std::string peel_name(std::string* path);
348 
349 
350  //--------------------------------------------------------------------------
351 
355  virtual void onWrite (Map&);
356 };
357 
358 }
359 
360 #endif
sstream
std::string
STL class.
utility
beast::PropertyStream::map_end
virtual void map_end()=0
beast::PropertyStream::Map
Definition: PropertyStream.h:185
beast::PropertyStream::Map::stream
PropertyStream & stream()
Definition: beast_PropertyStream.cpp:120
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:300
beast::PropertyStream::Source
Subclasses can be called to write to a stream and have children.
Definition: PropertyStream.h:274
beast::PropertyStream::map_begin
virtual void map_begin()=0
beast::PropertyStream::Item::operator*
Source & operator*() const
Definition: beast_PropertyStream.cpp:49
std::stringstream
STL class.
beast::PropertyStream::add
void add(std::string const &key, char const *value)
Definition: PropertyStream.h:53
std::recursive_mutex
STL class.
beast::PropertyStream::Proxy::~Proxy
~Proxy()
Definition: beast_PropertyStream.cpp:73
beast::PropertyStream::Item::m_source
Source * m_source
Definition: PropertyStream.h:146
beast::PropertyStream::Source::parent_
Source * parent_
Definition: PropertyStream.h:280
beast::PropertyStream::Proxy::m_map
Map const * m_map
Definition: PropertyStream.h:158
beast::PropertyStream::Set
Definition: PropertyStream.h:246
beast::PropertyStream::Map::add
void add(Key key, Value value) const
Definition: PropertyStream.h:210
beast::PropertyStream::Set::m_stream
PropertyStream & m_stream
Definition: PropertyStream.h:249
beast::PropertyStream::Item::operator->
Source * operator->() const
Definition: beast_PropertyStream.cpp:44
beast::PropertyStream::PropertyStream
PropertyStream()=default
beast::PropertyStream::Source::m_name
const std::string m_name
Definition: PropertyStream.h:277
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:278
beast::PropertyStream::Map::add
void add(std::string const &key, Value value) const
Definition: PropertyStream.h:204
std::ostream
STL class.
beast::PropertyStream::Set::add
void add(Value value) const
Definition: PropertyStream.h:263
beast::PropertyStream::array_begin
virtual void array_begin()=0
beast::PropertyStream::~PropertyStream
virtual ~PropertyStream()=default
beast::PropertyStream::Proxy
Definition: PropertyStream.h:155
cstdint
beast::PropertyStream::array_end
virtual void array_end()=0
beast::PropertyStream::Source::item_
Item item_
Definition: PropertyStream.h:279
std::ostringstream
STL class.
beast::PropertyStream::Proxy::m_key
std::string m_key
Definition: PropertyStream.h:159
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:92
beast::PropertyStream::Source::children_
List< Item > children_
Definition: PropertyStream.h:281
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:99
beast::PropertyStream::Proxy::m_ostream
std::ostringstream m_ostream
Definition: PropertyStream.h:160
beast::PropertyStream::Map::~Map
~Map()
Definition: beast_PropertyStream.cpp:115
beast::PropertyStream::add
void add(char const *value)
Definition: PropertyStream.h:93
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:188
beast::PropertyStream::Proxy::Proxy
Proxy(Map const &map, std::string const &key)
Definition: beast_PropertyStream.cpp:60
beast::PropertyStream::lexical_add
void lexical_add(std::string const &key, Value value)
Definition: PropertyStream.h:59
beast::List
Intrusive doubly linked list.
Definition: List.h:29
beast::PropertyStream::Item
Definition: PropertyStream.h:138
beast
Definition: base_uint.h:582
string