Fawkes API Fawkes Development Version
config.cpp
1
2/***************************************************************************
3 * config.cpp - Fawkes configuration interface
4 *
5 * Created: Mon Dec 18 14:54:23 2006
6 * Copyright 2006-2008 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#include <config/change_handler.h>
25#include <config/config.h>
26
27#include <cstring>
28
29namespace fawkes {
30
31/** @class Configuration <config/config.h>
32 * Interface for configuration handling.
33 * We know that half of robotics is about parameter tuning. The Configuration
34 * interface defines a unified way of storing parameters and other
35 * configuration options no matter of how the database is implemented.
36 * This is mainly done to allow for testing different solutions for ticket #10.
37 *
38 * @fn Configuration::~Configuration()
39 * Virtual empty destructor.
40 *
41 * @fn void Configuration::load(const char *file_path)
42 * Load configuration.
43 * Loads configuration data, or opens a file, depending on the implementation. After
44 * this call access to all other methods shall be possible.
45 * @param file_path path of the configuration file.
46 *
47 *
48 * @fn void Configuration::copy(Configuration *copyconf)
49 * Copies all values from the given configuration.
50 * All values from the given configuration are copied. Old values are not erased
51 * so that the copied values will overwrite existing values, new values are
52 * created, but values existent in current config but not in the copied config
53 * will remain unchanged.
54 * @param copyconf configuration to copy
55 *
56 * @fn bool Configuration::exists(const char *path)
57 * Check if a given value exists.
58 * @param path path to value
59 * @return true if the value exists, false otherwise
60 *
61 * @fn bool Configuration::is_float(const char *path)
62 * Check if a value is of type float
63 * @param path path to value
64 * @return true if the value exists and is of type float
65 *
66 * @fn bool Configuration::is_uint(const char *path)
67 * Check if a value is of type unsigned int
68 * @param path path to value
69 * @return true if the value exists and is of type unsigned int
70 *
71 * @fn bool Configuration::is_int(const char *path)
72 * Check if a value is of type int
73 * @param path path to value
74 * @return true if the value exists and is of type int
75 *
76 * @fn bool Configuration::is_bool(const char *path)
77 * Check if a value is of type bool
78 * @param path path to value
79 * @return true if the value exists and is of type bool
80 *
81 * @fn bool Configuration::is_string(const char *path)
82 * Check if a value is of type string
83 * @param path path to value
84 * @return true if the value exists and is of type string
85 *
86 * @fn bool Configuration::is_list(const char *path)
87 * Check if a value is a list.
88 * @param path path to value
89 * @return true if the value exists and is a list
90 *
91 * @fn bool Configuration::is_default(const char *path)
92 * Check if a value was read from the default config.
93 * @param path path to value
94 * @return true if the value exists and is only stored in the default config
95 *
96 * @fn float Configuration::get_float(const char *path)
97 * Get value from configuration which is of type float
98 * @param path path to value
99 * @return value
100 *
101 * @fn unsigned int Configuration::get_uint(const char *path)
102 * Get value from configuration which is of type unsigned int
103 * @param path path to value
104 * @return value
105 *
106 * @fn int Configuration::get_int(const char *path)
107 * Get value from configuration which is of type int
108 * @param path path to value
109 * @return value
110 *
111 * @fn bool Configuration::get_bool(const char *path)
112 * Get value from configuration which is of type bool
113 * @param path path to value
114 * @return value
115 *
116 * @fn std::string Configuration::get_string(const char *path)
117 * Get value from configuration which is of type string
118 * @param path path to value
119 * @return value
120 *
121 * @fn std::vector<float> Configuration::get_floats(const char *path)
122 * Get list of values from configuration which is of type float
123 * @param path path to value
124 * @return value
125 *
126 * @fn std::vector<unsigned int> Configuration::get_uints(const char *path)
127 * Get list of values from configuration which is of type unsigned int
128 * @param path path to value
129 * @return value
130 *
131 * @fn std::vector<int> Configuration::get_ints(const char *path)
132 * Get list of values from configuration which is of type int
133 * @param path path to value
134 * @return value
135 *
136 * @fn std::vector<bool> Configuration::get_bools(const char *path)
137 * Get list of values from configuration which is of type bool
138 * @param path path to value
139 * @return value
140 *
141 * @fn std::vector<std::string> Configuration::get_strings(const char *path)
142 * Get list of values from configuration which is of type string
143 * @param path path to value
144 * @return value
145 *
146 * @fn float Configuration::get_float_or_default(const char *path, const float &default_val)
147 * Get value from configuration which is of type float, or the given default if
148 * the path does not exist
149 * @param path path to value
150 * @param default_val the default value
151 * @return value
152 *
153 * @fn unsigned int Configuration::get_uint_or_default(const char *path, const unsigned int &default_val)
154 * Get value from configuration which is of type unsigned int, or the given
155 * default if the path does not exist
156 * @param path path to value
157 * @param default_val the default value
158 * @return value
159 *
160 * @fn int Configuration::get_int_or_default(const char *path, const int &default_val)
161 * Get value from configuration which is of type int, or the given default if
162 * the path does not exist
163 * @param path path to value
164 * @param default_val the default value
165 * @return value
166 *
167 * @fn bool Configuration::get_bool_or_default(const char *path, const bool &default_val)
168 * Get value from configuration which is of type bool, or the given default if
169 * the path does not exist
170 * @param path path to value
171 * @param default_val the default value
172 * @return value
173 *
174 * @fn string Configuration::get_string_or_default(const char *path, const string &default_val)
175 * Get value from configuration which is of type string, or the given default if
176 * the path does not exist
177 * @param path path to value
178 * @param default_val the default value
179 * @return value
180 *
181 * @fn std::vector<float> Configuration::get_floats_or_defaults(const char *path, const std::vector<float> &default_val)
182 * Get list of values from configuration which is of type float, or the given
183 * default if the path does not exist
184 * @param path path to value
185 * @param default_val the default value
186 * @return value
187 *
188 * @fn std::vector<unsigned int> Configuration::get_uints_or_defaults(const char *path, const std::vector<unsigned int> &default_val)
189 * Get list of values from configuration which is of type unsigned int, or the given
190 * default if the path does not exist
191 * @param path path to value
192 * @param default_val the default value
193 * @return value
194 *
195 * @fn std::vector<int> Configuration::get_ints_or_defaults(const char *path, const std::vector<int> &default_val)
196 * Get list of values from configuration which is of type int, or the given
197 * default if the path does not exist
198 * @param path path to value
199 * @param default_val the default value
200 * @return value
201 *
202 * @fn std::vector<bool> Configuration::get_bools_or_defaults(const char *path, const std::vector<bool> &default_val)
203 * Get list of values from configuration which is of type bool, or the given
204 * default if the path does not exist
205 * @param path path to value
206 * @param default_val the default value
207 * @return value
208 *
209 * @fn std::vector<string> Configuration::get_strings_or_defaults(const char *path, const std::vector<string> &default_val)
210 * Get list of values from configuration which is of type string, or the given
211 * default if the path does not exist
212 * @param path path to value
213 * @param default_val the default value
214 * @return value
215 *
216 * @fn Configuration::ValueIterator * Configuration::get_value(const char *path)
217 * Get value from configuration.
218 * @param path path to value
219 * @return value iterator for just this one value, maybe invalid if value does not
220 * exists.
221 *
222 * @fn std::string Configuration::get_type(const char *path)
223 * Get type of value at given path.
224 * @param path path to value
225 * @return string representation of type, one of float, unsigned int, int, bool,
226 * or string
227 * @exception ConfigurationException shall be thrown if value does not exist or
228 * on any other error.
229 *
230 * @fn std::string Configuration::get_comment(const char *path)
231 * Get comment of value at given path.
232 * The value at the given path must exist in the host-specific configuration.
233 * @param path path to value
234 * @return comment
235 * @exception ConfigEntryNotFoundException shall be thrown if value does not exist
236 * @exception ConfigurationException shall be thrown on any other error
237 *
238 * @fn std::string Configuration::get_default_comment(const char *path)
239 * Get comment of value at given path.
240 * The value at the given path must exist in the default configuration.
241 * @param path path to value
242 * @return comment
243 * @exception ConfigEntryNotFoundException shall be thrown if value does not exist
244 * @exception ConfigurationException shall be thrown on any other error
245 *
246 *
247 * @fn void Configuration::set_float(const char *path, float f)
248 * Set new value in configuration of type float
249 * @param path path to value
250 * @param f new float value
251 *
252 * @fn void Configuration::set_uint(const char *path, unsigned int uint)
253 * Set new value in configuration of type unsigned int
254 * @param path path to value
255 * @param uint new unsigned int value
256 *
257 * @fn void Configuration::set_int(const char *path, int i)
258 * Set new value in configuration of type int
259 * @param path path to value
260 * @param i new int value
261 *
262 * @fn void Configuration::set_bool(const char *path, bool b)
263 * Set new value in configuration of type bool
264 * @param path path to value
265 * @param b new bool value
266 *
267 * @fn void Configuration::set_string(const char *path, std::string &s)
268 * Set new value in configuration of type string
269 * @param path path to value
270 * @param s new string value
271 *
272 * @fn void Configuration::set_string(const char *path, const char *s)
273 * Set new value in configuration of type string. Works like the aforementioned method.
274 * Just takes an good ol' char array instead of a std::string.
275 * @param path path to value
276 * @param s new string value
277 *
278 * @fn void Configuration::set_floats(const char *path, std::vector<float> &f)
279 * Set new value in configuration of type float
280 * @param path path to value
281 * @param f new float values
282 *
283 * @fn void Configuration::set_uints(const char *path, std::vector<unsigned int> &uint)
284 * Set new value in configuration of type unsigned int
285 * @param path path to value
286 * @param uint new unsigned int values
287 *
288 * @fn void Configuration::set_ints(const char *path, std::vector<int> &i)
289 * Set new value in configuration of type int
290 * @param path path to value
291 * @param i new int values
292 *
293 * @fn void Configuration::set_bools(const char *path, std::vector<bool> &b)
294 * Set new value in configuration of type bool
295 * @param path path to value
296 * @param b new bool values
297 *
298 * @fn void Configuration::set_strings(const char *path, std::vector<std::string> &s)
299 * Set new value in configuration of type string
300 * @param path path to value
301 * @param s new string values
302 *
303 * @fn void Configuration::set_strings(const char *path, std::vector<const char *> &s)
304 * Set new value in configuration of type string. Works like the aforementioned method.
305 * Just takes an good ol' char array instead of a std::string.
306 * @param path path to value
307 * @param s new string values
308
309 *
310 * @fn void Configuration::set_comment(const char *path, std::string &comment)
311 * Set new comment for existing value.
312 * @param path path to value
313 * @param comment new comment string
314 *
315 * @fn void Configuration::set_comment(const char *path, const char *comment)
316 * Set new comment for existing value. Works like the aforementioned method.
317 * Just takes an good ol' char array instead of a std::string.
318 * @param path path to value
319 * @param comment new comment string
320 *
321 * @fn void Configuration::erase(const char *path)
322 * Erase the given value from the configuration. It is not an error if the value does
323 * not exists before deletion.
324 * @param path path to value
325 *
326 * @fn void Configuration::set_default_float(const char *path, float f)
327 * Set new default value in configuration of type float
328 * @param path path to value
329 * @param f new float value
330 *
331 * @fn void Configuration::set_default_uint(const char *path, unsigned int uint)
332 * Set new default value in configuration of type unsigned int
333 * @param path path to value
334 * @param uint new unsigned int value
335 *
336 * @fn void Configuration::set_default_int(const char *path, int i)
337 * Set new default value in configuration of type int
338 * @param path path to value
339 * @param i new int value
340 *
341 * @fn void Configuration::set_default_bool(const char *path, bool b)
342 * Set new default value in configuration of type bool
343 * @param path path to value
344 * @param b new bool value
345 *
346 * @fn void Configuration::set_default_string(const char *path, std::string &s)
347 * Set new default value in configuration of type string
348 * @param path path to value
349 * @param s new string value
350 *
351 * @fn void Configuration::set_default_string(const char *path, const char *s)
352 * Set new default value in configuration of type string. Works like the aforementioned method.
353 * Just takes an good ol' char array instead of a std::string.
354 * @param path path to value
355 * @param s new string value
356 *
357 * @fn void Configuration::set_default_comment(const char *path, std::string &comment)
358 * Set new default comment for existing default configuration value.
359 * @param path path to value
360 * @param comment new comment string
361 *
362 * @fn void Configuration::set_default_comment(const char *path, const char *comment)
363 * Set new default comment for existing default configuration value.
364 * Works like the aforementioned method. Just takes an good ol' char array
365 * instead of a std::string.
366 * @param path path to value
367 * @param comment new comment string
368 *
369 * @fn void Configuration::erase_default(const char *path)
370 * Erase the given default value from the configuration. It is not an error if the value does
371 * not exists before deletion.
372 * @param path path to value
373 *
374 * @fn Configuration::ValueIterator * Configuration::iterator()
375 * Iterator for all values.
376 * Returns an iterator that can be used to iterate over all values in the current
377 * configuration, it will value the overlay. If a default and a host-specific value
378 * exists you will only see the host-specific value.
379 * @return iterator over all values
380 *
381 * @fn Configuration::ValueIterator * Configuration::search(const char *path)
382 * Iterator with search results.
383 * Returns an iterator that can be used to iterate over the search results. All values
384 * whose path start with the given strings are returned.
385 * A call like
386 * @code
387 * config->search("");
388 * @endcode
389 * is effectively the same as a call to iterator().
390 * @param path start of path
391 * @return iterator to search results
392 *
393 * @fn void Configuration::lock()
394 * Lock the config.
395 * No further changes or queries can be executed on the configuration and will block until
396 * the config is unlocked.
397 *
398 * @fn bool Configuration::try_lock()
399 * Try to lock the config.
400 * @see Configuration::lock()
401 * @return true, if the lock has been aquired, false otherwise
402 *
403 * @fn void Configuration::unlock()
404 * Unlock the config.
405 * Modifications and queries are possible again.
406 *
407 * @fn void Configuration::try_dump()
408 * Try to dump configuration.
409 * For configuration methods that transform configuration files in a binary
410 * format this can be used to write out the text representation on shutdown
411 * of Fawkes.
412 * @exception Exception thrown if dumping fails
413 *
414 */
415
416/** @class ConfigurationException config/config.h
417 * Generic configuration exception.
418 * Thrown if there is no other matching exception.
419 */
420
421/** Constructor.
422 * @param msg message
423 */
425{
426}
427
428/** Constructor.
429 * @param prefix Put as "prefix: " before the message, can be used to have a prefix
430 * and put an error message from another API into msg.
431 * @param msg message
432 */
433ConfigurationException::ConfigurationException(const char *prefix, const char *msg) : Exception()
434{
435 append("%s: %s", prefix, msg);
436}
437
438/** @class ConfigEntryNotFoundException config/config.h
439 * Thrown if a config entry could not be found.
440 */
441
442/** Constructor.
443 * @param path path of value
444 */
446: Exception("Config value for '%s' not found", path)
447{
448}
449
450/** @class ConfigTypeMismatchException config/config.h
451 * Thrown if there a type problem was detected for example if you tried
452 * to query a float with get_int().
453 */
454
455/** Constructor.
456 * @param path path of value
457 * @param actual actual type
458 * @param requested requested type
459 */
461 const char *actual,
462 const char *requested)
463: Exception()
464{
465 append("Config value for '%s' is not of type '%s', but of type '%s'", path, requested, actual);
466}
467
468/** @class CouldNotOpenConfigException <config/config.h>
469 * Thrown if config could not be opened.
470 * This is most likely to happen during the constructor or load().
471 */
472
473/** Constructor.
474 * @param format format of message to describe cause or symptom of failure
475 */
477{
478 va_list va;
479 va_start(va, format);
480 append_va(format, va);
481 va_end(va);
482}
483
484/** @class Configuration::ValueIterator <config/config.h>
485 * Iterator interface to iterate over config values. This does not implement a
486 * classic iterator interface with begin and end nodes but rather mimics a more
487 * Java-like interface where you iterate over the entries in a while loop until
488 * you covered all entries (much like a queue).
489 * If you implement this for your own configuration system you should not make
490 * the constructor publically accessible.
491 *
492 * @fn Configuration::ValueIterator::~ValueIterator()
493 * Virtual emptry destructor.
494 *
495 * @fn bool Configuration::ValueIterator::next()
496 * Check if there is another element and advance to this if possible.
497 * This advances to the next element, if there is one.
498 * @return true, if another element has been reached, false otherwise
499 *
500 * @fn bool Configuration::ValueIterator::valid() const
501 * Check if the current element is valid.
502 * This is much like the classic end element for iterators. If the iterator is
503 * invalid there all subsequent calls to next() shall fail.
504 * @return true, if the iterator is still valid, false otherwise
505 *
506 * @fn const char * Configuration::ValueIterator::path() const
507 * Path of value.
508 * @return path of value
509 *
510 * @fn const char * Configuration::ValueIterator::type() const
511 * Type of value.
512 * @return string representation of value type.
513 *
514 * @fn bool Configuration::ValueIterator::is_float() const
515 * Check if current value is a float.
516 * @return true, if value is a float, false otherwise
517 *
518 * @fn bool Configuration::ValueIterator::is_uint() const
519 * Check if current value is a unsigned int.
520 * @return true, if value is a unsigned int, false otherwise
521 *
522 * @fn bool Configuration::ValueIterator::is_int() const
523 * Check if current value is a int.
524 * @return true, if value is a int, false otherwise
525 *
526 * @fn bool Configuration::ValueIterator::is_bool() const
527 * Check if current value is a bool.
528 * @return true, if value is a bool, false otherwise
529 *
530 * @fn bool Configuration::ValueIterator::is_string() const
531 * Check if current value is a string.
532 * @return true, if value is a string, false otherwise
533 *
534 * @fn bool Configuration::ValueIterator::is_list() const
535 * Check if a value is a list.
536 * @return true if the value exists and is a list
537 *
538 * @fn size_t Configuration::ValueIterator::get_list_size() const
539 * Get number of elements in list value.
540 * @return number of elements in list value
541 * @throw Exception thrown if the element is not a list.
542 *
543 * @fn bool Configuration::ValueIterator::is_default() const
544 * Check if current value was read from the default config.
545 * @return true, if value was read from the default config, false otherwise
546 *
547 * @fn float Configuration::ValueIterator::get_float() const
548 * Get float value.
549 * @return value
550 *
551 * @fn unsigned int Configuration::ValueIterator::get_uint() const
552 * Get unsigned int value.
553 * @return value
554 *
555 * @fn int Configuration::ValueIterator::get_int() const
556 * Get int value.
557 * @return value
558 *
559 * @fn bool Configuration::ValueIterator::get_bool() const
560 * Get bool value.
561 * @return value
562 *
563 * @fn std::string Configuration::ValueIterator::get_string() const
564 * Get string value.
565 * @return value
566 *
567 * @fn std::vector<float> Configuration::ValueIterator::get_floats() const
568 * Get list of values from configuration which is of type float
569 * @return value
570 *
571 * @fn std::vector<unsigned int> Configuration::ValueIterator::get_uints() const
572 * Get list of values from configuration which is of type unsigned int
573 * @return value
574 *
575 * @fn std::vector<int> Configuration::ValueIterator::get_ints() const
576 * Get list of values from configuration which is of type int
577 * @return value
578 *
579 * @fn std::vector<bool> Configuration::ValueIterator::get_bools() const
580 * Get list of values from configuration which is of type bool
581 * @return value
582 *
583 * @fn std::vector<std::string> Configuration::ValueIterator::get_strings() const
584 * Get list of values from configuration which is of type string
585 * @return value
586 *
587 * @fn std::string Configuration::ValueIterator::get_comment() const
588 * Get comment of value.
589 * @return comment
590 *
591 * @fn std::string Configuration::ValueIterator::get_as_string() const
592 * Get value as string.
593 * @return value as string
594 *
595 */
596
597/** Add a configuration change handler.
598 * The added handler is called whenever a value changes and the handler
599 * desires to get notified for the given component.
600 * @param h configuration change handler
601 */
602void
604{
605 const char *c = h->config_monitor_prefix();
606 if (c == NULL) {
607 c = "";
608 }
609
610 _change_handlers.insert(ChangeHandlerMultimap::value_type(c, h));
611}
612
613/** Remove a configuration change handler.
614 * The handler is removed from the change handler list and no longer called on
615 * config changes.
616 * @param h configuration change handler
617 */
618void
620{
621 const char *c = h->config_monitor_prefix();
622 if (c == NULL) {
623 c = "";
624 }
625 bool changed = true;
626 while (changed) {
627 changed = false;
628 for (ChangeHandlerMultimap::const_iterator j = _change_handlers.begin();
629 !changed && (j != _change_handlers.end());
630 ++j) {
631 _ch_range = _change_handlers.equal_range((*j).first);
632 for (ChangeHandlerMultimap::iterator i = _ch_range.first; !changed && (i != _ch_range.second);
633 ++i) {
634 if ((*i).second == h) {
635 _change_handlers.erase(i);
636 changed = true;
637 break;
638 }
639 }
640 if (changed)
641 break;
642 }
643 }
644}
645
646/** Find handlers for given path.
647 * @param path path to get handlers for
648 * @return list with config change handlers.
649 */
652{
654 for (ChangeHandlerMultimap::const_iterator j = _change_handlers.begin();
655 j != _change_handlers.end();
656 ++j) {
657 if (strstr(path, (*j).first) == path) {
658 _ch_range = _change_handlers.equal_range((*j).first);
659 for (ChangeHandlerMultimap::const_iterator i = _ch_range.first; i != _ch_range.second; ++i) {
660 rv->push_back((*i).second);
661 }
662 }
663 }
664
665 return rv;
666}
667
668/** Notify handlers for given path.
669 * @param path path to notify handlers for
670 * @param comment_changed true if the change is about a comment change,
671 * false otherwise
672 */
673void
674Configuration::notify_handlers(const char *path, bool comment_changed)
675{
678 if (value->next()) {
679 for (ChangeHandlerList::const_iterator i = h->begin(); i != h->end(); ++i) {
680 if (comment_changed) {
681 (*i)->config_comment_changed(value);
682 } else {
683 (*i)->config_value_changed(value);
684 }
685 }
686 } else {
687 for (ChangeHandlerList::const_iterator i = h->begin(); i != h->end(); ++i) {
688 (*i)->config_value_erased(path);
689 }
690 }
691 delete value;
692 delete h;
693}
694
695float
696Configuration::get_float_or_default(const char *path, const float &default_val)
697{
698 try {
699 return get_float(path);
700 } catch (ConfigEntryNotFoundException &e) {
701 return default_val;
702 }
703}
704
705unsigned int
706Configuration::get_uint_or_default(const char *path, const unsigned int &default_val)
707{
708 try {
709 return get_uint(path);
710 } catch (ConfigEntryNotFoundException &e) {
711 return default_val;
712 }
713}
714
715int
716Configuration::get_int_or_default(const char *path, const int &default_val)
717{
718 try {
719 return get_int(path);
720 } catch (ConfigEntryNotFoundException &e) {
721 return default_val;
722 }
723}
724
725bool
726Configuration::get_bool_or_default(const char *path, const bool &default_val)
727{
728 try {
729 return get_bool(path);
730 } catch (ConfigEntryNotFoundException &e) {
731 return default_val;
732 }
733}
734
735std::string
736Configuration::get_string_or_default(const char *path, const std::string &default_val)
737{
738 try {
739 return get_string(path);
740 } catch (ConfigEntryNotFoundException &e) {
741 return default_val;
742 }
743}
744
745std::vector<float>
746Configuration::get_floats_or_defaults(const char *path, const std::vector<float> &default_val)
747{
748 try {
749 return get_floats(path);
750 } catch (ConfigEntryNotFoundException &e) {
751 return default_val;
752 }
753}
754
755std::vector<unsigned int>
756Configuration::get_uints_or_defaults(const char *path, const std::vector<unsigned int> &default_val)
757{
758 try {
759 return get_uints(path);
760 } catch (ConfigEntryNotFoundException &e) {
761 return default_val;
762 }
763}
764
765std::vector<int>
766Configuration::get_ints_or_defaults(const char *path, const std::vector<int> &default_val)
767{
768 try {
769 return get_ints(path);
770 } catch (ConfigEntryNotFoundException &e) {
771 return default_val;
772 }
773}
774
775std::vector<bool>
776Configuration::get_bools_or_defaults(const char *path, const std::vector<bool> &default_val)
777{
778 try {
779 return get_bools(path);
780 } catch (ConfigEntryNotFoundException &e) {
781 return default_val;
782 }
783}
784
785std::vector<std::string>
787 const std::vector<std::string> &default_val)
788{
789 try {
790 return get_strings(path);
791 } catch (ConfigEntryNotFoundException &e) {
792 return default_val;
793 }
794}
795
796} // end namespace fawkes
Thrown if a config entry could not be found.
Definition: config.h:47
ConfigEntryNotFoundException(const char *path)
Constructor.
Definition: config.cpp:445
ConfigTypeMismatchException(const char *path, const char *actual, const char *requested)
Constructor.
Definition: config.cpp:460
Interface for configuration change handling.
const char * config_monitor_prefix()
Which path prefix shall be monitored.
ConfigurationException(const char *msg)
Constructor.
Definition: config.cpp:424
Iterator interface to iterate over config values.
Definition: config.h:75
virtual bool next()=0
Check if there is another element and advance to this if possible.
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 unsigned int get_uint(const char *path)=0
Get value from configuration which is of type unsigned int.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
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 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 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 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 void rem_change_handler(ConfigurationChangeHandler *h)
Remove a configuration change handler.
Definition: config.cpp:619
virtual ValueIterator * get_value(const char *path)=0
Get value from configuration.
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::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 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 std::vector< int > get_ints(const char *path)=0
Get list of values from configuration which is of type int.
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 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 int get_int(const char *path)=0
Get value from configuration which is of type int.
ChangeHandlerMultimap _change_handlers
Registered change handlers.
Definition: config.h:459
virtual void add_change_handler(ConfigurationChangeHandler *h)
Add a configuration change handler.
Definition: config.cpp:603
ChangeHandlerList * find_handlers(const char *path)
Find handlers for given path.
Definition: config.cpp:651
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
CouldNotOpenConfigException(const char *format,...)
Constructor.
Definition: config.cpp:476
Base class for exceptions in Fawkes.
Definition: exception.h:36
void append_va(const char *format, va_list va) noexcept
Append messages to the message list.
Definition: exception.cpp:353
void append(const char *format,...) noexcept
Append messages to the message list.
Definition: exception.cpp:333
Fawkes library namespace.