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 #include <string>
30 #include <list>
31 #include <map>
32 
33 namespace fawkes {
34 
35 class ConfigurationChangeHandler;
36 
38 {
39  public:
40  ConfigurationException(const char *msg);
41  ConfigurationException(const char *prefix, const char *msg);
42 };
43 
45 {
46  public:
47  ConfigEntryNotFoundException(const char *path);
48 };
49 
51 {
52  public:
53  ConfigTypeMismatchException(const char *path,
54  const char *actual, const char *requested);
55 };
56 
58 {
59  public:
60  CouldNotOpenConfigException(const char *format, ...);
61 };
62 
64 {
65  public:
66  virtual ~Configuration() {}
67 
69  {
70  public:
71  virtual ~ValueIterator() {}
72  virtual bool next() = 0;
73  virtual bool valid() const = 0;
74 
75  virtual const char * path() const = 0;
76  virtual const char * type() const = 0;
77 
78  virtual bool is_float() const = 0;
79  virtual bool is_uint() const = 0;
80  virtual bool is_int() const = 0;
81  virtual bool is_bool() const = 0;
82  virtual bool is_string() const = 0;
83 
84  virtual float get_float() const = 0;
85  virtual unsigned int get_uint() const = 0;
86  virtual int get_int() const = 0;
87  virtual bool get_bool() const = 0;
88  virtual std::string get_string() const = 0;
89  virtual std::string get_as_string() const = 0;
90 
91  virtual std::string get_comment() const = 0;
92 
93  virtual bool is_default() const = 0;
94  };
95 
96  virtual void copy(Configuration *copyconf) = 0;
97 
100 
101  virtual void load(const char *name, const char *defaults_name,
102  const char *tag = NULL) = 0;
103 
104  virtual void tag(const char *tag) = 0;
105  virtual std::list<std::string> tags() = 0;
106 
107  virtual bool exists(const char *path) = 0;
108  virtual bool is_float(const char *path) = 0;
109  virtual bool is_uint(const char *path) = 0;
110  virtual bool is_int(const char *path) = 0;
111  virtual bool is_bool(const char *path) = 0;
112  virtual bool is_string(const char *path) = 0;
113 
114  virtual bool is_default(const char *path) = 0;
115 
116  virtual float get_float(const char *path) = 0;
117  virtual unsigned int get_uint(const char *path) = 0;
118  virtual int get_int(const char *path) = 0;
119  virtual bool get_bool(const char *path) = 0;
120  virtual std::string get_string(const char *path) = 0;
121  virtual ValueIterator * get_value(const char *path) = 0;
122  virtual std::string get_type(const char *path) = 0;
123  virtual std::string get_comment(const char *path) = 0;
124  virtual std::string get_default_comment(const char *path) = 0;
125 
126  virtual void set_float(const char *path, float f) = 0;
127  virtual void set_uint(const char *path, unsigned int uint) = 0;
128  virtual void set_int(const char *path, int i) = 0;
129  virtual void set_bool(const char *path, bool b) = 0;
130  virtual void set_string(const char *path, std::string &s) = 0;
131  virtual void set_string(const char *path, const char *s) = 0;
132  virtual void set_comment(const char *path,
133  const char *comment) = 0;
134  virtual void set_comment(const char *path,
135  std::string &comment) = 0;
136 
137  virtual void erase(const char *path) = 0;
138 
139  virtual void set_default_float(const char *path, float f) = 0;
140  virtual void set_default_uint(const char *path,
141  unsigned int uint) = 0;
142  virtual void set_default_int(const char *path, int i) = 0;
143  virtual void set_default_bool(const char *path, bool b) = 0;
144  virtual void set_default_string(const char *path,
145  std::string &s) = 0;
146  virtual void set_default_string(const char *path,
147  const char *s) = 0;
148 
149  virtual void set_default_comment(const char *path,
150  const char *comment) = 0;
151  virtual void set_default_comment(const char *path,
152  std::string &comment) = 0;
153 
154  virtual void erase_default(const char *path) = 0;
155 
156  virtual ValueIterator * iterator() = 0;
157  virtual ValueIterator * iterator_default() = 0;
158  virtual ValueIterator * iterator_hostspecific() = 0;
159 
160  virtual ValueIterator * search(const char *path) = 0;
161 
162  virtual void lock() = 0;
163  virtual bool try_lock() = 0;
164  virtual void unlock() = 0;
165 
166  protected:
167  /** List that contains pointers to ConfigurationChangeHandler */
168  typedef std::list<ConfigurationChangeHandler *> ChangeHandlerList;
169 
170  /** Multimap string to config change handlers. */
171  typedef std::multimap<const char *, ConfigurationChangeHandler *, StringLess >
173 
174  /** Config change handler multimap range. */
175  typedef std::pair<ChangeHandlerMultimap::iterator,
176  ChangeHandlerMultimap::iterator>
178 
179  /** Registered change handlers. */
181  /** Change handler range. */
183 
184  ChangeHandlerList * find_handlers(const char *path);
185  void notify_handlers(const char *path, bool comment_changed = false);
186 
187 };
188 
189 } // end namespace fawkes
190 
191 #endif
virtual std::string get_comment() const =0
Get comment of value.
ChangeHandlerMultimapRange _ch_range
Change handler range.
Definition: config.h:182
virtual std::string get_default_comment(const char *path)=0
Get comment of value at given path.
virtual ValueIterator * iterator()=0
Iterator for all values.
virtual void set_default_float(const char *path, float f)=0
Set new default value in configuration of type float.
virtual bool is_string(const char *path)=0
Check if a value is of type string.
virtual void set_default_int(const char *path, int i)=0
Set new default value in configuration of type int.
virtual void load(const char *name, const char *defaults_name, const char *tag=NULL)=0
Load configuration.
ChangeHandlerMultimap _change_handlers
Registered change handlers.
Definition: config.h:180
virtual void set_default_comment(const char *path, const char *comment)=0
Set new default comment for existing default configuration value.
virtual const char * type() const =0
Type of value.
virtual bool is_bool() const =0
Check if current value is a bool.
virtual bool is_default(const char *path)=0
Check if a value was read from the default config.
virtual std::string get_type(const char *path)=0
Get type of value at given path.
Fawkes library namespace.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
Interface for configuration change handling.
Thrown if config could not be opened.
Definition: config.h:57
Thrown if a config entry could not be found.
Definition: config.h:44
virtual ValueIterator * search(const char *path)=0
Iterator with search results.
virtual bool next()=0
Check if there is another element and advance to this if possible.
virtual float get_float() const =0
Get float value.
virtual void set_int(const char *path, int i)=0
Set new value in configuration of type int.
virtual unsigned int get_uint() const =0
Get unsigned int value.
std::multimap< const char *, ConfigurationChangeHandler *, StringLess > ChangeHandlerMultimap
Multimap string to config change handlers.
Definition: config.h:172
virtual ValueIterator * iterator_hostspecific()=0
Iterator for all host-specific values.
virtual bool is_float() const =0
Check if current value is a float.
virtual int get_int(const char *path)=0
Get value from configuration which is of type int.
virtual void set_bool(const char *path, bool b)=0
Set new value in configuration of type bool.
virtual bool is_int() const =0
Check if current value is a int.
virtual bool get_bool() const =0
Get bool value.
virtual void set_float(const char *path, float f)=0
Set new value in configuration of type float.
virtual int get_int() const =0
Get int value.
std::pair< ChangeHandlerMultimap::iterator, ChangeHandlerMultimap::iterator > ChangeHandlerMultimapRange
Config change handler multimap range.
Definition: config.h:177
virtual ~ValueIterator()
Virtual emptry destructor.
Definition: config.h:71
virtual bool is_bool(const char *path)=0
Check if a value is of type bool.
virtual std::string get_as_string() const =0
Get value as string.
virtual bool is_string() const =0
Check if current value is a string.
virtual void erase_default(const char *path)=0
Erase the given default value from the configuration.
virtual void erase(const char *path)=0
Erase the given value from the configuration.
virtual void set_default_bool(const char *path, bool b)=0
Set new default value in configuration of type bool.
Base class for exceptions in Fawkes.
Definition: exception.h:36
ChangeHandlerList * find_handlers(const char *path)
Find handlers for given path.
Definition: config.cpp:525
virtual void set_default_uint(const char *path, unsigned int uint)=0
Set new default value in configuration of type unsigned int.
ConfigTypeMismatchException(const char *path, const char *actual, const char *requested)
Constructor.
Definition: config.cpp:360
Thrown if there a type problem was detected for example if you tried to query a float with get_int()...
Definition: config.h:50
virtual void rem_change_handler(ConfigurationChangeHandler *h)
Remove a configuration change handler.
Definition: config.cpp:496
virtual ~Configuration()
Virtual empty destructor.
Definition: config.h:66
virtual std::list< std::string > tags()=0
List of tags.
virtual bool is_uint() const =0
Check if current value is a unsigned int.
virtual void tag(const char *tag)=0
Tag this configuration version.
virtual std::string get_string() const =0
Get string value.
Generic configuration exception.
Definition: config.h:37
virtual const char * path() const =0
Path of value.
virtual bool valid() const =0
Check if the current element is valid.
ConfigurationException(const char *msg)
Constructor.
Definition: config.cpp:318
virtual bool is_float(const char *path)=0
Check if a value is of type float.
virtual void unlock()=0
Unlock the config.
virtual ValueIterator * iterator_default()=0
Iterator for all default values.
virtual bool is_uint(const char *path)=0
Check if a value is of type unsigned int.
void notify_handlers(const char *path, bool comment_changed=false)
Notify handlers for given path.
Definition: config.cpp:547
CouldNotOpenConfigException(const char *format,...)
Constructor.
Definition: config.cpp:377
virtual ValueIterator * get_value(const char *path)=0
Get value from configuration.
Iterator interface to iterate over config values.
Definition: config.h:68
virtual bool is_default() const =0
Check if current value was read from the default config.
virtual void copy(Configuration *copyconf)=0
Copy all values from the given configuration.
virtual void add_change_handler(ConfigurationChangeHandler *h)
Add a configuration change handler.
Definition: config.cpp:479
virtual bool exists(const char *path)=0
Check if a given value exists.
virtual unsigned int get_uint(const char *path)=0
Get value from configuration which is of type unsigned int.
virtual void set_default_string(const char *path, std::string &s)=0
Set new default value in configuration of type string.
virtual bool try_lock()=0
Try to lock the config.
virtual bool is_int(const char *path)=0
Check if a value is of type int.
virtual std::string get_comment(const char *path)=0
Get comment of value at given path.
Interface for configuration handling.
Definition: config.h:63
virtual void set_comment(const char *path, const char *comment)=0
Set new comment for existing value.
virtual float get_float(const char *path)=0
Get value from configuration which is of type float.
virtual void set_uint(const char *path, unsigned int uint)=0
Set new value in configuration of type unsigned int.
virtual void set_string(const char *path, std::string &s)=0
Set new value in configuration of type string.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
virtual void lock()=0
Lock the config.
ConfigEntryNotFoundException(const char *path)
Constructor.
Definition: config.cpp:344
std::list< ConfigurationChangeHandler * > ChangeHandlerList
List that contains pointers to ConfigurationChangeHandler.
Definition: config.h:168