Ocean
Config.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_BASE_CONFIG_H
9 #define META_OCEAN_BASE_CONFIG_H
10 
11 #include "ocean/base/Base.h"
12 #include "ocean/base/Timestamp.h"
13 
14 #include <vector>
15 
16 #ifdef __APPLE__
17  #ifdef TYPE_BOOL
18  #undef TYPE_BOOL
19  #endif
20 #endif
21 
22 namespace Ocean
23 {
24 
25 /**
26  * This class implements an application or module configuration toolkit.
27  * Each configuration toolkit holds a value tree.<br>
28  * Different single value types are supported like e.g. boolean, integer or strings.<br>
29  * Furthermore, groups can be specified holding an arbitrary number of sub values.<br>
30  * Values names does not need to be unique. Therefore, a group can hold several sub values with the same name.<br>
31  * Also, values with the same name can have different value types.<br>
32  * However, this is an abstract configuration object and therefore should be used as platform independent interface only.<br>
33  * Use e.g. file configuration or a registry configuration object instead.
34  * The entire object is not thread-safe.
35  * @ingroup base
36  */
37 class OCEAN_BASE_EXPORT Config
38 {
39  public:
40 
41  /**
42  * Definition of different value types.
43  */
44  enum ValueType
45  {
46  /// Invalid value type.
48  /// Undefined value type.
50  /// Boolean value type.
52  /// Integer value type.
54  /// Number value type.
56  /// String value type.
58  /// Multi boolean value type.
60  /// Multi integer value type.
62  /// Multi number value type.
64  /// Multi string value type.
66  /// Group value type.
67  TYPE_GROUP
68  };
69 
70  public:
71 
72  /**
73  * This class implements a configuration value.
74  * Each value can represent a single parameter or a grouping object.<br>
75  */
76  class OCEAN_BASE_EXPORT Value
77  {
78  friend class Config;
79 
80  public:
81 
82  /**
83  * Returns the type of this value.
84  * @return Value type
85  */
86  virtual ValueType type() const;
87 
88  /**
89  * Returns whether this value holds a multi type.
90  * @return True, if so
91  */
92  virtual bool isMultiType() const;
93 
94  /**
95  * Returns the number of sub values.
96  * @return Sub value number
97  */
98  virtual unsigned int values() const;
99 
100  /**
101  * Returns the number of sub values with a given name.
102  * @param name The name of the sub values to return
103  * @return Sub value number
104  */
105  virtual unsigned int values(const std::string& name) const;
106 
107  /**
108  * Returns whether this value holds at least one specified sub value.
109  * @param name The name of the sub value to check
110  * @return True, if so
111  */
112  virtual bool exist(const std::string& name) const;
113 
114  /**
115  * Returns a sub value specified by it's name and it's index if more than one value exists with the same name.
116  * @param name The name of the value to return
117  * @param index The index within those sub values with the specified name
118  * @return Sub value
119  */
120  virtual Value& value(const std::string& name, const unsigned int index = 0u);
121 
122  /**
123  * Returns a sub value specified by it's name and it's index if more than one value exists with the same name.
124  * @param name The name of the value to return
125  * @param index The index within those sub values with the specified name
126  * @param value Resulting sub value
127  * @return True, if the value exists
128  */
129  virtual bool value(const std::string& name, const unsigned int index, Value** value);
130 
131  /**
132  * Returns a sub value specified by it's index.
133  * @param index The index of the sub value to return
134  * @param name Resulting name of the returning value
135  * @return Sub value
136  */
137  virtual Value& value(const unsigned int index, std::string& name);
138 
139  /**
140  * Returns a sub value specified by it's index.
141  * @param index The index of the sub value to return
142  * @param name Resulting name of the returning value
143  * @param value Resulting sub value
144  * @return True, if the value exists
145  */
146  virtual bool value(const unsigned int index, std::string& name, Value** value);
147 
148  /**
149  * Adds a new sub value specified by it's name.
150  * @param name The name of the sub value to create
151  * @return New sub value
152  */
153  virtual Value& add(const std::string& name);
154 
155  /**
156  * Returns this value as boolean.
157  * @param value Default value which will be returned if this value holds no boolean
158  * @return Internal value
159  */
160  virtual bool operator()(const bool value) const;
161 
162  /**
163  * Returns this value as integer.
164  * @param value Default value which will be returned if this value holds no integer
165  * @return Internal value
166  */
167  virtual int operator()(const int value) const;
168 
169  /**
170  * Returns this value as number.
171  * @param value Default value which will be returned if this value holds no number
172  * @return Internal value
173  */
174  virtual double operator()(const double value) const;
175 
176  /**
177  * Returns this value as string.
178  * @param value Default value which will be returned if this value holds no string
179  * @return Internal value
180  */
181  inline std::string operator()(const char* value) const;
182 
183  /**
184  * Returns this value as string.
185  * @param value Default value which will be returned if this value holds no string
186  * @return Internal value
187  */
188  virtual std::string operator()(const std::string& value) const;
189 
190  /**
191  * Returns this value as multi boolean.
192  * @param value Default value which will be returned if this value holds no boolean
193  * @return Internal value
194  */
195  virtual std::vector<bool> operator()(const std::vector<bool>& value) const;
196 
197  /**
198  * Returns this value as multi integer.
199  * @param value Default value which will be returned if this value holds no integer
200  * @return Internal value
201  */
202  virtual std::vector<int> operator()(const std::vector<int>& value) const;
203 
204  /**
205  * Returns this value as multi number.
206  * @param value Default value which will be returned if this value holds no number
207  * @return Internal value
208  */
209  virtual std::vector<double> operator()(const std::vector<double>& value) const;
210 
211  /**
212  * Returns this value as multi string.
213  * @param value Default value which will be returned if this value holds no string
214  * @return Internal value
215  */
216  virtual std::vector<std::string> operator()(const std::vector<std::string>& value) const;
217 
218  /**
219  * Sets this value as boolean.
220  * @param value The value to set
221  * @return True, if succeeded
222  */
223  virtual bool operator=(const bool value);
224 
225  /**
226  * Sets this value as integer.
227  * @param value The value to set
228  * @return True, if succeeded
229  */
230  virtual bool operator=(const int value);
231 
232  /**
233  * Sets this value as number.
234  * @param value The value to set
235  * @return True, if succeeded
236  */
237  virtual bool operator=(const double value);
238 
239  /**
240  * Sets this value as string.
241  * @param value The value to set
242  * @return True, if succeeded
243  */
244  inline bool operator=(const char* value);
245 
246  /**
247  * Sets this value as string.
248  * @param value The value to set
249  * @return True, if succeeded
250  */
251  virtual bool operator=(const std::string& value);
252 
253  /**
254  * Sets this value as multi boolean.
255  * @param values The values to set
256  * @return True, if succeeded
257  */
258  virtual bool operator=(const std::vector<bool>& values);
259 
260  /**
261  * Sets this value as multi integer.
262  * @param values The values to set
263  * @return True, if succeeded
264  */
265  virtual bool operator=(const std::vector<int>& values);
266 
267  /**
268  * Sets this value as multi number.
269  * @param values The values to set
270  * @return True, if succeeded
271  */
272  virtual bool operator=(const std::vector<double>& values);
273 
274  /**
275  * Sets this value as multi string.
276  * @param values The values to set
277  * @return True, if succeeded
278  */
279  virtual bool operator=(const std::vector<std::string>& values);
280 
281  /**
282  * Returns the first sub value specified by it's name or creates a new value if no existing.
283  * @param name The name of the sub-value to return
284  * @return Specified sub value
285  */
286  virtual Value& operator[](const std::string& name);
287 
288  protected:
289 
290  /**
291  * Creates a new value object.
292  */
293  Value() = default;
294 
295  /**
296  * Default copy constructor.
297  * @param value The value to copied
298  */
299  Value(const Value& value) = default;
300 
301  /**
302  * Destructs a value object.
303  */
304  virtual ~Value() = default;
305 
306  protected:
307 
308  /// Value type.
309  ValueType valueType_ = TYPE_INVALID;
310  };
311 
312  public:
313 
314  /**
315  * Destructs a configuration toolkit.
316  */
317  virtual ~Config();
318 
319  /**
320  * Reads / loads all values of this configuration.
321  * @return True, if succeeded
322  */
323  virtual bool read();
324 
325  /**
326  * Writes / saves all values of this configuration.
327  * @return True, if succeeded
328  */
329  virtual bool write();
330 
331  /**
332  * Returns the timestamp of the last write execution.
333  * @return Most recent write timestamp
334  */
335  inline Timestamp writeTimestamp() const;
336 
337  /**
338  * Returns the number of sub values.
339  * @return Sub value number
340  */
341  virtual unsigned int values() const;
342 
343  /**
344  * Returns the number of sub values with a given name.
345  * @param name The name of the sub values to return
346  * @return Sub value number
347  */
348  virtual unsigned int values(const std::string& name) const;
349 
350  /**
351  * Returns whether this value holds at least one specified sub value.
352  * @param name The name of the sub value to check
353  * @return True, if so
354  */
355  virtual bool exist(const std::string& name) const;
356 
357  /**
358  * Returns a sub value specified by it's index.
359  * @param index The index of the sub value to return
360  * @param name Resulting name of the returning value
361  * @return Sub value
362  */
363  virtual Value& value(const unsigned int index, std::string& name);
364 
365  /**
366  * Returns a sub value specified by it's index.
367  * @param index The index of the sub value to return
368  * @param name Resulting name of the returning value
369  * @param value Resulting sub value
370  * @return True, if the value exists
371  */
372  virtual bool value(const unsigned int index, std::string& name, Value** value);
373 
374  /**
375  * Returns a sub value specified by it's name and it's index if more than one value exists with the same name.
376  * @param name The name of the value to return
377  * @param index The index within those sub values with the specified name
378  * @param value Resulting sub value
379  * @return True, if the value exists
380  */
381  virtual bool value(const std::string& name, const unsigned int index, Value** value);
382 
383  /**
384  * Returns a sub value specified by it's name and it's index if more than one value exists with the same name.
385  * @param name The name of the value to return
386  * @param index The index within those sub values with the specified name
387  * @return Sub value
388  */
389  virtual Value& value(const std::string& name, const unsigned int index = 0);
390 
391  /**
392  * Adds a new sub value specified by it's name.
393  * @param name The name of the sub value to create
394  * @return New sub value
395  */
396  virtual Value& add(const std::string& name);
397 
398  /**
399  * Returns the first sub value specified by it's name or creates a new one if not existing.
400  * @param name The name of the sub value to return
401  * @return Specified sub value
402  */
403  virtual Value& operator[](const std::string& name);
404 
405  protected:
406 
407  /**
408  * Creates a new configuration toolkit
409  */
411 
412  /**
413  * Disabled copy constructor.
414  * @param config Object which would be copied
415  */
416  Config(const Config& config) = delete;
417 
418  /**
419  * Disabled copy operator.
420  * @param config Object which would be copied
421  * @return Reference to this object
422  */
423  Config& operator=(const Config& config) = delete;
424 
425  /**
426  * Returns the default value holding no data.
427  * @note This function is not virtual by intention. Derived classes are supposed to override it and have it return their derived value type.
428  * @return The default value holding no data.
429  */
430  static Value& nullValue();
431 
432  protected:
433 
434  /// Holds the timestamp of the most recent write execution.
436 };
437 
438 inline std::string Config::Value::operator()(const char* value) const
439 {
440  return (*this)(std::string(value));
441 }
442 
443 inline bool Config::Value::operator=(const char* value)
444 {
445  return (*this) = std::string(value);
446 }
447 
449 {
450  return writeTimestamp_;
451 }
452 
453 }
454 
455 #endif // META_OCEAN_BASE_CONFIG_H
This class implements a configuration value.
Definition: Config.h:77
virtual bool value(const unsigned int index, std::string &name, Value **value)
Returns a sub value specified by it's index.
virtual std::vector< bool > operator()(const std::vector< bool > &value) const
Returns this value as multi boolean.
virtual bool operator=(const bool value)
Sets this value as boolean.
virtual Value & value(const unsigned int index, std::string &name)
Returns a sub value specified by it's index.
virtual unsigned int values(const std::string &name) const
Returns the number of sub values with a given name.
virtual int operator()(const int value) const
Returns this value as integer.
virtual std::vector< double > operator()(const std::vector< double > &value) const
Returns this value as multi number.
virtual Value & value(const std::string &name, const unsigned int index=0u)
Returns a sub value specified by it's name and it's index if more than one value exists with the same...
Value()=default
Creates a new value object.
virtual bool operator=(const std::vector< double > &values)
Sets this value as multi number.
virtual bool exist(const std::string &name) const
Returns whether this value holds at least one specified sub value.
virtual bool operator=(const double value)
Sets this value as number.
virtual bool operator=(const std::string &value)
Sets this value as string.
virtual bool operator=(const int value)
Sets this value as integer.
virtual std::vector< int > operator()(const std::vector< int > &value) const
Returns this value as multi integer.
virtual bool operator=(const std::vector< bool > &values)
Sets this value as multi boolean.
virtual std::string operator()(const std::string &value) const
Returns this value as string.
virtual Value & add(const std::string &name)
Adds a new sub value specified by it's name.
virtual ValueType type() const
Returns the type of this value.
virtual bool operator=(const std::vector< std::string > &values)
Sets this value as multi string.
virtual unsigned int values() const
Returns the number of sub values.
virtual double operator()(const double value) const
Returns this value as number.
virtual bool value(const std::string &name, const unsigned int index, Value **value)
Returns a sub value specified by it's name and it's index if more than one value exists with the same...
virtual Value & operator[](const std::string &name)
Returns the first sub value specified by it's name or creates a new value if no existing.
virtual bool operator=(const std::vector< int > &values)
Sets this value as multi integer.
Value(const Value &value)=default
Default copy constructor.
virtual bool isMultiType() const
Returns whether this value holds a multi type.
virtual bool operator()(const bool value) const
Returns this value as boolean.
virtual ~Value()=default
Destructs a value object.
virtual std::vector< std::string > operator()(const std::vector< std::string > &value) const
Returns this value as multi string.
This class implements an application or module configuration toolkit.
Definition: Config.h:38
virtual Value & value(const unsigned int index, std::string &name)
Returns a sub value specified by it's index.
virtual Value & add(const std::string &name)
Adds a new sub value specified by it's name.
static Value & nullValue()
Returns the default value holding no data.
virtual bool value(const unsigned int index, std::string &name, Value **value)
Returns a sub value specified by it's index.
Config & operator=(const Config &config)=delete
Disabled copy operator.
virtual bool read()
Reads / loads all values of this configuration.
Config(const Config &config)=delete
Disabled copy constructor.
virtual unsigned int values() const
Returns the number of sub values.
Config()
Creates a new configuration toolkit.
ValueType
Definition of different value types.
Definition: Config.h:45
@ TYPE_MULTI_NUMBER
Multi number value type.
Definition: Config.h:63
@ TYPE_NUMBER
Number value type.
Definition: Config.h:55
@ TYPE_MULTI_BOOL
Multi boolean value type.
Definition: Config.h:59
@ TYPE_MULTI_INT
Multi integer value type.
Definition: Config.h:61
@ TYPE_BOOL
Boolean value type.
Definition: Config.h:51
@ TYPE_MULTI_STRING
Multi string value type.
Definition: Config.h:65
@ TYPE_INT
Integer value type.
Definition: Config.h:53
@ TYPE_STRING
String value type.
Definition: Config.h:57
@ TYPE_UNDEFINED
Undefined value type.
Definition: Config.h:49
@ TYPE_INVALID
Invalid value type.
Definition: Config.h:47
virtual ~Config()
Destructs a configuration toolkit.
virtual Value & operator[](const std::string &name)
Returns the first sub value specified by it's name or creates a new one if not existing.
virtual unsigned int values(const std::string &name) const
Returns the number of sub values with a given name.
Timestamp writeTimestamp_
Holds the timestamp of the most recent write execution.
Definition: Config.h:435
virtual bool value(const std::string &name, const unsigned int index, Value **value)
Returns a sub value specified by it's name and it's index if more than one value exists with the same...
virtual Value & value(const std::string &name, const unsigned int index=0)
Returns a sub value specified by it's name and it's index if more than one value exists with the same...
virtual bool write()
Writes / saves all values of this configuration.
virtual bool exist(const std::string &name) const
Returns whether this value holds at least one specified sub value.
Timestamp writeTimestamp() const
Returns the timestamp of the last write execution.
Definition: Config.h:448
This class implements a timestamp.
Definition: Timestamp.h:36
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15