Fawkes API  Fawkes Development Version
config.h
1 
2 /***************************************************************************
3  * config.h - Fawkes configuration interface
4  *
5  * Created: Mon Dec 04 17:38:32 2006
6  * Copyright 2006 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef _CONFIG_CONFIG_H_
25 #define _CONFIG_CONFIG_H_
26 
27 #include <core/exception.h>
28 #include <utils/misc/string_compare.h>
29 
30 #include <list>
31 #include <map>
32 #include <string>
33 #include <vector>
34 
35 namespace fawkes {
36 
37 class ConfigurationChangeHandler;
38 
40 {
41 public:
42  ConfigurationException(const char *msg);
43  ConfigurationException(const char *prefix, const char *msg);
44 };
45 
47 {
48 public:
49  ConfigEntryNotFoundException(const char *path);
50 };
51 
53 {
54 public:
55  ConfigTypeMismatchException(const char *path, const char *actual, const char *requested);
56 };
57 
59 {
60 public:
61  CouldNotOpenConfigException(const char *format, ...);
62 };
63 
64 #pragma GCC diagnostic push
65 #pragma GCC diagnostic ignored "-Woverloaded-virtual"
66 
68 {
69 public:
70  virtual ~Configuration()
71  {
72  }
73 
75  {
76  public:
77  virtual ~ValueIterator()
78  {
79  }
80  virtual bool next() = 0;
81  virtual bool valid() const = 0;
82 
83  virtual const char *path() const = 0;
84  virtual const char *type() const = 0;
85 
86  virtual bool is_float() const = 0;
87  virtual bool is_uint() const = 0;
88  virtual bool is_int() const = 0;
89  virtual bool is_bool() const = 0;
90  virtual bool is_string() const = 0;
91  virtual bool is_list() const = 0;
92  virtual size_t get_list_size() const = 0;
93 
94  virtual float get_float() const = 0;
95  virtual unsigned int get_uint() const = 0;
96  virtual int get_int() const = 0;
97  virtual bool get_bool() const = 0;
98  virtual std::string get_string() const = 0;
99  virtual std::vector<float> get_floats() const = 0;
100  virtual std::vector<unsigned int> get_uints() const = 0;
101  virtual std::vector<int> get_ints() const = 0;
102  virtual std::vector<bool> get_bools() const = 0;
103  virtual std::vector<std::string> get_strings() const = 0;
104  virtual std::string get_as_string() const = 0;
105 
106  virtual std::string get_comment() const = 0;
107 
108  virtual bool is_default() const = 0;
109  };
110 
111  virtual void copy(Configuration *copyconf) = 0;
112 
115 
116  virtual void load(const char *file_path) = 0;
117 
118  virtual bool exists(const char *path) = 0;
119  virtual bool is_float(const char *path) = 0;
120  virtual bool is_uint(const char *path) = 0;
121  virtual bool is_int(const char *path) = 0;
122  virtual bool is_bool(const char *path) = 0;
123  virtual bool is_string(const char *path) = 0;
124  virtual bool is_list(const char *path) = 0;
125 
126  virtual bool is_default(const char *path) = 0;
127 
128  virtual float get_float(const char *path) = 0;
129  virtual unsigned int get_uint(const char *path) = 0;
130  virtual int get_int(const char *path) = 0;
131  virtual bool get_bool(const char *path) = 0;
132  virtual std::string get_string(const char *path) = 0;
133  virtual float get_float_or_default(const char *path, const float &default_val);
134  virtual unsigned int get_uint_or_default(const char *path, const unsigned int &default_val);
135  virtual int get_int_or_default(const char *path, const int &default_val);
136  virtual bool get_bool_or_default(const char *path, const bool &default_val);
137  virtual std::string get_string_or_default(const char *path, const std::string &default_val);
138  virtual std::vector<float> get_floats(const char *path) = 0;
139  virtual std::vector<unsigned int> get_uints(const char *path) = 0;
140  virtual std::vector<int> get_ints(const char *path) = 0;
141  virtual std::vector<bool> get_bools(const char *path) = 0;
142  virtual std::vector<std::string> get_strings(const char *path) = 0;
143  virtual std::vector<float> get_floats_or_defaults(const char * path,
144  const std::vector<float> &default_val);
145  virtual std::vector<unsigned int>
146  get_uints_or_defaults(const char *path, const std::vector<unsigned int> &default_val);
147  virtual std::vector<int> get_ints_or_defaults(const char * path,
148  const std::vector<int> &default_val);
149  virtual std::vector<bool> get_bools_or_defaults(const char * path,
150  const std::vector<bool> &default_val);
151  virtual std::vector<std::string>
152  get_strings_or_defaults(const char *path, const std::vector<std::string> &default_val);
153  virtual ValueIterator *get_value(const char *path) = 0;
154  virtual std::string get_type(const char *path) = 0;
155  virtual std::string get_comment(const char *path) = 0;
156  virtual std::string get_default_comment(const char *path) = 0;
157 
158  virtual void set_float(const char *path, float f) = 0;
159  virtual void set_uint(const char *path, unsigned int uint) = 0;
160  virtual void set_int(const char *path, int i) = 0;
161  virtual void set_bool(const char *path, bool b) = 0;
162  virtual void set_string(const char *path, std::string &s) = 0;
163  virtual void set_string(const char *path, const char *s) = 0;
164  virtual void set_floats(const char *path, std::vector<float> &f) = 0;
165  virtual void set_uints(const char *path, std::vector<unsigned int> &uint) = 0;
166  virtual void set_ints(const char *path, std::vector<int> &i) = 0;
167  virtual void set_bools(const char *path, std::vector<bool> &b) = 0;
168  virtual void set_strings(const char *path, std::vector<std::string> &s) = 0;
169  virtual void set_strings(const char *path, std::vector<const char *> &s) = 0;
170  virtual void set_comment(const char *path, const char *comment) = 0;
171  virtual void set_comment(const char *path, std::string &comment) = 0;
172 
173  virtual void erase(const char *path) = 0;
174 
175  virtual void set_default_float(const char *path, float f) = 0;
176  virtual void set_default_uint(const char *path, unsigned int uint) = 0;
177  virtual void set_default_int(const char *path, int i) = 0;
178  virtual void set_default_bool(const char *path, bool b) = 0;
179  virtual void set_default_string(const char *path, std::string &s) = 0;
180  virtual void set_default_string(const char *path, const char *s) = 0;
181 
182  virtual void set_default_comment(const char *path, const char *comment) = 0;
183  virtual void set_default_comment(const char *path, std::string &comment) = 0;
184 
185  virtual void erase_default(const char *path) = 0;
186 
187  virtual ValueIterator *iterator() = 0;
188 
189  virtual ValueIterator *search(const char *path) = 0;
190 
191  virtual void lock() = 0;
192  virtual bool try_lock() = 0;
193  virtual void unlock() = 0;
194 
195  virtual void try_dump() = 0;
196 
197  /// @cond CONVENIENCE_METHODS
198  virtual bool
199  exists(const std::string &path)
200  {
201  return exists(path.c_str());
202  }
203 
204  virtual bool
205  is_float(const std::string &path)
206  {
207  return is_float(path.c_str());
208  }
209  virtual bool
210  is_uint(const std::string &path)
211  {
212  return is_uint(path.c_str());
213  }
214  virtual bool
215  is_int(const std::string &path)
216  {
217  return is_int(path.c_str());
218  }
219  virtual bool
220  is_bool(const std::string &path)
221  {
222  return is_bool(path.c_str());
223  }
224  virtual bool
225  is_string(const std::string &path)
226  {
227  return is_string(path.c_str());
228  }
229  virtual bool
230  is_list(const std::string &path)
231  {
232  return is_list(path.c_str());
233  }
234 
235  virtual bool
236  is_default(const std::string &path)
237  {
238  return is_default(path.c_str());
239  }
240 
241  virtual float
242  get_float(const std::string &path)
243  {
244  return get_float(path.c_str());
245  }
246  virtual unsigned int
247  get_uint(const std::string &path)
248  {
249  return get_uint(path.c_str());
250  }
251  virtual int
252  get_int(const std::string &path)
253  {
254  return get_int(path.c_str());
255  }
256  virtual bool
257  get_bool(const std::string &path)
258  {
259  return get_bool(path.c_str());
260  }
261  virtual std::string
262  get_string(const std::string &path)
263  {
264  return get_string(path.c_str());
265  }
266  virtual std::vector<float>
267  get_floats(const std::string &path)
268  {
269  return get_floats(path.c_str());
270  }
271  virtual std::vector<unsigned int>
272  get_uints(const std::string &path)
273  {
274  return get_uints(path.c_str());
275  }
276  virtual std::vector<int>
277  get_ints(const std::string &path)
278  {
279  return get_ints(path.c_str());
280  }
281  virtual std::vector<bool>
282  get_bools(const std::string &path)
283  {
284  return get_bools(path.c_str());
285  }
286  virtual std::vector<std::string>
287  get_strings(const std::string &path)
288  {
289  return get_strings(path.c_str());
290  }
291  virtual ValueIterator *
292  get_value(const std::string &path)
293  {
294  return get_value(path.c_str());
295  }
296  virtual std::string
297  get_type(const std::string &path)
298  {
299  return get_type(path.c_str());
300  }
301  virtual std::string
302  get_comment(const std::string &path)
303  {
304  return get_comment(path.c_str());
305  }
306  virtual std::string
307  get_default_comment(const std::string &path)
308  {
309  return get_default_comment(path.c_str());
310  }
311 
312  virtual void
313  set_float(const std::string &path, float f)
314  {
315  set_float(path.c_str(), f);
316  }
317  virtual void
318  set_uint(const std::string &path, unsigned int uint)
319  {
320  set_uint(path.c_str(), uint);
321  }
322  virtual void
323  set_int(const std::string &path, int i)
324  {
325  set_int(path.c_str(), i);
326  }
327  virtual void
328  set_bool(const std::string &path, bool b)
329  {
330  set_bool(path.c_str(), b);
331  }
332  virtual void
333  set_string(const std::string &path, std::string &s)
334  {
335  set_string(path.c_str(), s);
336  }
337  virtual void
338  set_string(const std::string &path, const char *s)
339  {
340  set_string(path.c_str(), s);
341  }
342  virtual void
343  set_floats(const std::string &path, std::vector<float> &f)
344  {
345  set_floats(path.c_str(), f);
346  }
347  virtual void
348  set_uints(const std::string &path, std::vector<unsigned int> &uint)
349  {
350  set_uints(path.c_str(), uint);
351  }
352  virtual void
353  set_ints(const std::string &path, std::vector<int> &i)
354  {
355  set_ints(path.c_str(), i);
356  }
357  virtual void
358  set_bools(const std::string &path, std::vector<bool> &b)
359  {
360  set_bools(path.c_str(), b);
361  }
362  virtual void
363  set_strings(const std::string &path, std::vector<std::string> &s)
364  {
365  set_strings(path.c_str(), s);
366  }
367  virtual void
368  set_strings(const std::string &path, std::vector<const char *> &s)
369  {
370  set_strings(path.c_str(), s);
371  }
372  virtual void
373  set_comment(const std::string &path, const char *comment)
374  {
375  set_comment(path.c_str(), comment);
376  }
377  virtual void
378  set_comment(const std::string &path, std::string &comment)
379  {
380  set_comment(path.c_str(), comment);
381  }
382 
383  virtual void
384  erase(const std::string &path)
385  {
386  erase(path.c_str());
387  }
388 
389  virtual void
390  set_default_float(const std::string &path, float f)
391  {
392  set_default_float(path.c_str(), f);
393  }
394  virtual void
395  set_default_uint(const std::string &path, unsigned int uint)
396  {
397  set_default_uint(path.c_str(), uint);
398  }
399  virtual void
400  set_default_int(const std::string &path, int i)
401  {
402  set_default_int(path.c_str(), i);
403  }
404  virtual void
405  set_default_bool(const std::string &path, bool b)
406  {
407  set_default_bool(path.c_str(), b);
408  }
409  virtual void
410  set_default_string(const std::string &path, std::string &s)
411  {
412  set_default_string(path.c_str(), s);
413  }
414  virtual void
415  set_default_string(const std::string &path, const char *s)
416  {
417  set_default_string(path.c_str(), s);
418  }
419 
420  virtual void
421  set_default_comment(const std::string &path, const char *comment)
422  {
423  set_default_comment(path.c_str(), comment);
424  }
425  virtual void
426  set_default_comment(const std::string &path, std::string &comment)
427  {
428  set_default_comment(path.c_str(), comment);
429  }
430 
431  virtual void
432  erase_default(const std::string &path)
433  {
434  erase_default(path.c_str());
435  }
436 
437  virtual ValueIterator *
438  search(const std::string &path)
439  {
440  return search(path.c_str());
441  }
442  /// @endcond
443 
444 #pragma GCC diagnostic pop
445 
446 protected:
447  /** List that contains pointers to ConfigurationChangeHandler */
448  typedef std::list<ConfigurationChangeHandler *> ChangeHandlerList;
449 
450  /** Multimap string to config change handlers. */
451  typedef std::multimap<const char *, ConfigurationChangeHandler *, StringLess>
453 
454  /** Config change handler multimap range. */
455  typedef std::pair<ChangeHandlerMultimap::iterator, ChangeHandlerMultimap::iterator>
457 
458  /** Registered change handlers. */
460  /** Change handler range. */
462 
463  ChangeHandlerList *find_handlers(const char *path);
464  void notify_handlers(const char *path, bool comment_changed = false);
465 };
466 
467 } // end namespace fawkes
468 
469 #endif
Thrown if a config entry could not be found.
Definition: config.h:47
ConfigEntryNotFoundException(const char *path)
Constructor.
Definition: config.cpp:445
Thrown if there a type problem was detected for example if you tried to query a float with get_int().
Definition: config.h:53
ConfigTypeMismatchException(const char *path, const char *actual, const char *requested)
Constructor.
Definition: config.cpp:460
Interface for configuration change handling.
Generic configuration exception.
Definition: config.h:40
ConfigurationException(const char *msg)
Constructor.
Definition: config.cpp:424
Iterator interface to iterate over config values.
Definition: config.h:75
virtual bool is_list() const =0
Check if a value is a list.
virtual bool is_uint() const =0
Check if current value is a unsigned int.
virtual std::vector< unsigned int > get_uints() const =0
Get list of values from configuration which is of type unsigned int.
virtual const char * path() const =0
Path of value.
virtual bool get_bool() const =0
Get bool value.
virtual unsigned int get_uint() const =0
Get unsigned int value.
virtual bool next()=0
Check if there is another element and advance to this if possible.
virtual std::string get_as_string() const =0
Get value as string.
virtual std::vector< bool > get_bools() const =0
Get list of values from configuration which is of type bool.
virtual float get_float() const =0
Get float value.
virtual bool is_float() const =0
Check if current value is a float.
virtual bool is_int() const =0
Check if current value is a int.
virtual bool is_default() const =0
Check if current value was read from the default config.
virtual ~ValueIterator()
Virtual emptry destructor.
Definition: config.h:77
virtual bool is_string() const =0
Check if current value is a string.
virtual bool is_bool() const =0
Check if current value is a bool.
virtual int get_int() const =0
Get int value.
virtual std::vector< std::string > get_strings() const =0
Get list of values from configuration which is of type string.
virtual bool valid() const =0
Check if the current element is valid.
virtual size_t get_list_size() const =0
Get number of elements in list value.
virtual const char * type() const =0
Type of value.
virtual std::string get_comment() const =0
Get comment of value.
virtual std::string get_string() const =0
Get string value.
virtual std::vector< int > get_ints() const =0
Get list of values from configuration which is of type int.
virtual std::vector< float > get_floats() const =0
Get list of values from configuration which is of type float.
Interface for configuration handling.
Definition: config.h:68
virtual void set_bools(const char *path, std::vector< bool > &b)=0
Set new value in configuration of type bool.
virtual float get_float_or_default(const char *path, const float &default_val)
Get value from configuration which is of type float, or the given default if the path does not exist.
Definition: config.cpp:696
virtual void set_comment(const char *path, const char *comment)=0
Set new comment for existing value.
virtual unsigned int get_uint(const char *path)=0
Get value from configuration which is of type unsigned int.
virtual bool is_float(const char *path)=0
Check if a value is of type float.
virtual bool is_uint(const char *path)=0
Check if a value is of type unsigned int.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
virtual void set_uint(const char *path, unsigned int uint)=0
Set new value in configuration of type unsigned int.
virtual std::string get_type(const char *path)=0
Get type of value at given path.
virtual std::vector< unsigned int > get_uints_or_defaults(const char *path, const std::vector< unsigned int > &default_val)
Get list of values from configuration which is of type unsigned int, or the given default if the path...
Definition: config.cpp:756
virtual void set_ints(const char *path, std::vector< int > &i)=0
Set new value in configuration of type int.
virtual std::string get_string_or_default(const char *path, const std::string &default_val)
Get value from configuration which is of type string, or the given default if the path does not exist...
Definition: config.cpp:736
virtual float get_float(const char *path)=0
Get value from configuration which is of type float.
virtual std::vector< int > get_ints_or_defaults(const char *path, const std::vector< int > &default_val)
Get list of values from configuration which is of type int, or the given default if the path does not...
Definition: config.cpp:766
void notify_handlers(const char *path, bool comment_changed=false)
Notify handlers for given path.
Definition: config.cpp:674
virtual void set_bool(const char *path, bool b)=0
Set new value in configuration of type bool.
virtual unsigned int get_uint_or_default(const char *path, const unsigned int &default_val)
Get value from configuration which is of type unsigned int, or the given default if the path does not...
Definition: config.cpp:706
virtual std::vector< float > get_floats(const char *path)=0
Get list of values from configuration which is of type float.
virtual void set_default_comment(const char *path, std::string &comment)=0
Set new default comment for existing default configuration value.
std::multimap< const char *, ConfigurationChangeHandler *, StringLess > ChangeHandlerMultimap
Multimap string to config change handlers.
Definition: config.h:452
virtual std::vector< std::string > get_strings(const char *path)=0
Get list of values from configuration which is of type string.
virtual std::vector< float > get_floats_or_defaults(const char *path, const std::vector< float > &default_val)
Get list of values from configuration which is of type float, or the given default if the path does n...
Definition: config.cpp:746
virtual ValueIterator * iterator()=0
Iterator for all values.
virtual void rem_change_handler(ConfigurationChangeHandler *h)
Remove a configuration change handler.
Definition: config.cpp:619
virtual void set_default_float(const char *path, float f)=0
Set new default value in configuration of type float.
virtual void set_float(const char *path, float f)=0
Set new value in configuration of type float.
std::pair< ChangeHandlerMultimap::iterator, ChangeHandlerMultimap::iterator > ChangeHandlerMultimapRange
Config change handler multimap range.
Definition: config.h:456
virtual void set_string(const char *path, const char *s)=0
Set new value in configuration of type string.
virtual ValueIterator * search(const char *path)=0
Iterator with search results.
virtual bool is_bool(const char *path)=0
Check if a value is of type bool.
virtual void set_default_string(const char *path, std::string &s)=0
Set new default value in configuration of type string.
virtual ValueIterator * get_value(const char *path)=0
Get value from configuration.
virtual void set_string(const char *path, std::string &s)=0
Set new value in configuration of type string.
virtual void set_uints(const char *path, std::vector< unsigned int > &uint)=0
Set new value in configuration of type unsigned int.
virtual bool is_list(const char *path)=0
Check if a value is a list.
virtual int get_int_or_default(const char *path, const int &default_val)
Get value from configuration which is of type int, or the given default if the path does not exist.
Definition: config.cpp:716
virtual std::string get_default_comment(const char *path)=0
Get comment of value at given path.
virtual std::vector< bool > get_bools(const char *path)=0
Get list of values from configuration which is of type bool.
std::list< ConfigurationChangeHandler * > ChangeHandlerList
List that contains pointers to ConfigurationChangeHandler.
Definition: config.h:448
virtual void erase_default(const char *path)=0
Erase the given default value from the configuration.
virtual void load(const char *file_path)=0
Load configuration.
virtual bool is_int(const char *path)=0
Check if a value is of type int.
virtual void set_strings(const char *path, std::vector< const char * > &s)=0
Set new value in configuration of type string.
virtual bool get_bool_or_default(const char *path, const bool &default_val)
Get value from configuration which is of type bool, or the given default if the path does not exist.
Definition: config.cpp:726
ChangeHandlerMultimapRange _ch_range
Change handler range.
Definition: config.h:461
virtual bool exists(const char *path)=0
Check if a given value exists.
virtual std::vector< int > get_ints(const char *path)=0
Get list of values from configuration which is of type int.
virtual void set_floats(const char *path, std::vector< float > &f)=0
Set new value in configuration of type float.
virtual bool is_string(const char *path)=0
Check if a value is of type string.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
virtual std::vector< unsigned int > get_uints(const char *path)=0
Get list of values from configuration which is of type unsigned int.
virtual void lock()=0
Lock the config.
virtual std::vector< bool > get_bools_or_defaults(const char *path, const std::vector< bool > &default_val)
Get list of values from configuration which is of type bool, or the given default if the path does no...
Definition: config.cpp:776
virtual void unlock()=0
Unlock the config.
virtual void set_default_bool(const char *path, bool b)=0
Set new default value in configuration of type bool.
virtual void set_int(const char *path, int i)=0
Set new value in configuration of type int.
virtual ~Configuration()
Virtual empty destructor.
Definition: config.h:70
virtual int get_int(const char *path)=0
Get value from configuration which is of type int.
virtual void set_default_uint(const char *path, unsigned int uint)=0
Set new default value in configuration of type unsigned int.
ChangeHandlerMultimap _change_handlers
Registered change handlers.
Definition: config.h:459
virtual void set_default_int(const char *path, int i)=0
Set new default value in configuration of type int.
virtual void set_comment(const char *path, std::string &comment)=0
Set new comment for existing value.
virtual void set_default_comment(const char *path, const char *comment)=0
Set new default comment for existing default configuration value.
virtual void set_strings(const char *path, std::vector< std::string > &s)=0
Set new value in configuration of type string.
virtual std::string get_comment(const char *path)=0
Get comment of value at given path.
virtual void set_default_string(const char *path, const char *s)=0
Set new default value in configuration of type string.
virtual void add_change_handler(ConfigurationChangeHandler *h)
Add a configuration change handler.
Definition: config.cpp:603
virtual bool try_lock()=0
Try to lock the config.
virtual void copy(Configuration *copyconf)=0
Copies all values from the given configuration.
virtual void try_dump()=0
Try to dump configuration.
ChangeHandlerList * find_handlers(const char *path)
Find handlers for given path.
Definition: config.cpp:651
virtual bool is_default(const char *path)=0
Check if a value was read from the default config.
virtual void erase(const char *path)=0
Erase the given value from the configuration.
virtual std::vector< std::string > get_strings_or_defaults(const char *path, const std::vector< std::string > &default_val)
Get list of values from configuration which is of type string, or the given default if the path does ...
Definition: config.cpp:786
Thrown if config could not be opened.
Definition: config.h:59
CouldNotOpenConfigException(const char *format,...)
Constructor.
Definition: config.cpp:476
Base class for exceptions in Fawkes.
Definition: exception.h:36
Fawkes library namespace.