1 #ifndef CONFIGLET_CLASS
2 #define CONFIGLET_CLASS
4 /*****************************************************************************\
7 * Author : Chris Koeritz *
9 *******************************************************************************
10 * Copyright (c) 2001-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
18 #include "configurator.h"
20 #include <basis/astring.h>
21 #include <basis/contracts.h>
23 namespace configuration {
25 //! Represents an atom of configuration info.
27 The configlet has a location in a configuration repository that is defined
28 by its section and key name. Derived types can also have a value that is
29 stored in that location.
32 class configlet : public virtual basis::root_object
35 configlet(const basis::astring §ion, const basis::astring &entry);
36 //!< creates a configlet that lives in the "section" at the "entry".
37 configlet(const configlet &to_copy);
41 DEFINE_CLASS_NAME("configlet");
43 configlet &operator =(const configlet &to_copy);
45 const basis::astring §ion() const;
46 //!< observes the section of this configlet.
47 const basis::astring &entry() const;
48 //!< observes the entry name of this configlet.
50 void section(const basis::astring &new_section) const;
51 //!< modifies the configlet section location.
52 void entry(const basis::astring &new_entry) const;
53 //!< modifies the configlet entry name.
55 virtual bool load(configurator &config) = 0;
56 //!< retrieves the configlet's information from the "config".
57 /*!< true is returned when this is successful. note that false is returned
58 if the entry was not originally present; if the configurator has the
59 AUTO_STORE behavior, then we will write out the default value on failure.
60 the next load() would be a success in that case, but would return the
63 virtual bool store(configurator &config) const = 0;
64 //!< writes the configlet's information out to the "config".
66 virtual configlet *duplicate() const = 0;
67 //!< a virtual copy constructor for configlets.
68 /*!< the returned object will be a new copy of this configlet. */
71 basis::astring *_section; //!< the section name, with whatever form is appropriate.
72 basis::astring *_entry; //!< the entry name in the native representation.
77 //! a string_configlet holds onto a character string value.
79 it has a current value, which could change due to updates to the
80 configuration, and a default value that probably won't change as often.
81 if the "load" operation fails, the default value will be used.
84 class string_configlet : public configlet
87 string_configlet(const basis::astring §ion, const basis::astring &entry,
88 const basis::astring ¤t_value = basis::astring::empty_string(),
89 const basis::astring &default_value = basis::astring::empty_string());
90 string_configlet(const string_configlet &to_copy);
91 virtual ~string_configlet();
93 string_configlet &operator =(const string_configlet &to_copy);
95 const basis::astring ¤t_value() const;
96 const basis::astring &default_value() const;
98 void current_value(const basis::astring &new_current);
99 void default_value(const basis::astring &new_default);
101 virtual bool load(configurator &config);
102 virtual bool store(configurator &config) const;
104 configlet *duplicate() const;
107 basis::astring *_current;
108 basis::astring *_default;
113 //! Stores a simple integer in a configuration repository.
115 class int_configlet : public configlet
118 int_configlet(const basis::astring §ion, const basis::astring &entry,
119 int current_value = 0, int default_value = 0);
120 virtual ~int_configlet();
122 int current_value() const { return _current; }
124 virtual void current_value(int new_current);
125 //!< the modifier function is virtual so derived classes can extend.
127 int default_value() const { return _default; }
128 void default_value(int new_default) { _default = new_default; }
130 virtual bool load(configurator &config);
131 virtual bool store(configurator &config) const;
133 configlet *duplicate() const;
142 //! Stores an integer in a configuration repository with range checking.
144 a bounded_int_configlet has current and default values but also specifies a
145 valid range for the current value. if the current value falls outside
146 of that range (even via a "set" operation), then the default value is
147 used for the current.
150 class bounded_int_configlet : public int_configlet
153 bounded_int_configlet(const basis::astring §ion, const basis::astring &entry,
154 int current_value, int default_value, int minimum, int maximum);
155 virtual ~bounded_int_configlet();
157 virtual void current_value(int new_current);
159 int minimum() const { return _minimum; }
160 int maximum() const { return _maximum; }
162 void minimum(int new_min) { _minimum = new_min; }
163 void maximum(int new_max) { _maximum = new_max; }
165 configlet *duplicate() const;