blob: 35e31fc020813bb7942f9ffba8cac5c4f252478a [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
Adam Tkacc58b3d12010-04-23 13:55:10 +000052 enum ConfigurationObject { ConfGlobal, ConfServer, ConfViewer };
53
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000054 // -=- Configuration
55 // Class used to access parameters.
56
57 class Configuration {
58 public:
59 // - Create a new Configuration object
60 Configuration(const char* name, Configuration* attachToGroup=0);
61
62 // - Return the buffer containing the Configuration's name
63 const char* getName() const { return name.buf; }
64
65 // - Assignment operator. For every Parameter in this Configuration's
66 // group, get()s the corresponding source parameter and copies its
67 // content.
68 Configuration& operator=(const Configuration& src);
69
70 // - Set named parameter to value
71 bool set(const char* param, const char* value, bool immutable=false);
72
73 // - Set parameter to value (separated by "=")
74 bool set(const char* config, bool immutable=false);
75
76 // - Set named parameter to value, with name truncated at len
77 bool set(const char* name, int len,
78 const char* val, bool immutable);
79
80 // - Get named parameter
81 VoidParameter* get(const char* param);
82
83 // - List the parameters of this Configuration group
84 void list(int width=79, int nameWidth=10);
85
86 // - readFromFile
87 // Read configuration parameters from the specified file.
88 void readFromFile(const char* filename);
89
90 // - writeConfigToFile
91 // Write a new configuration parameters file, then mv it
92 // over the old file.
93 void writeToFile(const char* filename);
94
95
96 // - Get the Global Configuration object
97 // NB: This call does NOT lock the Configuration system.
98 // ALWAYS ensure that if you have ANY global Parameters,
99 // then they are defined as global objects, to ensure that
100 // global() is called when only the main thread is running.
101 static Configuration* global();
102
Adam Tkacc58b3d12010-04-23 13:55:10 +0000103 // Enable server/viewer specific parameters
104 static void enableServerParams() { global()->appendConfiguration(server()); }
105 static void enableViewerParams() { global()->appendConfiguration(viewer()); }
106
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000107 // - Container for process-wide Global parameters
108 static bool setParam(const char* param, const char* value, bool immutable=false) {
109 return global()->set(param, value, immutable);
110 }
111 static bool setParam(const char* config, bool immutable=false) {
112 return global()->set(config, immutable);
113 }
114 static bool setParam(const char* name, int len,
115 const char* val, bool immutable) {
116 return global()->set(name, len, val, immutable);
117 }
118 static VoidParameter* getParam(const char* param) { return global()->get(param); }
Adam Tkacc58b3d12010-04-23 13:55:10 +0000119 static void listParams(int width=79, int nameWidth=10) {
120 global()->list(width, nameWidth);
121 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000122
Adam Tkacc58b3d12010-04-23 13:55:10 +0000123 private:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000124 friend class VoidParameter;
125 friend struct ParameterIterator;
126
127 // Name for this Configuration
128 CharArray name;
129
130 // - Pointer to first Parameter in this group
131 VoidParameter* head;
132
133 // Pointer to next Configuration in this group
134 Configuration* _next;
135
136 // The process-wide, Global Configuration object
137 static Configuration* global_;
Adam Tkacc58b3d12010-04-23 13:55:10 +0000138
139 // The server only Configuration object
140 static Configuration* server_;
141
142 // The viewer only Configuration object
143 static Configuration* viewer_;
144
145 // Get server/viewer specific configuration object
146 static Configuration* server();
147 static Configuration* viewer();
148
149 // Append configuration object to this instance.
150 // NOTE: conf instance can be only one configuration object
151 void appendConfiguration(Configuration *conf) {
152 conf->_next = _next; _next = conf;
153 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000154 };
155
156 // -=- VoidParameter
157 // Configuration parameter base-class.
158
159 class VoidParameter {
160 public:
Adam Tkacc58b3d12010-04-23 13:55:10 +0000161 VoidParameter(const char* name_, const char* desc_, ConfigurationObject co=ConfGlobal);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000162 virtual ~VoidParameter();
163 const char* getName() const;
164 const char* getDescription() const;
165
166 virtual bool setParam(const char* value) = 0;
167 virtual bool setParam();
168 virtual char* getDefaultStr() const = 0;
169 virtual char* getValueStr() const = 0;
170 virtual bool isBool() const;
171
172 virtual void setImmutable();
173 virtual void setHasBeenSet();
174 bool hasBeenSet();
175
176 protected:
177 friend class Configuration;
178 friend struct ParameterIterator;
179
180 VoidParameter* _next;
181 bool immutable;
182 bool _hasBeenSet;
183 const char* name;
184 const char* description;
185 };
186
187 class AliasParameter : public VoidParameter {
188 public:
Adam Tkacc58b3d12010-04-23 13:55:10 +0000189 AliasParameter(const char* name_, const char* desc_,VoidParameter* param_,
190 ConfigurationObject co=ConfGlobal);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000191 virtual bool setParam(const char* value);
192 virtual bool setParam();
193 virtual char* getDefaultStr() const;
194 virtual char* getValueStr() const;
195 virtual bool isBool() const;
196 virtual void setImmutable();
197 private:
198 VoidParameter* param;
199 };
200
201 class BoolParameter : public VoidParameter {
202 public:
Adam Tkacc58b3d12010-04-23 13:55:10 +0000203 BoolParameter(const char* name_, const char* desc_, bool v,
204 ConfigurationObject co=ConfGlobal);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000205 virtual bool setParam(const char* value);
206 virtual bool setParam();
207 virtual void setParam(bool b);
208 virtual char* getDefaultStr() const;
209 virtual char* getValueStr() const;
210 virtual bool isBool() const;
211 operator bool() const;
212 protected:
213 bool value;
214 bool def_value;
215 };
216
217 class IntParameter : public VoidParameter {
218 public:
219 IntParameter(const char* name_, const char* desc_, int v,
Adam Tkacc58b3d12010-04-23 13:55:10 +0000220 int minValue=INT_MIN, int maxValue=INT_MAX,
221 ConfigurationObject co=ConfGlobal);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000222 virtual bool setParam(const char* value);
223 virtual bool setParam(int v);
224 virtual char* getDefaultStr() const;
225 virtual char* getValueStr() const;
226 operator int() const;
227 protected:
228 int value;
229 int def_value;
230 int minValue, maxValue;
231 };
232
233 class StringParameter : public VoidParameter {
234 public:
235 // StringParameter contains a null-terminated string, which CANNOT
236 // be Null, and so neither can the default value!
Adam Tkacc58b3d12010-04-23 13:55:10 +0000237 StringParameter(const char* name_, const char* desc_, const char* v,
238 ConfigurationObject co=ConfGlobal);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000239 virtual ~StringParameter();
240 virtual bool setParam(const char* value);
241 virtual char* getDefaultStr() const;
242 virtual char* getValueStr() const;
Adam Tkac3c7f8e12010-11-11 14:29:35 +0000243 void setDefaultStr(const char* v);
Pierre Ossman660f1082011-03-02 12:44:12 +0000244 operator const char*() const;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000245
246 // getData() returns a copy of the data - it must be delete[]d by the
247 // caller.
248 char* getData() const { return getValueStr(); }
249 protected:
250 char* value;
251 const char* def_value;
252 };
253
254 class BinaryParameter : public VoidParameter {
255 public:
Adam Tkacc58b3d12010-04-23 13:55:10 +0000256 BinaryParameter(const char* name_, const char* desc_, const void* v, int l,
257 ConfigurationObject co=ConfGlobal);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000258 virtual ~BinaryParameter();
259 virtual bool setParam(const char* value);
260 virtual void setParam(const void* v, int l);
261 virtual char* getDefaultStr() const;
262 virtual char* getValueStr() const;
263
264 // getData() will return length zero if there is no data
265 // NB: data may be set to zero, OR set to a zero-length buffer
266 void getData(void** data, int* length) const;
267
268 protected:
269 char* value;
270 int length;
271 char* def_value;
272 int def_length;
273 };
274
275 // -=- ParameterIterator
276 // Iterates over all the Parameters in a Configuration group. The
277 // current Parameter is accessed via param, the current Configuration
278 // via config. The next() method moves on to the next Parameter.
279
280 struct ParameterIterator {
281 ParameterIterator(Configuration* c) : config(c), param(c ? c->head : 0) {}
282 void next() {
283 param = param->_next;
284 while (!param) {
285 config = config->_next;
286 if (!config) break;
287 param = config->head;
288 }
289 }
290 Configuration* config;
291 VoidParameter* param;
292 };
293
294};
295
296#endif // __RFB_CONFIGURATION_H__