Ocean
FileConfig.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_IO_FILE_CONFIG_H
9 #define META_OCEAN_IO_FILE_CONFIG_H
10 
11 #include "ocean/io/IO.h"
12 #include "ocean/io/Scanner.h"
13 
14 #include "ocean/base/Config.h"
15 
16 #include <map>
17 
18 namespace Ocean
19 {
20 
21 namespace IO
22 {
23 
24 /**
25  * This class implements an application or module configuration toolkit using files as input or output.
26  * @ingroup io
27  */
28 class OCEAN_IO_EXPORT FileConfig : public Config
29 {
30  protected:
31 
32  // Forward declaration
33  class FileValue;
34 
35  /**
36  * Definition of a map mapping value names to value objects.
37  */
38  typedef std::multimap<std::string, FileValue> FileValues;
39 
40  /**
41  * Definition of a file value object.
42  */
43  class OCEAN_IO_EXPORT FileValue final : public Value
44  {
45  friend class FileConfig;
46 
47  public:
48 
49  /**
50  * Creates a new value object.
51  */
52  FileValue() = default;
53 
54  /**
55  * Default copy constructor.
56  * @param fileValue The value to copy
57  */
58  FileValue(const FileValue& fileValue) = default;
59 
60  /**
61  * Creates a new value object by a single value as string and the real value type.
62  * @param value String value
63  * @param type Value type
64  */
65  FileValue(const std::string& value, const ValueType type);
66 
67  /**
68  * Creates a new value object holding sub values.
69  * @param values Sub values
70  */
71  explicit FileValue(const FileValues& values);
72 
73  /**
74  * Destructs a value object.
75  */
76  ~FileValue() override;
77 
78  /**
79  * Returns the number of sub values.
80  * @see Config::Value::values().
81  */
82  unsigned int values() const override;
83 
84  /**
85  * Returns the number of sub values with a given name.
86  * @see Config::Value::values().
87  */
88  unsigned int values(const std::string& name) const override;
89 
90  /**
91  * Returns whether this value holds at least one specified sub value.
92  * @see Config::Value::exist().
93  */
94  bool exist(const std::string& name) const override;
95 
96  /**
97  * Returns a sub value specified by it's name and it's index if more than one value exists with the same name.
98  * @see Config::Value::value().
99  */
100  FileValue& value(const std::string& name, const unsigned int index) override;
101 
102  /**
103  * Returns a sub value specified by it's name and it's index if more than one value exists with the same name.
104  * @see Config::Value::value().
105  */
106  bool value(const std::string& name, const unsigned int index, Value** value) override;
107 
108  /**
109  * Returns a sub value specified by it's index.
110  * @see Config::Value::value().
111  */
112  FileValue& value(const unsigned int index, std::string& name) override;
113 
114  /**
115  * Returns a sub value specified by it's index.
116  * @see Config::Value::value().
117  */
118  bool value(const unsigned int index, std::string& name, Value** value) override;
119 
120  /**
121  * Adds a new sub value specified by it's name.
122  * @param name The name of the sub value to create
123  * @return New sub value
124  */
125  FileValue& add(const std::string& name) override;
126 
127  /**
128  * Returns this value as boolean.
129  * @see Config::Value::operator().
130  */
131  bool operator()(const bool value) const override;
132 
133  /**
134  * Returns this value as integer.
135  * @see Config::Value::operator().
136  */
137  int operator()(const int value) const override;
138 
139  /**
140  * Returns this value as number.
141  * @see Config::Value::operator().
142  */
143  double operator()(const double value) const override;
144 
145  /**
146  * Returns this value as string.
147  * @see Config::Value::operator().
148  */
149  std::string operator()(const std::string& value) const override;
150 
151  /**
152  * Returns this value as multi boolean.
153  * @see Config::Value::operator().
154  */
155  std::vector<bool> operator()(const std::vector<bool>& value) const override;
156 
157  /**
158  * Returns this value as multi integer.
159  * @see Config::Value::operator().
160  */
161  std::vector<int> operator()(const std::vector<int>& value) const override;
162 
163  /**
164  * Returns this value as multi number.
165  * @see Config::Value::operator().
166  */
167  std::vector<double> operator()(const std::vector<double>& value) const override;
168 
169  /**
170  * Returns this value as multi string.
171  * @see Config::Value::operator().
172  */
173  std::vector<std::string> operator()(const std::vector<std::string>& value) const override;
174 
175  /**
176  * Sets this value as boolean.
177  * @see Config::Value::operator=().
178  */
179  bool operator=(const bool value) override;
180 
181  /**
182  * Sets this value as integer.
183  * @see Config::Value::operator=().
184  */
185  bool operator=(const int value) override;
186 
187  /**
188  * Sets this value as number.
189  * @see Config::Value::operator=().
190  */
191  bool operator=(const double value) override;
192 
193  /**
194  * Sets this value as string.
195  * @see Config::Value::operator=().
196  */
197  bool operator=(const std::string& value) override;
198 
199  /**
200  * Sets this value as multi boolean.
201  * @see Config::Value::operator=().
202  */
203  bool operator=(const std::vector<bool>& values) override;
204 
205  /**
206  * Sets this value as multi integer.
207  * @see Config::Value::operator=().
208  */
209  bool operator=(const std::vector<int>& values) override;
210 
211  /**
212  * Sets this value as multi number.
213  * @see Config::Value::operator=().
214  */
215  bool operator=(const std::vector<double>& values) override;
216 
217  /**
218  * Sets this value as multi string.
219  * @see Config::Value::operator=().
220  */
221  bool operator=(const std::vector<std::string>& values) override;
222 
223  /**
224  * Returns a sub value specified by it's name
225  * @see Config::Value::operator[]().
226  */
227  Value& operator[](const std::string& name) override;
228 
229  private:
230 
231  /**
232  * Returns the boolean value of this object.
233  * @return Parsed boolean value
234  */
235  bool boolValue() const;
236 
237  /**
238  * Returns the integer value of this object.
239  * @return Parsed integer value
240  */
241  int integerValue() const;
242 
243  /**
244  * Returns the number value of this object.
245  * @return Parsed number value
246  */
247  double numberValue() const;
248 
249  /**
250  * Returns the boolean value of this object.
251  * @return Parsed boolean value
252  */
253  std::vector<bool> boolValues() const;
254 
255  /**
256  * Returns the integer values of this object.
257  * @return Parsed integer value
258  */
259  std::vector<int> integerValues() const;
260 
261  /**
262  * Returns the number values of this object.
263  * @return Parsed number value
264  */
265  std::vector<double> numberValues() const;
266 
267  /**
268  * Returns the string values of this object.
269  * @return Parsed number value
270  */
271  std::vector<std::string> stringValues() const;
272 
273  private:
274 
275  /// Value as string.
276  std::string string_;
277 
278  /// Sub values if this value is a group.
280  };
281 
282  /**
283  * This scanner is able to scan the specific config file type.
284  */
285  class ConfigScanner : public Scanner
286  {
287  public:
288 
289  /**
290  * Definition of several symbols.
291  */
292  enum Symbol : uint32_t
293  {
294  /// Curly open brackets.
296  /// Curly close brackets.
298  /// Square open brackets.
300  /// Square close brackets.
301  SYMBOL_END
302  };
303 
304  /**
305  * Definition of several keywords.
306  */
307  enum Keyword : uint32_t
308  {
309  /// TRUE keyword
311  /// FALSE keyword
312  KEYWORD_FALSE
313  };
314 
315  public:
316 
317  /**
318  * Creates a new config scanner object by a given filename.
319  * @param filename Name of the file for the scanner
320  */
321  explicit ConfigScanner(const std::string& filename);
322  };
323 
324  public:
325 
326  /**
327  * Creates a new config object.
328  */
330 
331  /**
332  * Creates a new config object by a given configuration file.
333  * @param filename Configuration file
334  * @param read True, to load the file directly
335  */
336  explicit FileConfig(const std::string& filename, const bool read = true);
337 
338  /**
339  * Returns the config file.
340  * @return Config file
341  */
342  inline const std::string& filename() const;
343 
344  /**
345  * Sets the filename of the new config objects.
346  * All old config objects will be released before.
347  * @param filename to set
348  * @param read True, to load the file directly
349  * @return True, if the file could be loaded
350  */
351  bool setFilename(const std::string& filename, const bool read = true);
352 
353  /**
354  * Reads / loads all values of this configuration.
355  * @see Config::read().
356  */
357  bool read() override;
358 
359  /**
360  * Writes / saves all values of this configuration.
361  * @see Config::write().
362  */
363  bool write() override;
364 
365  /**
366  * Returns the number of sub values.
367  * @see Config::values().
368  */
369  unsigned int values() const override;
370 
371  /**
372  * Returns the number of sub values with a given name.
373  * @see Config::values().
374  */
375  unsigned int values(const std::string& name) const override;
376 
377  /**
378  * Returns whether this value holds at least one specified sub value.
379  * @see Config::exist().
380  */
381  bool exist(const std::string& name) const override;
382 
383  /**
384  * Returns a sub value specified by it's index.
385  * @see Config::value().
386  */
387  FileValue& value(const unsigned int index, std::string& name) override;
388 
389  /**
390  * Returns a sub value specified by it's index.
391  * @see Config::value().
392  */
393  bool value(const unsigned int index, std::string& name, Value** value) override;
394 
395  /**
396  * Returns a sub value specified by it's name and it's index if more than one value exists with the same name.
397  * @see Config::value().
398  */
399  FileValue& value(const std::string& name, const unsigned int index) override;
400 
401  /**
402  * Returns a sub value specified by it's name and it's index if more than one value exists with the same name.
403  * @see Config::value().
404  */
405  bool value(const std::string& name, const unsigned int index, Value** value) override;
406 
407  /**
408  * Adds a new sub value specified by it's name.
409  * @param name The name of the sub value to create
410  * @return New sub value
411  */
412  FileValue& add(const std::string& name) override;
413 
414  /**
415  * Returns a sub value specified by it's name
416  * @see Config::operator[]().
417  */
418  FileValue& operator[](const std::string& name) override;
419 
420  /**
421  * Writes objects to a given output stream explicitly.
422  * @param values The values to write
423  * @param stream Target stream
424  * @return True, if succeeded
425  */
426  static bool write(const FileValues& values, std::ostream& stream);
427 
428  /**
429  * Returns whether this file configuration container does not hold any values.
430  * @return True, if so
431  */
432  inline bool isEmpty() const;
433 
434  protected:
435 
436  /**
437  * Reads sub values.
438  * @param values Sub values to fill
439  * @param scanner The scanner used for token scanning
440  * @param stopWithBrackets Determines whether to stop at a closing bracket
441  * @return True, if succeeded
442  */
443  bool readSubValues(FileValues& values, Scanner& scanner, bool stopWithBrackets);
444 
445  /**
446  * Writes values to a stream with a given tab offset.
447  * @param values The values to write
448  * @param stream Target stream
449  * @param offset Tab offset
450  * @return True, if succeeded
451  */
452  static bool write(const FileValues& values, std::ostream& stream, unsigned int offset);
453 
454  /**
455  * Writes a given number of tabs.
456  * @param stream Target stream
457  * @param offset Tab offset
458  * @return True, if succeeded
459  */
460  static bool writeTabs(std::ostream& stream, unsigned int offset);
461 
462  /**
463  * Returns the default value holding no data.
464  * @return The default value holding no data.
465  */
466  static FileValue& nullValue();
467 
468  protected:
469 
470  /// Roots objects.
472 
473  /// Filename of this config object.
474  std::string filename_;
475 };
476 
477 inline const std::string& FileConfig::filename() const
478 {
479  return filename_;
480 }
481 
482 inline bool FileConfig::isEmpty() const
483 {
484  return values_.empty();
485 }
486 
487 } // namespace IO
488 
489 } // namespace Ocean
490 
491 #endif // META_OCEAN_IO_FILE_CONFIG_H
This class implements a configuration value.
Definition: Config.h:77
This class implements an application or module configuration toolkit.
Definition: Config.h:38
ValueType
Definition of different value types.
Definition: Config.h:45
This scanner is able to scan the specific config file type.
Definition: FileConfig.h:286
ConfigScanner(const std::string &filename)
Creates a new config scanner object by a given filename.
Keyword
Definition of several keywords.
Definition: FileConfig.h:308
@ KEYWORD_TRUE
TRUE keyword.
Definition: FileConfig.h:310
Symbol
Definition of several symbols.
Definition: FileConfig.h:293
@ SYMBOL_CLOSE
Curly close brackets.
Definition: FileConfig.h:297
@ SYMBOL_BEGIN
Square open brackets.
Definition: FileConfig.h:299
@ SYMBOL_OPEN
Curly open brackets.
Definition: FileConfig.h:295
Definition of a file value object.
Definition: FileConfig.h:44
Value & operator[](const std::string &name) override
Returns a sub value specified by it's name.
bool operator()(const bool value) const override
Returns this value as boolean.
FileValue()=default
Creates a new value object.
std::string string_
Value as string.
Definition: FileConfig.h:276
std::vector< int > operator()(const std::vector< int > &value) const override
Returns this value as multi integer.
std::vector< double > numberValues() const
Returns the number values of this object.
std::string operator()(const std::string &value) const override
Returns this value as string.
bool value(const unsigned int index, std::string &name, Value **value) override
Returns a sub value specified by it's index.
std::vector< bool > operator()(const std::vector< bool > &value) const override
Returns this value as multi boolean.
bool operator=(const std::vector< double > &values) override
Sets this value as multi number.
FileValues subValues_
Sub values if this value is a group.
Definition: FileConfig.h:279
std::vector< int > integerValues() const
Returns the integer values of this object.
unsigned int values(const std::string &name) const override
Returns the number of sub values with a given name.
FileValue & value(const std::string &name, const unsigned int index) override
Returns a sub value specified by it's name and it's index if more than one value exists with the same...
FileValue(const FileValues &values)
Creates a new value object holding sub values.
std::vector< double > operator()(const std::vector< double > &value) const override
Returns this value as multi number.
~FileValue() override
Destructs a value object.
bool operator=(const double value) override
Sets this value as number.
bool exist(const std::string &name) const override
Returns whether this value holds at least one specified sub value.
bool operator=(const std::vector< std::string > &values) override
Sets this value as multi string.
bool value(const std::string &name, const unsigned int index, Value **value) override
Returns a sub value specified by it's name and it's index if more than one value exists with the same...
FileValue(const std::string &value, const ValueType type)
Creates a new value object by a single value as string and the real value type.
bool operator=(const std::vector< bool > &values) override
Sets this value as multi boolean.
int integerValue() const
Returns the integer value of this object.
std::vector< std::string > operator()(const std::vector< std::string > &value) const override
Returns this value as multi string.
bool operator=(const std::vector< int > &values) override
Sets this value as multi integer.
unsigned int values() const override
Returns the number of sub values.
bool boolValue() const
Returns the boolean value of this object.
FileValue & add(const std::string &name) override
Adds a new sub value specified by it's name.
std::vector< std::string > stringValues() const
Returns the string values of this object.
bool operator=(const int value) override
Sets this value as integer.
std::vector< bool > boolValues() const
Returns the boolean value of this object.
bool operator=(const bool value) override
Sets this value as boolean.
double operator()(const double value) const override
Returns this value as number.
FileValue(const FileValue &fileValue)=default
Default copy constructor.
FileValue & value(const unsigned int index, std::string &name) override
Returns a sub value specified by it's index.
int operator()(const int value) const override
Returns this value as integer.
double numberValue() const
Returns the number value of this object.
bool operator=(const std::string &value) override
Sets this value as string.
This class implements an application or module configuration toolkit using files as input or output.
Definition: FileConfig.h:29
static FileValue & nullValue()
Returns the default value holding no data.
bool value(const unsigned int index, std::string &name, Value **value) override
Returns a sub value specified by it's index.
static bool writeTabs(std::ostream &stream, unsigned int offset)
Writes a given number of tabs.
FileValues values_
Roots objects.
Definition: FileConfig.h:471
FileValue & value(const unsigned int index, std::string &name) override
Returns a sub value specified by it's index.
bool setFilename(const std::string &filename, const bool read=true)
Sets the filename of the new config objects.
std::string filename_
Filename of this config object.
Definition: FileConfig.h:474
bool read() override
Reads / loads all values of this configuration.
static bool write(const FileValues &values, std::ostream &stream, unsigned int offset)
Writes values to a stream with a given tab offset.
unsigned int values() const override
Returns the number of sub values.
FileConfig(const std::string &filename, const bool read=true)
Creates a new config object by a given configuration file.
bool exist(const std::string &name) const override
Returns whether this value holds at least one specified sub value.
FileValue & add(const std::string &name) override
Adds a new sub value specified by it's name.
std::multimap< std::string, FileValue > FileValues
Definition of a map mapping value names to value objects.
Definition: FileConfig.h:33
FileConfig()
Creates a new config object.
bool readSubValues(FileValues &values, Scanner &scanner, bool stopWithBrackets)
Reads sub values.
FileValue & value(const std::string &name, const unsigned int index) override
Returns a sub value specified by it's name and it's index if more than one value exists with the same...
bool value(const std::string &name, const unsigned int index, Value **value) override
Returns a sub value specified by it's name and it's index if more than one value exists with the same...
bool isEmpty() const
Returns whether this file configuration container does not hold any values.
Definition: FileConfig.h:482
bool write() override
Writes / saves all values of this configuration.
unsigned int values(const std::string &name) const override
Returns the number of sub values with a given name.
static bool write(const FileValues &values, std::ostream &stream)
Writes objects to a given output stream explicitly.
FileValue & operator[](const std::string &name) override
Returns a sub value specified by it's name.
const std::string & filename() const
Returns the config file.
Definition: FileConfig.h:477
This class implements a simple scanner.
Definition: Scanner.h:31
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15