humoto
config.h
Go to the documentation of this file.
1 /**
2  @file
3  @author Alexander Sherikov
4  @copyright 2014-2017 INRIA. Licensed under the Apache License, Version 2.0.
5  (see @ref LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
6 
7  @brief
8 */
9 
10 #pragma once
11 
12 namespace humoto
13 {
14  /**
15  * @brief Namespace of classes related to configuration handling
16  */
17  namespace config
18  {
19  }
20 }
21 
22 #define HUMOTO_CONFIG_DEFINE_ACCESSORS "humoto/config/define_accessors.h"
23 
24 #ifdef HUMOTO_USE_CONFIG
25  #define HUMOTO_CONFIG_WRITE_PARENT_CLASS(parent_class) parent_class::writeConfigEntries(writer);
26  #define HUMOTO_CONFIG_WRITE_MEMBER_CLASS(member, name) member.writeNestedConfig(writer, name);
27 
28  #define HUMOTO_CONFIG_WRITE_COMPOUND_(entry) writer.writeCompound(entry##_, #entry);
29  #define HUMOTO_CONFIG_WRITE_COMPOUND(entry) writer.writeCompound(entry, #entry);
30 
31  #define HUMOTO_CONFIG_WRITE_SCALAR_(entry) writer.writeScalar(entry##_, #entry);
32  #define HUMOTO_CONFIG_WRITE_SCALAR(entry) writer.writeScalar(entry, #entry);
33 
34  #define HUMOTO_CONFIG_WRITE_ENUM_(entry) writer.writeEnum(entry##_, #entry);
35  #define HUMOTO_CONFIG_WRITE_ENUM(entry) writer.writeEnum(entry, #entry);
36 
37 
38  #define HUMOTO_CONFIG_READ_PARENT_CLASS(parent_class) parent_class::readConfigEntries(reader, crash_on_missing_entry);
39  #define HUMOTO_CONFIG_READ_MEMBER_CLASS(member, name) member.readNestedConfig(reader, name, crash_on_missing_entry);
40 
41  #define HUMOTO_CONFIG_READ_COMPOUND_(entry) reader.readCompound(entry##_, #entry, crash_on_missing_entry);
42  #define HUMOTO_CONFIG_READ_COMPOUND(entry) reader.readCompound(entry, #entry, crash_on_missing_entry);
43 
44  #define HUMOTO_CONFIG_READ_SCALAR_(entry) reader.readScalar(entry##_, #entry, crash_on_missing_entry);
45  #define HUMOTO_CONFIG_READ_SCALAR(entry) reader.readScalar(entry, #entry, crash_on_missing_entry);
46 
47  #define HUMOTO_CONFIG_READ_ENUM_(entry) reader.readEnum(entry##_, #entry, crash_on_missing_entry);
48  #define HUMOTO_CONFIG_READ_ENUM(entry) reader.readEnum(entry, #entry, crash_on_missing_entry);
49 
50  // ----------------------------
51 
52  #ifndef HUMOTO_CONFIG_CONFIGURABLE_BASE_PARENT
53  #error "Header inclusion error: configuration is enabled, but no configuration bridges included."
54  #endif
55 
56  namespace humoto
57  {
58  namespace config
59  {
60  /**
61  * @brief Configurable base class.
62  */
63  template <bool t_crash_on_missing_entry>
65  {
66  protected:
67  static const bool default_crash_on_missing_entry_ = t_crash_on_missing_entry;
68 
69 
70  protected:
71  /**
72  * @brief Protected destructor: prevent destruction of the child
73  * classes through a base pointer.
74  */
77 
78 
79  /**
80  * @brief Set members to their default values.
81  */
82  virtual void setDefaults() = 0;
83 
84 
85  /**
86  * @brief Return the default name of a configuration node
87  * corresponding to this class
88  *
89  * @return the name
90  *
91  * @attention Implementation of this method is added
92  * automatically upon inclusion of define_accessors.h if
93  * HUMOTO_CONFIG_SECTION_ID is defined.
94  */
95  virtual const std::string & getConfigSectionID() const = 0;
96 
97 
98  /**
99  * @brief This function is called automaticaly after reading
100  * a configuration file. Does nothing by default.
101  */
102  virtual void finalize() {};
103 
104 
105  /**
106  * @brief Get number of entries in the corresponding
107  * configuration node.
108  *
109  * @return number of entries
110  */
111  virtual std::size_t getNumberOfEntries() const = 0;
112 
113 
114  /// @{
115  /**
116  * These functions are always defined automatically.
117  */
118  using HUMOTO_CONFIG_CONFIGURABLE_BASE_PARENT::writeConfigEntries;
119  using HUMOTO_CONFIG_CONFIGURABLE_BASE_PARENT::readConfigEntries;
120  /// @}
121 
122 
123  public:
124  /**
125  * @brief Read nested configuration node.
126  *
127  * @param[in] reader
128  * @param[in] crash_on_missing_entry
129  * @param[in] node_name node name, the default is used if empty
130  */
131  template <class t_Reader>
132  void readNestedConfig( t_Reader & reader,
133  const std::string & node_name,
134  const bool crash_on_missing_entry = default_crash_on_missing_entry_)
135  {
136  try
137  {
138  setDefaults();
139  if (reader.descend(node_name))
140  {
141  readConfigEntries(reader, crash_on_missing_entry);
142  reader.ascend();
143  }
144  else
145  {
146  if (crash_on_missing_entry)
147  {
148  HUMOTO_THROW_MSG(std::string("Configuration file does not contain entry '") + node_name + "'.");
149  }
150  }
151  }
152  catch(const std::exception &e)
153  {
154  HUMOTO_THROW_MSG(std::string("Failed to parse node <") + node_name + "> in the configuration file: " + e.what());
155  }
156  }
157 
158 
159  /**
160  * @brief Read configuration (assuming the configuration node
161  * to be in the root).
162  *
163  * @param[in] reader configuration reader
164  * @param[in] crash_on_missing_entry
165  */
166  template <class t_Reader>
167  void readConfig(t_Reader & reader,
168  const bool crash_on_missing_entry = default_crash_on_missing_entry_)
169  {
170  readNestedConfig(reader, getConfigSectionID(), crash_on_missing_entry);
171  }
172 
173 
174  /**
175  * @brief Read configuration (assuming the configuration node
176  * to be in the root).
177  *
178  * @param[in] reader configuration reader
179  * @param[in] crash_on_missing_entry
180  * @param[in] node_name node name, the default is used if empty
181  */
182  template <class t_Reader>
183  void readConfig(t_Reader & reader,
184  const std::string & node_name,
185  const bool crash_on_missing_entry = default_crash_on_missing_entry_)
186  {
187  readNestedConfig(reader, node_name, crash_on_missing_entry);
188  }
189 
190 
191  /**
192  * @brief Read configuration (assuming the configuration node
193  * to be in the root).
194  *
195  * @param[in] reader configuration reader
196  * @param[in] crash_on_missing_entry
197  * @param[in] node_name node name, the default is used if empty
198  *
199  * @note Intercept implicit conversion of a pointer to bool.
200  */
201  template <class t_Reader>
202  void readConfig(t_Reader & reader,
203  const char * node_name,
204  const bool crash_on_missing_entry = default_crash_on_missing_entry_)
205  {
206  readNestedConfig(reader, node_name, crash_on_missing_entry);
207  }
208 
209 
210  /**
211  * @brief Read configuration (assuming the configuration node
212  * to be in the root).
213  *
214  * @param[in] file_name file name
215  * @param[in] crash_on_missing_entry
216  */
217  template <class t_Reader>
218  void readConfig(const std::string &file_name,
219  const bool crash_on_missing_entry = default_crash_on_missing_entry_)
220  {
221  t_Reader reader(file_name);
222  readNestedConfig(reader, getConfigSectionID(), crash_on_missing_entry);
223  }
224 
225 
226  /**
227  * @brief Read configuration (assuming the configuration node
228  * to be in the root).
229  *
230  * @param[in] file_name file name
231  * @param[in] node_name node name, the default is used if empty
232  * @param[in] crash_on_missing_entry
233  */
234  template <class t_Reader>
235  void readConfig(const std::string &file_name,
236  const std::string &node_name,
237  const bool crash_on_missing_entry = default_crash_on_missing_entry_)
238  {
239  t_Reader reader(file_name);
240  readNestedConfig(reader, node_name, crash_on_missing_entry);
241  }
242 
243 
244  /**
245  * @brief Read configuration (assuming the configuration node
246  * to be in the root).
247  *
248  * @param[in] file_name file name
249  * @param[in] crash_on_missing_entry
250  * @param[in] node_name node name, the default is used if empty
251  *
252  * @note Intercept implicit conversion of a pointer to bool.
253  */
254  template <class t_Reader>
255  void readConfig(const std::string &file_name,
256  const char *node_name,
257  const bool crash_on_missing_entry = default_crash_on_missing_entry_)
258  {
259  t_Reader reader(file_name);
260  readNestedConfig(reader, node_name, crash_on_missing_entry);
261  }
262 
263  // ------------------------------------------
264 
265 
266  /**
267  * @brief Write nested configuration node
268  *
269  * @param[in,out] writer configuration writer
270  */
271  template <class t_Writer>
272  void writeNestedConfig( t_Writer& writer) const
273  {
274  writeNestedConfig(writer, getConfigSectionID());
275  }
276 
277 
278  /**
279  * @brief Write nested configuration node
280  *
281  * @param[in,out] writer configuration writer
282  * @param[in] node_name node name, the default is used if empty
283  */
284  template <class t_Writer>
285  void writeNestedConfig( t_Writer& writer,
286  const std::string &node_name) const
287  {
288  writer.descend(node_name, getNumberOfEntries());
289  writeConfigEntries(writer);
290  writer.ascend();
291  }
292 
293 
294  /**
295  * @brief Write configuration
296  *
297  * @param[in,out] writer configuration writer
298  */
299  template <class t_Writer>
300  void writeConfig(t_Writer& writer) const
301  {
302  writeConfig(writer, getConfigSectionID());
303  }
304 
305 
306  /**
307  * @brief Write configuration
308  *
309  * @param[in,out] writer configuration writer
310  * @param[in] node_name node name, the default is used if empty
311  */
312  template <class t_Writer>
313  void writeConfig(t_Writer& writer,
314  const std::string &node_name) const
315  {
316  writer.initRoot();
317  writeNestedConfig(writer, node_name);
318  writer.flush();
319  }
320 
321 
322  /**
323  * @brief Write configuration.
324  *
325  * @param[in] file_name file name
326  */
327  template <class t_Writer>
328  void writeConfig(const std::string &file_name) const
329  {
330  t_Writer writer(file_name);
331  writeConfig(writer);
332  }
333 
334 
335  /**
336  * @brief Write configuration.
337  *
338  * @param[in] file_name file name
339  * @param[in] node_name node name, the default is used if empty
340  */
341  template <class t_Writer>
342  void writeConfig(const std::string &file_name,
343  const std::string &node_name) const
344  {
345  t_Writer writer(file_name);
346  writeConfig(writer, node_name);
347  }
348  };
349 
350 
351 
352  /// Default configurable base is strict
354  {
355  protected:
356  /**
357  * @brief Protected destructor: prevent destruction of the child
358  * classes through a base pointer.
359  */
362  };
363 
364 
366  {
367  protected:
368  /**
369  * @brief Protected destructor: prevent destruction of the child
370  * classes through a base pointer.
371  */
374  };
375 
376 
378  {
379  protected:
380  /**
381  * @brief Protected destructor: prevent destruction of the child
382  * classes through a base pointer.
383  */
386  };
387  }
388  }
389 
390 #else
391 
392  namespace humoto
393  {
394  namespace config
395  {
396  // Some classes may inherit from this
397  class HUMOTO_LOCAL ConfigurableBase
398  {
399  protected:
400  /**
401  * @brief Protected destructor: prevent destruction of the
402  * child classes through a base pointer.
403  */
404  ~ConfigurableBase() {}
405  };
406 
407 
408  // Some classes may inherit from this
409  class HUMOTO_LOCAL StrictConfigurableBase
410  {
411  protected:
412  /**
413  * @brief Protected destructor: prevent destruction of the
414  * child classes through a base pointer.
415  */
416  ~StrictConfigurableBase() {}
417  };
418 
419 
420  // Some classes may inherit from this
421  class HUMOTO_LOCAL RelaxedConfigurableBase
422  {
423  protected:
424  /**
425  * @brief Protected destructor: prevent destruction of the
426  * child classes through a base pointer.
427  */
428  ~RelaxedConfigurableBase() {}
429  };
430  }
431  }
432 
433  #define HUMOTO_CONFIG_DISABLED
434 
435 #endif //HUMOTO_USE_CONFIG
void readConfig(const std::string &file_name, const bool crash_on_missing_entry=default_crash_on_missing_entry_)
Read configuration (assuming the configuration node to be in the root).
Definition: config.h:218
void writeNestedConfig(t_Writer &writer, const std::string &node_name) const
Write nested configuration node.
Definition: config.h:285
~RelaxedConfigurableBase()
Protected destructor: prevent destruction of the child classes through a base pointer.
Definition: config.h:384
void readNestedConfig(t_Reader &reader, const std::string &node_name, const bool crash_on_missing_entry=default_crash_on_missing_entry_)
Read nested configuration node.
Definition: config.h:132
#define HUMOTO_LOCAL
Definition: export_import.h:26
void readConfig(const std::string &file_name, const char *node_name, const bool crash_on_missing_entry=default_crash_on_missing_entry_)
Read configuration (assuming the configuration node to be in the root).
Definition: config.h:255
void writeConfig(const std::string &file_name, const std::string &node_name) const
Write configuration.
Definition: config.h:342
Default configurable base is strict.
Definition: config.h:353
virtual void finalize()
This function is called automaticaly after reading a configuration file. Does nothing by default...
Definition: config.h:102
#define HUMOTO_THROW_MSG(s)
HUMOTO_THROW_MSG throws an error message concatenated with the name of the function (if supported)...
void readConfig(t_Reader &reader, const char *node_name, const bool crash_on_missing_entry=default_crash_on_missing_entry_)
Read configuration (assuming the configuration node to be in the root).
Definition: config.h:202
~CommonConfigurableBase()
Protected destructor: prevent destruction of the child classes through a base pointer.
Definition: config.h:75
~StrictConfigurableBase()
Protected destructor: prevent destruction of the child classes through a base pointer.
Definition: config.h:372
~ConfigurableBase()
Protected destructor: prevent destruction of the child classes through a base pointer.
Definition: config.h:360
void readConfig(const std::string &file_name, const std::string &node_name, const bool crash_on_missing_entry=default_crash_on_missing_entry_)
Read configuration (assuming the configuration node to be in the root).
Definition: config.h:235
void writeNestedConfig(t_Writer &writer) const
Write nested configuration node.
Definition: config.h:272
void writeConfig(const std::string &file_name) const
Write configuration.
Definition: config.h:328
Configurable base class.
Definition: config.h:64
void readConfig(t_Reader &reader, const bool crash_on_missing_entry=default_crash_on_missing_entry_)
Read configuration (assuming the configuration node to be in the root).
Definition: config.h:167
The root namespace of HuMoTo.
Definition: config.h:12
#define HUMOTO_CONFIG_CONFIGURABLE_BASE_PARENT
Definition: config_yaml.h:60
void writeConfig(t_Writer &writer) const
Write configuration.
Definition: config.h:300
void readConfig(t_Reader &reader, const std::string &node_name, const bool crash_on_missing_entry=default_crash_on_missing_entry_)
Read configuration (assuming the configuration node to be in the root).
Definition: config.h:183
void writeConfig(t_Writer &writer, const std::string &node_name) const
Write configuration.
Definition: config.h:313