Ocean
XMLConfig.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_XML_CONFIG_H
9 #define META_OCEAN_IO_XML_CONFIG_H
10 
11 #include "ocean/io/IO.h"
12 
13 #include "ocean/base/Config.h"
14 
15 #include <map>
16 
17 // @cond
18 namespace tinyxml2
19 {
20  // Forward declaration.
21  class XMLDocument;
22  // Forward declaration.
23  class XMLNode;
24  // Forward declaration.
25  class XMLText;
26 }
27 // @endcond
28 
29 namespace Ocean
30 {
31 
32 namespace IO
33 {
34 
35 /**
36  * This class implements a configuration toolkit using XML encoding to store the parameters.
37  * @ingroup io
38  */
39 class OCEAN_IO_EXPORT XMLConfig final : public Config
40 {
41  public:
42 
43  /**
44  * Definition of a file value object.
45  */
46  class OCEAN_IO_EXPORT XMLValue final : public Value
47  {
48  friend class XMLConfig;
49 
50  public:
51 
52  /**
53  * Creates a new value object.
54  */
55  XMLValue(XMLConfig* config, tinyxml2::XMLNode* node);
56 
57  /**
58  * Destructs a value object.
59  */
60  ~XMLValue() final;
61 
62  /**
63  * Returns the number of sub values.
64  * @see Config::Value::values().
65  */
66  unsigned int values() const override;
67 
68  /**
69  * Returns the number of sub values with a given name.
70  * @see Config::Value::values().
71  */
72  unsigned int values(const std::string& name) const override;
73 
74  /**
75  * Returns whether this value holds at least one specified sub value.
76  * @see Config::Value::exist().
77  */
78  bool exist(const std::string& name) const override;
79 
80  /**
81  * Returns a sub value specified by it's name and it's index if more than one value exists with the same name.
82  * @see Config::Value::value().
83  */
84  XMLValue& value(const std::string& name, const unsigned int index) override;
85 
86  /**
87  * Returns a sub value specified by it's name and it's index if more than one value exists with the same name.
88  * @see Config::Value::value().
89  */
90  bool value(const std::string& name, const unsigned int index, Value** value) override;
91 
92  /**
93  * Returns a sub value specified by it's index.
94  * @see Config::Value::value().
95  */
96  XMLValue& value(const unsigned int index, std::string& name) override;
97 
98  /**
99  * Returns a sub value specified by it's index.
100  * @see Config::Value::value().
101  */
102  bool value(const unsigned int index, std::string& name, Value** value) override;
103 
104  /**
105  * Adds a new sub value specified by it's name.
106  * @param name The name of the sub value to create
107  * @return New sub value
108  */
109  XMLValue& add(const std::string& name) override;
110 
111  /**
112  * Returns the value of a specified attribute of this value.
113  * @param name The name of the attribute for which the value will be returned
114  * @return The value of the attribute, an empty string if the attribute does not exist
115  */
116  std::string attribute(const std::string& name) const;
117 
118  /**
119  * Returns this value as boolean.
120  * @see Config::Value::operator().
121  */
122  bool operator()(const bool value) const override;
123 
124  /**
125  * Returns this value as integer.
126  * @see Config::Value::operator().
127  */
128  int operator()(const int value) const override;
129 
130  /**
131  * Returns this value as number.
132  * @see Config::Value::operator().
133  */
134  double operator()(const double value) const override;
135 
136  /**
137  * Returns this value as string.
138  * @see Config::Value::operator().
139  */
140  std::string operator()(const std::string& value) const override;
141 
142  /**
143  * Returns this value as multi boolean.
144  * @see Config::Value::operator().
145  */
146  std::vector<bool> operator()(const std::vector<bool>& value) const override;
147 
148  /**
149  * Returns this value as multi integer.
150  * @see Config::Value::operator().
151  */
152  std::vector<int> operator()(const std::vector<int>& value) const override;
153 
154  /**
155  * Returns this value as multi number.
156  * @see Config::Value::operator().
157  */
158  std::vector<double> operator()(const std::vector<double>& value) const override;
159 
160  /**
161  * Returns this value as multi string.
162  * @see Config::Value::operator().
163  */
164  std::vector<std::string> operator()(const std::vector<std::string>& value) const override;
165 
166  /**
167  * Sets this value as boolean.
168  * @see Config::Value::operator=().
169  */
170  bool operator=(const bool value) override;
171 
172  /**
173  * Sets this value as integer.
174  * @see Config::Value::operator=().
175  */
176  bool operator=(const int value) override;
177 
178  /**
179  * Sets this value as number.
180  * @see Config::Value::operator=().
181  */
182  bool operator=(const double value) override;
183 
184  /**
185  * Sets this value as string.
186  * @see Config::Value::operator=().
187  */
188  bool operator=(const std::string& value) override;
189 
190  /**
191  * Sets this value as multi boolean.
192  * @see Config::Value::operator=().
193  */
194  bool operator=(const std::vector<bool>& values) override;
195 
196  /**
197  * Sets this value as multi integer.
198  * @see Config::Value::operator=().
199  */
200  bool operator=(const std::vector<int>& values) override;
201 
202  /**
203  * Sets this value as multi number.
204  * @see Config::Value::operator=().
205  */
206  bool operator=(const std::vector<double>& values) override;
207 
208  /**
209  * Sets this value as multi string.
210  * @see Config::Value::operator=().
211  */
212  bool operator=(const std::vector<std::string>& values) override;
213 
214  /**
215  * Returns a sub value specified by it's name
216  * @see Config::Value::operator[]().
217  */
218  XMLValue& operator[](const std::string& name) override;
219 
220  /**
221  * Returns whether the config value is valid.
222  * @return True, if so
223  */
224  explicit operator bool() const;
225 
226  private:
227 
228  /**
229  * Disabled copy constructor.
230  * @param object The object which would be copied
231  */
232  XMLValue(const XMLValue& object) = delete;
233 
234  /**
235  * Disabled copy operator.
236  * @param object The object which would be copied
237  * @return Reference to this object
238  */
239  XMLValue& operator=(const XMLValue& object) = delete;
240 
241  private:
242 
243  /// XML node.
244  tinyxml2::XMLNode* xmlNode_ = nullptr;
245 
246  /// Main config object.
247  XMLConfig* xmlConfig_ = nullptr;
248  };
249 
250  /**
251  * Definition of a vector holding values.
252  */
253  typedef std::vector<XMLValue*> XMLValues;
254 
255  public:
256 
257  /**
258  * Creates a new config object.
259  */
260  XMLConfig();
261 
262  /**
263  * Destructs a xml config object.
264  */
265  ~XMLConfig() override;
266 
267  /**
268  * Creates a new config object by a given configuration file.
269  * @param filename Configuration file
270  * @param read True, to load the file directly
271  */
272  explicit XMLConfig(const std::string& filename, const bool read = true);
273 
274  /**
275  * Creates a new config object by a given configuration input stream.
276  * @param inputStream The input stream providing the configuration information
277  * @param read True, to load the file directly
278  */
279  explicit XMLConfig(std::istream& inputStream, const bool read = true);
280 
281  /**
282  * Returns the config file.
283  * @return Config file
284  */
285  inline const std::string& filename() const;
286 
287  /**
288  * Sets the filename of the new config objects.
289  * All old config objects will be released before.
290  * @param filename to set
291  * @param read True, to load the file directly
292  * @return True, if the file could be loaded
293  */
294  bool setFilename(const std::string& filename, const bool read = true);
295 
296  /**
297  * Reads / loads all values of this configuration.
298  * @see Config::read().
299  */
300  bool read() override;
301 
302  /**
303  * Writes / saves all values of this configuration.
304  * @see Config::write().
305  */
306  bool write() override;
307 
308  /**
309  * Returns the number of sub values.
310  * @see Config::values().
311  */
312  unsigned int values() const override;
313 
314  /**
315  * Returns the number of sub values with a given name.
316  * @see Config::values().
317  */
318  unsigned int values(const std::string& name) const override;
319 
320  /**
321  * Returns whether this value holds at least one specified sub value.
322  * @see Config::exist().
323  */
324  bool exist(const std::string& name) const override;
325 
326  /**
327  * Returns a sub value specified by it's index.
328  * @see Config::value().
329  */
330  XMLValue& value(const unsigned int index, std::string& name) override;
331 
332  /**
333  * Returns a sub value specified by it's index.
334  * @see Config::value().
335  */
336  bool value(const unsigned int index, std::string& name, Value** value) override;
337 
338  /**
339  * Returns a sub value specified by it's name and it's index if more than one value exists with the same name.
340  * @see Config::value().
341  */
342  XMLValue& value(const std::string& name, const unsigned int index) override;
343 
344  /**
345  * Returns a sub value specified by it's name and it's index if more than one value exists with the same name.
346  * @see Config::value().
347  */
348  bool value(const std::string& name, const unsigned int index, Value** value) override;
349 
350  /**
351  * Adds a new sub value specified by it's name.
352  * @param name The name of the sub value to create
353  * @return New sub value
354  */
355  XMLValue& add(const std::string& name) override;
356 
357  /**
358  * Returns a sub value specified by it's name
359  * @see Config::operator[]().
360  */
361  XMLValue& operator[](const std::string& name) override;
362 
363  /**
364  * Returns whether this file configuration container does not hold any values.
365  * @return True, if so
366  */
367  bool isEmpty() const;
368 
369  protected:
370 
371  /**
372  * Returns the first text node connected with a given node.
373  * @param xmlNode Node for that the connected text node is requested
374  * @return First text node
375  */
376  static const tinyxml2::XMLText* firstText(const tinyxml2::XMLNode* xmlNode);
377 
378  /**
379  * Returns the first text node connected with a given node.
380  * @param xmlNode Node for that the connected text node is requested
381  * @return First text node
382  */
383  static tinyxml2::XMLText* firstText(tinyxml2::XMLNode* xmlNode);
384 
385  /**
386  * Returns the default value holding no data.
387  * @return The default value holding no data.
388  */
389  static XMLValue& nullValue();
390 
391  protected:
392 
393  /// XML parser.
394  tinyxml2::XMLDocument* xmlDocument_ = nullptr;
395 
396  /// XML values.
397  XMLValues xmlIntermediateValues_;
398 
399  /// Filename of this config object.
400  std::string filename_;
401 
402  /// The input stream.
403  std::istream& inputStream_;
404 
405  /// The input file stream which is used if a filename is specified.
406  std::ifstream inputFileStream_;
407 };
408 
409 inline const std::string& XMLConfig::filename() const
410 {
411  return filename_;
412 }
413 
414 } // namespace IO
415 
416 } // namespace Ocean
417 
418 #endif // META_OCEAN_IO_XML_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
Definition of a file value object.
Definition: XMLConfig.h:46
This class implements a configuration toolkit using XML encoding to store the parameters.
Definition: XMLConfig.h:39
std::vector< XMLValue * > XMLValues
Definition of a vector holding values.
Definition: XMLConfig.h:252
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15