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 #include <vector>
33 
34 namespace fawkes {
35 #if 0 /* just to make Emacs auto-indent happy */
36 }
37 #endif
38 
39 class ConfigurationChangeHandler;
40 
42 {
43  public:
44  ConfigurationException(const char *msg);
45  ConfigurationException(const char *prefix, const char *msg);
46 };
47 
49 {
50  public:
51  ConfigEntryNotFoundException(const char *path);
52 };
53 
55 {
56  public:
57  ConfigTypeMismatchException(const char *path,
58  const char *actual, const char *requested);
59 };
60 
62 {
63  public:
64  CouldNotOpenConfigException(const char *format, ...);
65 };
66 
68 {
69  public:
70  virtual ~Configuration() {}
71 
73  {
74  public:
75  virtual ~ValueIterator() {}
76  virtual bool next() = 0;
77  virtual bool valid() const = 0;
78 
79  virtual const char * path() const = 0;
80  virtual const char * type() const = 0;
81 
82  virtual bool is_float() const = 0;
83  virtual bool is_uint() const = 0;
84  virtual bool is_int() const = 0;
85  virtual bool is_bool() const = 0;
86  virtual bool is_string() const = 0;
87  virtual bool is_list() const = 0;
88  virtual size_t get_list_size() const = 0;
89 
90  virtual float get_float() const = 0;
91  virtual unsigned int get_uint() const = 0;
92  virtual int get_int() const = 0;
93  virtual bool get_bool() const = 0;
94  virtual std::string get_string() const = 0;
95  virtual std::vector<float> get_floats() const = 0;
96  virtual std::vector<unsigned int> get_uints() const = 0;
97  virtual std::vector<int> get_ints() const = 0;
98  virtual std::vector<bool> get_bools() const = 0;
99  virtual std::vector<std::string> get_strings() const = 0;
100  virtual std::string get_as_string() const = 0;
101 
102  virtual std::string get_comment() const = 0;
103 
104  virtual bool is_default() const = 0;
105  };
106 
107  virtual void copy(Configuration *copyconf) = 0;
108 
109  virtual void add_change_handler(ConfigurationChangeHandler *h);
110  virtual void rem_change_handler(ConfigurationChangeHandler *h);
111 
112  virtual void load(const char *file_path) = 0;
113 
114  virtual bool exists(const char *path) = 0;
115  virtual bool is_float(const char *path) = 0;
116  virtual bool is_uint(const char *path) = 0;
117  virtual bool is_int(const char *path) = 0;
118  virtual bool is_bool(const char *path) = 0;
119  virtual bool is_string(const char *path) = 0;
120  virtual bool is_list(const char *path) = 0;
121 
122  virtual bool is_default(const char *path) = 0;
123 
124  virtual float get_float(const char *path) = 0;
125  virtual unsigned int get_uint(const char *path) = 0;
126  virtual int get_int(const char *path) = 0;
127  virtual bool get_bool(const char *path) = 0;
128  virtual std::string get_string(const char *path) = 0;
129  virtual std::vector<float> get_floats(const char *path) = 0;
130  virtual std::vector<unsigned int> get_uints(const char *path) = 0;
131  virtual std::vector<int> get_ints(const char *path) = 0;
132  virtual std::vector<bool> get_bools(const char *path) = 0;
133  virtual std::vector<std::string> get_strings(const char *path) = 0;
134  virtual ValueIterator * get_value(const char *path) = 0;
135  virtual std::string get_type(const char *path) = 0;
136  virtual std::string get_comment(const char *path) = 0;
137  virtual std::string get_default_comment(const char *path) = 0;
138 
139  virtual void set_float(const char *path, float f) = 0;
140  virtual void set_uint(const char *path, unsigned int uint) = 0;
141  virtual void set_int(const char *path, int i) = 0;
142  virtual void set_bool(const char *path, bool b) = 0;
143  virtual void set_string(const char *path, std::string &s) = 0;
144  virtual void set_string(const char *path, const char *s) = 0;
145  virtual void set_floats(const char *path, std::vector<float> &f) = 0;
146  virtual void set_uints(const char *path, std::vector<unsigned int> &uint) = 0;
147  virtual void set_ints(const char *path, std::vector<int> &i) = 0;
148  virtual void set_bools(const char *path, std::vector<bool> &b) = 0;
149  virtual void set_strings(const char *path, std::vector<std::string> &s) = 0;
150  virtual void set_strings(const char *path, std::vector<const char *> &s) = 0;
151  virtual void set_comment(const char *path,
152  const char *comment) = 0;
153  virtual void set_comment(const char *path,
154  std::string &comment) = 0;
155 
156  virtual void erase(const char *path) = 0;
157 
158  virtual void set_default_float(const char *path, float f) = 0;
159  virtual void set_default_uint(const char *path,
160  unsigned int uint) = 0;
161  virtual void set_default_int(const char *path, int i) = 0;
162  virtual void set_default_bool(const char *path, bool b) = 0;
163  virtual void set_default_string(const char *path,
164  std::string &s) = 0;
165  virtual void set_default_string(const char *path,
166  const char *s) = 0;
167 
168  virtual void set_default_comment(const char *path,
169  const char *comment) = 0;
170  virtual void set_default_comment(const char *path,
171  std::string &comment) = 0;
172 
173  virtual void erase_default(const char *path) = 0;
174 
175  virtual ValueIterator * iterator() = 0;
176 
177  virtual ValueIterator * search(const char *path) = 0;
178 
179  virtual void lock() = 0;
180  virtual bool try_lock() = 0;
181  virtual void unlock() = 0;
182 
183  virtual void try_dump() = 0;
184 
185  /// @cond CONVENIENCE_METHODS
186  virtual bool exists(const std::string &path)
187  { return exists(path.c_str()); }
188 
189  virtual bool is_float(const std::string &path) { return is_float(path.c_str()); }
190  virtual bool is_uint(const std::string &path) { return is_uint(path.c_str()); }
191  virtual bool is_int(const std::string &path) { return is_int(path.c_str()); }
192  virtual bool is_bool(const std::string &path) { return is_bool(path.c_str()); }
193  virtual bool is_string(const std::string &path) { return is_string(path.c_str()); }
194  virtual bool is_list(const std::string &path) { return is_list(path.c_str()); }
195 
196  virtual bool is_default(const std::string &path) { return is_default(path.c_str()); }
197 
198  virtual float get_float(const std::string &path) { return get_float(path.c_str()); }
199  virtual unsigned int get_uint(const std::string &path) { return get_uint(path.c_str()); }
200  virtual int get_int(const std::string &path) { return get_int(path.c_str()); }
201  virtual bool get_bool(const std::string &path) { return get_bool(path.c_str()); }
202  virtual std::string get_string(const std::string &path)
203  { return get_string(path.c_str()); }
204  virtual std::vector<float> get_floats(const std::string &path)
205  { return get_floats(path.c_str()); }
206  virtual std::vector<unsigned int> get_uints(const std::string &path)
207  { return get_uints(path.c_str()); }
208  virtual std::vector<int> get_ints(const std::string &path)
209  { return get_ints(path.c_str()); }
210  virtual std::vector<bool> get_bools(const std::string &path)
211  { return get_bools(path.c_str()); }
212  virtual std::vector<std::string> get_strings(const std::string &path)
213  { return get_strings(path.c_str()); }
214  virtual ValueIterator * get_value(const std::string &path)
215  { return get_value(path.c_str()); }
216  virtual std::string get_type(const std::string &path)
217  { return get_type(path.c_str()); }
218  virtual std::string get_comment(const std::string &path)
219  { return get_comment(path.c_str()); }
220  virtual std::string get_default_comment(const std::string &path)
221  { return get_default_comment(path.c_str()); }
222 
223  virtual void set_float(const std::string &path, float f)
224  { set_float(path.c_str(), f); }
225  virtual void set_uint(const std::string &path, unsigned int uint)
226  { set_uint(path.c_str(), uint); }
227  virtual void set_int(const std::string &path, int i)
228  { set_int(path.c_str(), i); }
229  virtual void set_bool(const std::string &path, bool b)
230  { set_bool(path.c_str(), b); }
231  virtual void set_string(const std::string &path, std::string &s)
232  { set_string(path.c_str(), s); }
233  virtual void set_string(const std::string &path, const char *s)
234  { set_string(path.c_str(), s); }
235  virtual void set_floats(const std::string &path, std::vector<float> &f)
236  { set_floats(path.c_str(), f); }
237  virtual void set_uints(const std::string &path, std::vector<unsigned int> &uint)
238  { set_uints(path.c_str(), uint); }
239  virtual void set_ints(const std::string &path, std::vector<int> &i)
240  { set_ints(path.c_str(), i); }
241  virtual void set_bools(const std::string &path, std::vector<bool> &b)
242  { set_bools(path.c_str(), b); }
243  virtual void set_strings(const std::string &path, std::vector<std::string> &s)
244  { set_strings(path.c_str(), s); }
245  virtual void set_strings(const std::string &path, std::vector<const char *> &s)
246  { set_strings(path.c_str(), s); }
247  virtual void set_comment(const std::string &path, const char *comment)
248  { set_comment(path.c_str(), comment); }
249  virtual void set_comment(const std::string &path, std::string &comment)
250  { set_comment(path.c_str(), comment); }
251 
252  virtual void erase(const std::string &path) { erase(path.c_str()); }
253 
254  virtual void set_default_float(const std::string &path, float f)
255  { set_default_float(path.c_str(), f); }
256  virtual void set_default_uint(const std::string &path, unsigned int uint)
257  { set_default_uint(path.c_str(), uint); }
258  virtual void set_default_int(const std::string &path, int i)
259  { set_default_int(path.c_str(), i); }
260  virtual void set_default_bool(const std::string &path, bool b)
261  { set_default_bool(path.c_str(), b); }
262  virtual void set_default_string(const std::string &path, std::string &s)
263  { set_default_string(path.c_str(), s); }
264  virtual void set_default_string(const std::string &path, const char *s)
265  { set_default_string(path.c_str(), s); }
266 
267  virtual void set_default_comment(const std::string &path,
268  const char *comment)
269  { set_default_comment(path.c_str(), comment); }
270  virtual void set_default_comment(const std::string &path,
271  std::string &comment)
272  { set_default_comment(path.c_str(), comment); }
273 
274  virtual void erase_default(const std::string &path)
275  { erase_default(path.c_str()); }
276 
277  virtual ValueIterator * search(const std::string &path)
278  { return search(path.c_str()); }
279  /// @endcond
280 
281  protected:
282  /** List that contains pointers to ConfigurationChangeHandler */
283  typedef std::list<ConfigurationChangeHandler *> ChangeHandlerList;
284 
285  /** Multimap string to config change handlers. */
286  typedef std::multimap<const char *, ConfigurationChangeHandler *, StringLess >
288 
289  /** Config change handler multimap range. */
290  typedef std::pair<ChangeHandlerMultimap::iterator,
291  ChangeHandlerMultimap::iterator>
293 
294  /** Registered change handlers. */
296  /** Change handler range. */
298 
299  ChangeHandlerList * find_handlers(const char *path);
300  void notify_handlers(const char *path, bool comment_changed = false);
301 
302 };
303 
304 } // end namespace fawkes
305 
306 #endif
ChangeHandlerMultimapRange _ch_range
Change handler range.
Definition: config.h:297
ChangeHandlerMultimap _change_handlers
Registered change handlers.
Definition: config.h:295
This class tries to translate the found plan to interpreteable data for the rest of the program...
Fawkes library namespace.
Interface for configuration change handling.
Thrown if config could not be opened.
Definition: config.h:61
Thrown if a config entry could not be found.
Definition: config.h:48
std::multimap< const char *, ConfigurationChangeHandler *, StringLess > ChangeHandlerMultimap
Multimap string to config change handlers.
Definition: config.h:287
std::pair< ChangeHandlerMultimap::iterator, ChangeHandlerMultimap::iterator > ChangeHandlerMultimapRange
Config change handler multimap range.
Definition: config.h:292
virtual ~ValueIterator()
Virtual emptry destructor.
Definition: config.h:75
Base class for exceptions in Fawkes.
Definition: exception.h:36
Thrown if there a type problem was detected for example if you tried to query a float with get_int()...
Definition: config.h:54
virtual ~Configuration()
Virtual empty destructor.
Definition: config.h:70
Generic configuration exception.
Definition: config.h:41
ConfigurationException(const char *msg)
Constructor.
Definition: config.cpp:357
Iterator interface to iterate over config values.
Definition: config.h:72
Interface for configuration handling.
Definition: config.h:67
std::list< ConfigurationChangeHandler * > ChangeHandlerList
List that contains pointers to ConfigurationChangeHandler.
Definition: config.h:283