first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / configuration / configlet.h
1 #ifndef CONFIGLET_CLASS
2 #define CONFIGLET_CLASS
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : configlet                                                         *
7 *  Author : Chris Koeritz                                                     *
8 *                                                                             *
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 \*****************************************************************************/
17
18 #include "configurator.h"
19
20 #include <basis/astring.h>
21 #include <basis/contracts.h>
22
23 namespace configuration {
24
25 //! Represents an atom of configuration info.
26 /*!
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.
30 */
31
32 class configlet : public virtual basis::root_object
33 {
34 public:
35   configlet(const basis::astring &section, const basis::astring &entry);
36     //!< creates a configlet that lives in the "section" at the "entry".
37   configlet(const configlet &to_copy);
38
39   virtual ~configlet();
40
41   DEFINE_CLASS_NAME("configlet");
42
43   configlet &operator =(const configlet &to_copy);
44
45   const basis::astring &section() const;
46     //!< observes the section of this configlet.
47   const basis::astring &entry() const;
48     //!< observes the entry name of this configlet.
49
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.
54
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
61     default. */
62
63   virtual bool store(configurator &config) const = 0;
64     //!< writes the configlet's information out to the "config".
65
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. */
69
70 private:
71   basis::astring *_section;  //!< the section name, with whatever form is appropriate.
72   basis::astring *_entry;  //!< the entry name in the native representation.
73 };
74
75 //////////////
76
77 //! a string_configlet holds onto a character string value.
78 /*!
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.
82 */
83
84 class string_configlet : public configlet
85 {
86 public:
87   string_configlet(const basis::astring &section, const basis::astring &entry,
88           const basis::astring &current_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();
92
93   string_configlet &operator =(const string_configlet &to_copy);
94
95   const basis::astring &current_value() const;
96   const basis::astring &default_value() const;
97
98   void current_value(const basis::astring &new_current);
99   void default_value(const basis::astring &new_default);
100   
101   virtual bool load(configurator &config);
102   virtual bool store(configurator &config) const;
103
104   configlet *duplicate() const;
105
106 private:
107   basis::astring *_current;
108   basis::astring *_default;
109 };
110
111 //////////////
112
113 //! Stores a simple integer in a configuration repository.
114
115 class int_configlet : public configlet
116 {
117 public:
118   int_configlet(const basis::astring &section, const basis::astring &entry,
119           int current_value = 0, int default_value = 0);
120   virtual ~int_configlet();
121
122   int current_value() const { return _current; }
123
124   virtual void current_value(int new_current);
125     //!< the modifier function is virtual so derived classes can extend.
126
127   int default_value() const { return _default; }
128   void default_value(int new_default) { _default = new_default; }
129
130   virtual bool load(configurator &config);
131   virtual bool store(configurator &config) const;
132
133   configlet *duplicate() const;
134
135 private:
136   int _current;
137   int _default;
138 };
139
140 //////////////
141
142 //! Stores an integer in a configuration repository with range checking.
143 /*!
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.
148 */
149
150 class bounded_int_configlet : public int_configlet
151 {
152 public:
153   bounded_int_configlet(const basis::astring &section, const basis::astring &entry,
154           int current_value, int default_value, int minimum, int maximum);
155   virtual ~bounded_int_configlet();
156
157   virtual void current_value(int new_current);
158
159   int minimum() const { return _minimum; }
160   int maximum() const { return _maximum; }
161   
162   void minimum(int new_min) { _minimum = new_min; }
163   void maximum(int new_max) { _maximum = new_max; }
164
165   configlet *duplicate() const;
166
167 private:
168   int _minimum;
169   int _maximum;
170 };
171
172 } //namespace.
173
174 #endif
175