VSM C++ SDK
Vehicle Specific Modules SDK
properties.h
Go to the documentation of this file.
1 // Copyright (c) 2014, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
10 #ifndef _PROPERTIES_H_
11 #define _PROPERTIES_H_
12 
13 #include <ugcs/vsm/exception.h>
14 #include <ugcs/vsm/singleton.h>
15 
16 #include <istream>
17 #include <map>
18 
19 namespace ugcs {
20 namespace vsm {
21 
27 class Properties {
28 public:
30  typedef std::shared_ptr<Properties> Ptr;
31 
42 
44  template <typename... Args>
45  static Ptr
46  Get_instance(Args &&... args)
47  {
48  return singleton.Get_instance(std::forward<Args>(args)...);
49  }
50 
52  Properties();
53 
59  void
60  Load(std::istream &stream);
61 
63  bool
64  Exists(const std::string &key) const;
65 
72  std::string
73  Get(const std::string &key) const;
74 
85  long
86  Get_int(const std::string &key) const;
87 
96  double
97  Get_float(const std::string &key) const;
98 
108  void
109  Set_description(const std::string &key, const std::string &description);
110 
117  void
118  Set(const std::string &key, const std::string &value);
119 
126  void
127  Set(const std::string &key, int value);
128 
135  void
136  Set(const std::string &key, double value);
137 
143  void
144  Delete(const std::string &key);
145 
147  void
148  Store(std::ostream &stream);
149 
150 private:
152  class Property {
153  public:
155  std::string str_repr;
157  long int_repr;
159  double float_repr;
161  bool int_valid:1,
163  float_valid:1;
165  std::string description;
167  int seq_number;
168 
170  Property(std::string &&value);
171 
173  Property(long value);
174 
176  Property(double value);
177  };
178 
179  typedef std::map<std::string, Property> Table_type;
180 
182  Table_type table;
183 
184 public:
186  class Iterator {
187  public:
188 
196  Iterator(const Table_type::const_iterator &iterator,
197  const Table_type::const_iterator &end_iterator,
198  const std::string &prefix = std::string(),
199  char separator = '.'):
200  table_iterator(iterator),
201  table_end(end_iterator),
202  prefix(prefix),
203  separator(separator)
204  {
205  _NextProp();
206  }
207 
209  std::string
210  operator *() const
211  {
212  if (table_iterator == table_end) {
214  "Dereferencing end iterator");
215  }
216  return table_iterator->first;
217  }
218 
220  const std::string *
221  operator->() const
222  {
223  if (table_iterator == table_end) {
225  "Dereferencing end iterator");
226  }
227  return &table_iterator->first;
228  }
229 
231  bool
232  operator ==(const Iterator &it) const
233  {
234  return table_iterator == it.table_iterator;
235  }
236 
238  bool
239  operator !=(const Iterator &it) const
240  {
241  return !(*this == it);
242  }
243 
245  void
246  operator ++();
247 
249  void
250  operator ++(int)
251  {
252  ++(*this);
253  }
254 
263  std::string
264  operator[](size_t comp_idx);
265 
266  int
267  Get_count();
268 
269  private:
270  Table_type::const_iterator table_iterator, table_end;
271  std::string prefix;
272  char separator;
273 
275  void
276  _NextProp();
277  };
278 
285  Iterator
286  begin(const std::string &prefix = std::string(), char separator = '.') const
287  {
288  return Iterator(table.begin(), table.end(), prefix, separator);
289  }
290 
292  Iterator
293  end() const
294  {
295  return Iterator(table.end(), table.end());
296  }
297 
298 private:
300  static Singleton<Properties> singleton;
301 
302  int last_sequence_number = 0;
303 
306  std::string trailer;
307 
309  const Property &
310  Find_property(const std::string &key) const;
311 
313  Property *
314  Find_property(const std::string &key);
315 
317  static std::string
318  Escape(const std::string &str, bool is_key = false);
319 };
320 
321 } /* namespace vsm */
322 } /* namespace ugcs */
323 
324 #endif /* _PROPERTIES_H_ */
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
std::shared_ptr< Properties > Ptr
Pointer type.
Definition: properties.h:30
Singleton class definition.
Exception class with one parameter.
Definition: exception.h:88
void Store(std::ostream &stream)
Store properties into the specified stream.
static Ptr Get_instance(Args &&...args)
Get global or create new properties instance.
Definition: properties.h:46
Iterator end() const
Iterator for one-past-the-end position.
Definition: properties.h:293
void Set_description(const std::string &key, const std::string &description)
Set the description of the property.
This class represents persistent set of properties which can be stored and loaded in/from any stream...
Definition: properties.h:27
VSM exceptions definition.
std::string Get(const std::string &key) const
Get string value of the property.
Stored properties iterator.
Definition: properties.h:186
#define VSM_DEFINE_DERIVED_EXCEPTION(__base_class, __exc_class)
Define custom derived exception.
Definition: exception.h:210
const std::string * operator->() const
Get the value.
Definition: properties.h:221
void Load(std::istream &stream)
Load properties from text stream.
Properties()
Construct empty properties.
bool Exists(const std::string &key) const
Check if the property with the specified key exists.
#define VSM_DEFINE_EXCEPTION(__exc_class,...)
Define custom exception type.
Definition: exception.h:202
void Delete(const std::string &key)
Delete the property with the specified key.
Iterator begin(const std::string &prefix=std::string(), char separator= '.') const
Begin properties iteration.
Definition: properties.h:286
double Get_float(const std::string &key) const
Get floating point number value of the property.
void Set(const std::string &key, const std::string &value)
Set string value of the property.
Helper class for implementing singletons.
Definition: singleton.h:69
#define VSM_EXCEPTION(__exc_class, __msg,...)
Throw VSM exception instance.
Definition: exception.h:170
Iterator(const Table_type::const_iterator &iterator, const Table_type::const_iterator &end_iterator, const std::string &prefix=std::string(), char separator= '.')
Construct new iterator which iterates values starting from the given prefix.
Definition: properties.h:196
long Get_int(const std::string &key) const
Get integer value of the property.
Helper class for defining derived exceptions.
Definition: exception.h:135