blob: e3b85b839f3e1dd45feda53a5aef15e3bc785235 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19// -=- Configuration.h
20//
21// This header defines a set of classes used to represent configuration
22// parameters of different types. Instances of the different parameter
23// types are associated with instances of the Configuration class, and
24// are each given a unique name. The Configuration class provides a
25// generic API through which parameters may be located by name and their
26// value set, thus removing the need to write platform-specific code.
27// Simply defining a new parameter and associating it with a Configuration
28// will allow it to be configured by the user.
29//
30// If no Configuration is specified when creating a Parameter, then the
31// global Configuration will be assumed.
32//
33// Configurations can be "chained" into groups. Each group has a root
34// Configuration, a pointer to which should be passed to the constructors
35// of the other group members. set() and get() operations called on the
36// root will iterate through all of the group's members.
37//
38// NB: On platforms that support Threading, locking is performed to protect
39// complex parameter types from concurrent access (e.g. strings).
40// NB: NO LOCKING is performed when linking Configurations to groups
41// or when adding Parameters to Configurations.
42
43#ifndef __RFB_CONFIGURATION_H__
44#define __RFB_CONFIGURATION_H__
45
46#include <rfb/util.h>
47
48namespace rfb {
49 class VoidParameter;
50 struct ParameterIterator;
51
52 // -=- Configuration
53 // Class used to access parameters.
54
55 class Configuration {
56 public:
57 // - Create a new Configuration object
58 Configuration(const char* name, Configuration* attachToGroup=0);
59
60 // - Return the buffer containing the Configuration's name
61 const char* getName() const { return name.buf; }
62
63 // - Assignment operator. For every Parameter in this Configuration's
64 // group, get()s the corresponding source parameter and copies its
65 // content.
66 Configuration& operator=(const Configuration& src);
67
68 // - Set named parameter to value
69 bool set(const char* param, const char* value, bool immutable=false);
70
71 // - Set parameter to value (separated by "=")
72 bool set(const char* config, bool immutable=false);
73
74 // - Set named parameter to value, with name truncated at len
75 bool set(const char* name, int len,
76 const char* val, bool immutable);
77
78 // - Get named parameter
79 VoidParameter* get(const char* param);
80
81 // - List the parameters of this Configuration group
82 void list(int width=79, int nameWidth=10);
83
84 // - readFromFile
85 // Read configuration parameters from the specified file.
86 void readFromFile(const char* filename);
87
88 // - writeConfigToFile
89 // Write a new configuration parameters file, then mv it
90 // over the old file.
91 void writeToFile(const char* filename);
92
93
94 // - Get the Global Configuration object
95 // NB: This call does NOT lock the Configuration system.
96 // ALWAYS ensure that if you have ANY global Parameters,
97 // then they are defined as global objects, to ensure that
98 // global() is called when only the main thread is running.
99 static Configuration* global();
100
101 // - Container for process-wide Global parameters
102 static bool setParam(const char* param, const char* value, bool immutable=false) {
103 return global()->set(param, value, immutable);
104 }
105 static bool setParam(const char* config, bool immutable=false) {
106 return global()->set(config, immutable);
107 }
108 static bool setParam(const char* name, int len,
109 const char* val, bool immutable) {
110 return global()->set(name, len, val, immutable);
111 }
112 static VoidParameter* getParam(const char* param) { return global()->get(param); }
113 static void listParams(int width=79, int nameWidth=10) { global()->list(width, nameWidth); }
114
115 protected:
116 friend class VoidParameter;
117 friend struct ParameterIterator;
118
119 // Name for this Configuration
120 CharArray name;
121
122 // - Pointer to first Parameter in this group
123 VoidParameter* head;
124
125 // Pointer to next Configuration in this group
126 Configuration* _next;
127
128 // The process-wide, Global Configuration object
129 static Configuration* global_;
130 };
131
132 // -=- VoidParameter
133 // Configuration parameter base-class.
134
135 class VoidParameter {
136 public:
137 VoidParameter(const char* name_, const char* desc_, Configuration* conf=0);
138 virtual ~VoidParameter();
139 const char* getName() const;
140 const char* getDescription() const;
141
142 virtual bool setParam(const char* value) = 0;
143 virtual bool setParam();
144 virtual char* getDefaultStr() const = 0;
145 virtual char* getValueStr() const = 0;
146 virtual bool isBool() const;
147
148 virtual void setImmutable();
149 virtual void setHasBeenSet();
150 bool hasBeenSet();
151
152 protected:
153 friend class Configuration;
154 friend struct ParameterIterator;
155
156 VoidParameter* _next;
157 bool immutable;
158 bool _hasBeenSet;
159 const char* name;
160 const char* description;
161 };
162
163 class AliasParameter : public VoidParameter {
164 public:
165 AliasParameter(const char* name_, const char* desc_,VoidParameter* param_, Configuration* conf=0);
166 virtual bool setParam(const char* value);
167 virtual bool setParam();
168 virtual char* getDefaultStr() const;
169 virtual char* getValueStr() const;
170 virtual bool isBool() const;
171 virtual void setImmutable();
172 private:
173 VoidParameter* param;
174 };
175
176 class BoolParameter : public VoidParameter {
177 public:
178 BoolParameter(const char* name_, const char* desc_, bool v, Configuration* conf=0);
179 virtual bool setParam(const char* value);
180 virtual bool setParam();
181 virtual void setParam(bool b);
182 virtual char* getDefaultStr() const;
183 virtual char* getValueStr() const;
184 virtual bool isBool() const;
185 operator bool() const;
186 protected:
187 bool value;
188 bool def_value;
189 };
190
191 class IntParameter : public VoidParameter {
192 public:
193 IntParameter(const char* name_, const char* desc_, int v,
194 int minValue=INT_MIN, int maxValue=INT_MAX, Configuration* conf=0);
195 virtual bool setParam(const char* value);
196 virtual bool setParam(int v);
197 virtual char* getDefaultStr() const;
198 virtual char* getValueStr() const;
199 operator int() const;
200 protected:
201 int value;
202 int def_value;
203 int minValue, maxValue;
204 };
205
206 class StringParameter : public VoidParameter {
207 public:
208 // StringParameter contains a null-terminated string, which CANNOT
209 // be Null, and so neither can the default value!
210 StringParameter(const char* name_, const char* desc_, const char* v, Configuration* conf=0);
211 virtual ~StringParameter();
212 virtual bool setParam(const char* value);
213 virtual char* getDefaultStr() const;
214 virtual char* getValueStr() const;
215
216 // getData() returns a copy of the data - it must be delete[]d by the
217 // caller.
218 char* getData() const { return getValueStr(); }
219 protected:
220 char* value;
221 const char* def_value;
222 };
223
224 class BinaryParameter : public VoidParameter {
225 public:
226 BinaryParameter(const char* name_, const char* desc_, const void* v, int l, Configuration* conf=0);
227 virtual ~BinaryParameter();
228 virtual bool setParam(const char* value);
229 virtual void setParam(const void* v, int l);
230 virtual char* getDefaultStr() const;
231 virtual char* getValueStr() const;
232
233 // getData() will return length zero if there is no data
234 // NB: data may be set to zero, OR set to a zero-length buffer
235 void getData(void** data, int* length) const;
236
237 protected:
238 char* value;
239 int length;
240 char* def_value;
241 int def_length;
242 };
243
244 // -=- ParameterIterator
245 // Iterates over all the Parameters in a Configuration group. The
246 // current Parameter is accessed via param, the current Configuration
247 // via config. The next() method moves on to the next Parameter.
248
249 struct ParameterIterator {
250 ParameterIterator(Configuration* c) : config(c), param(c ? c->head : 0) {}
251 void next() {
252 param = param->_next;
253 while (!param) {
254 config = config->_next;
255 if (!config) break;
256 param = config->head;
257 }
258 }
259 Configuration* config;
260 VoidParameter* param;
261 };
262
263};
264
265#endif // __RFB_CONFIGURATION_H__