VSM C++ SDK
Vehicle Specific Modules SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
properties.h
Go to the documentation of this file.
1 // Copyright (c) 2018, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
10 #ifndef _UGCS_VSM_PROPERTIES_H_
11 #define _UGCS_VSM_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  int32_t
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, int32_t 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  int32_t int_repr;
159  double float_repr;
161  bool int_valid:1,
163  float_valid:1;
165  std::string description;
167  int seq_number = -1;
168 
170  Property(std::string &&value);
171 
173  Property(int32_t 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:
195  Iterator(const Table_type::const_iterator &iterator,
196  const Table_type::const_iterator &end_iterator,
197  const std::string &prefix = std::string(),
198  char separator = '.'):
199  table_iterator(iterator),
200  table_end(end_iterator),
201  prefix(prefix),
202  separator(separator)
203  {
204  _NextProp();
205  }
206 
208  std::string
209  operator *() const
210  {
211  if (table_iterator == table_end) {
212  VSM_EXCEPTION(ugcs::vsm::Internal_error_exception,
213  "Dereferencing end iterator");
214  }
215  return table_iterator->first;
216  }
217 
219  const std::string *
220  operator->() const
221  {
222  if (table_iterator == table_end) {
223  VSM_EXCEPTION(ugcs::vsm::Internal_error_exception,
224  "Dereferencing end iterator");
225  }
226  return &table_iterator->first;
227  }
228 
230  bool
231  operator ==(const Iterator &it) const
232  {
233  return table_iterator == it.table_iterator;
234  }
235 
237  bool
238  operator !=(const Iterator &it) const
239  {
240  return !(*this == it);
241  }
242 
244  void
245  operator++();
246 
248  void
250  {
251  ++(*this);
252  }
253 
262  std::string
263  operator[](size_t comp_idx);
264 
265  int
266  Get_count();
267 
268  private:
269  Table_type::const_iterator table_iterator, table_end;
270  std::string prefix;
271  char separator;
272 
274  void
275  _NextProp();
276  };
277 
284  Iterator
285  begin(const std::string &prefix = std::string(), char separator = '.') const
286  {
287  return Iterator(table.begin(), table.end(), prefix, separator);
288  }
289 
291  Iterator
292  end() const
293  {
294  return Iterator(table.end(), table.end());
295  }
296 
297 private:
299  static Singleton<Properties> singleton;
300 
301  int last_sequence_number = 0;
302 
305  std::string trailer;
306 
308  const Property &
309  Find_property(const std::string &key) const;
310 
312  Property *
313  Find_property(const std::string &key);
314 
316  static std::string
317  Escape(const std::string &str, bool is_key = false);
318 };
319 
320 } /* namespace vsm */
321 } /* namespace ugcs */
322 
323 #endif /* _UGCS_VSM_PROPERTIES_H_ */
std::shared_ptr< Properties > Ptr
Pointer type.
Definition: properties.h:30
void operator++(int)
Postfix increment operator.
Definition: properties.h:249
Singleton class definition.
void operator++()
Prefix increment operator.
bool operator==(const Iterator &it) const
Equality check method.
Definition: properties.h:231
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:292
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 operator[](size_t comp_idx)
Get component of name with the specified index.
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:208
const std::string * operator->() const
Get the value.
Definition: properties.h:220
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.
Definition: property.h:16
void Delete(const std::string &key)
Delete the property with the specified key.
bool operator!=(const Iterator &it) const
Non-equality check method.
Definition: properties.h:238
int32_t Get_int(const std::string &key) const
Get integer value of the property.
Iterator begin(const std::string &prefix=std::string(), char separator= '.') const
Begin properties iteration.
Definition: properties.h:285
VSM_DEFINE_EXCEPTION(Exception)
Base class for all Properties exceptions.
std::string operator*() const
Get the value.
Definition: properties.h:209
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:168
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:195
T value
Stored value (in wire byte order).
Definition: endian.h:376
Helper class for defining derived exceptions.
Definition: exception.h:133
Base class for all VSM exceptions.
Definition: exception.h:22