Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* 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 | |
| 48 | namespace rfb { |
| 49 | class VoidParameter; |
| 50 | struct ParameterIterator; |
| 51 | |
Adam Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 52 | enum ConfigurationObject { ConfGlobal, ConfServer, ConfViewer }; |
| 53 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 54 | // -=- 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 Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 103 | // Enable server/viewer specific parameters |
| 104 | static void enableServerParams() { global()->appendConfiguration(server()); } |
| 105 | static void enableViewerParams() { global()->appendConfiguration(viewer()); } |
| 106 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 107 | // - 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 Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 119 | static void listParams(int width=79, int nameWidth=10) { |
| 120 | global()->list(width, nameWidth); |
| 121 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 122 | |
Adam Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 123 | private: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 124 | 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 Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 138 | |
| 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | // -=- VoidParameter |
| 157 | // Configuration parameter base-class. |
| 158 | |
| 159 | class VoidParameter { |
| 160 | public: |
Adam Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 161 | VoidParameter(const char* name_, const char* desc_, ConfigurationObject co=ConfGlobal); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 162 | 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 Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 189 | AliasParameter(const char* name_, const char* desc_,VoidParameter* param_, |
| 190 | ConfigurationObject co=ConfGlobal); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 191 | 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 Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 203 | BoolParameter(const char* name_, const char* desc_, bool v, |
| 204 | ConfigurationObject co=ConfGlobal); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 205 | 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 Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 220 | int minValue=INT_MIN, int maxValue=INT_MAX, |
| 221 | ConfigurationObject co=ConfGlobal); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 222 | 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 Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 237 | StringParameter(const char* name_, const char* desc_, const char* v, |
| 238 | ConfigurationObject co=ConfGlobal); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 239 | virtual ~StringParameter(); |
| 240 | virtual bool setParam(const char* value); |
| 241 | virtual char* getDefaultStr() const; |
| 242 | virtual char* getValueStr() const; |
Adam Tkac | 3c7f8e1 | 2010-11-11 14:29:35 +0000 | [diff] [blame] | 243 | void setDefaultStr(const char* v); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 244 | |
| 245 | // getData() returns a copy of the data - it must be delete[]d by the |
| 246 | // caller. |
| 247 | char* getData() const { return getValueStr(); } |
| 248 | protected: |
| 249 | char* value; |
| 250 | const char* def_value; |
| 251 | }; |
| 252 | |
| 253 | class BinaryParameter : public VoidParameter { |
| 254 | public: |
Adam Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 255 | BinaryParameter(const char* name_, const char* desc_, const void* v, int l, |
| 256 | ConfigurationObject co=ConfGlobal); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 257 | virtual ~BinaryParameter(); |
| 258 | virtual bool setParam(const char* value); |
| 259 | virtual void setParam(const void* v, int l); |
| 260 | virtual char* getDefaultStr() const; |
| 261 | virtual char* getValueStr() const; |
| 262 | |
| 263 | // getData() will return length zero if there is no data |
| 264 | // NB: data may be set to zero, OR set to a zero-length buffer |
| 265 | void getData(void** data, int* length) const; |
| 266 | |
| 267 | protected: |
| 268 | char* value; |
| 269 | int length; |
| 270 | char* def_value; |
| 271 | int def_length; |
| 272 | }; |
| 273 | |
| 274 | // -=- ParameterIterator |
| 275 | // Iterates over all the Parameters in a Configuration group. The |
| 276 | // current Parameter is accessed via param, the current Configuration |
| 277 | // via config. The next() method moves on to the next Parameter. |
| 278 | |
| 279 | struct ParameterIterator { |
| 280 | ParameterIterator(Configuration* c) : config(c), param(c ? c->head : 0) {} |
| 281 | void next() { |
| 282 | param = param->_next; |
| 283 | while (!param) { |
| 284 | config = config->_next; |
| 285 | if (!config) break; |
| 286 | param = config->head; |
| 287 | } |
| 288 | } |
| 289 | Configuration* config; |
| 290 | VoidParameter* param; |
| 291 | }; |
| 292 | |
| 293 | }; |
| 294 | |
| 295 | #endif // __RFB_CONFIGURATION_H__ |