Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2004 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 | // The Configuration class is used to allow multiple distinct configurations |
| 31 | // to co-exist at the same time. A process serving several desktops, for |
| 32 | // instance, can create a Configuration instance for each, to allow them |
| 33 | // to be configured independently, from the command-line, registry, etc. |
| 34 | |
| 35 | #ifndef __RFB_CONFIGURATION_H__ |
| 36 | #define __RFB_CONFIGURATION_H__ |
| 37 | |
| 38 | namespace rfb { |
| 39 | class VoidParameter; |
| 40 | |
| 41 | // -=- Configuration |
| 42 | // Class used to access parameters. |
| 43 | |
| 44 | class Configuration { |
| 45 | public: |
| 46 | // - Set named parameter to value |
| 47 | static bool setParam(const char* param, const char* value, bool immutable=false); |
| 48 | |
| 49 | // - Set parameter to value (separated by "=") |
| 50 | static bool setParam(const char* config, bool immutable=false); |
| 51 | |
| 52 | // - Set named parameter to value, with name truncated at len |
| 53 | static bool setParam(const char* name, int len, |
| 54 | const char* val, bool immutable); |
| 55 | |
| 56 | // - Get named parameter |
| 57 | static VoidParameter* getParam(const char* param); |
| 58 | |
| 59 | static void listParams(int width=79, int nameWidth=10); |
| 60 | |
| 61 | static VoidParameter* head; |
| 62 | }; |
| 63 | |
| 64 | // -=- VoidParameter |
| 65 | // Configuration parameter base-class. |
| 66 | |
| 67 | class VoidParameter { |
| 68 | public: |
| 69 | VoidParameter(const char* name_, const char* desc_); |
| 70 | virtual ~VoidParameter(); |
| 71 | const char* getName() const; |
| 72 | const char* getDescription() const; |
| 73 | |
| 74 | virtual bool setParam(const char* value) = 0; |
| 75 | virtual bool setParam(); |
| 76 | virtual char* getDefaultStr() const = 0; |
| 77 | virtual char* getValueStr() const = 0; |
| 78 | virtual bool isBool() const; |
| 79 | |
| 80 | virtual void setImmutable(); |
Peter Åstrand | 55855d5 | 2005-01-03 12:01:45 +0000 | [diff] [blame^] | 81 | virtual void setHasBeenSet(); |
| 82 | bool hasBeenSet(); |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 83 | |
| 84 | VoidParameter* _next; |
| 85 | protected: |
| 86 | bool immutable; |
Peter Åstrand | 55855d5 | 2005-01-03 12:01:45 +0000 | [diff] [blame^] | 87 | bool _hasBeenSet; |
Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 88 | const char* name; |
| 89 | const char* description; |
| 90 | }; |
| 91 | |
| 92 | class AliasParameter : public VoidParameter { |
| 93 | public: |
| 94 | AliasParameter(const char* name_, const char* desc_,VoidParameter* param_); |
| 95 | virtual bool setParam(const char* value); |
| 96 | virtual bool setParam(); |
| 97 | virtual char* getDefaultStr() const; |
| 98 | virtual char* getValueStr() const; |
| 99 | virtual bool isBool() const; |
| 100 | private: |
| 101 | VoidParameter* param; |
| 102 | }; |
| 103 | |
| 104 | class BoolParameter : public VoidParameter { |
| 105 | public: |
| 106 | BoolParameter(const char* name_, const char* desc_, bool v); |
| 107 | virtual bool setParam(const char* value); |
| 108 | virtual bool setParam(); |
| 109 | virtual void setParam(bool b); |
| 110 | virtual char* getDefaultStr() const; |
| 111 | virtual char* getValueStr() const; |
| 112 | virtual bool isBool() const; |
| 113 | operator bool() const; |
| 114 | protected: |
| 115 | bool value; |
| 116 | bool def_value; |
| 117 | }; |
| 118 | |
| 119 | class IntParameter : public VoidParameter { |
| 120 | public: |
| 121 | IntParameter(const char* name_, const char* desc_, int v); |
| 122 | virtual bool setParam(const char* value); |
| 123 | virtual bool setParam(int v); |
| 124 | virtual char* getDefaultStr() const; |
| 125 | virtual char* getValueStr() const; |
| 126 | operator int() const; |
| 127 | protected: |
| 128 | int value; |
| 129 | int def_value; |
| 130 | }; |
| 131 | |
| 132 | class StringParameter : public VoidParameter { |
| 133 | public: |
| 134 | // StringParameter contains a null-terminated string, which CANNOT |
| 135 | // be Null, and so neither can the default value! |
| 136 | StringParameter(const char* name_, const char* desc_, const char* v); |
| 137 | virtual ~StringParameter(); |
| 138 | virtual bool setParam(const char* value); |
| 139 | virtual char* getDefaultStr() const; |
| 140 | virtual char* getValueStr() const; |
| 141 | |
| 142 | // getData() returns a copy of the data - it must be delete[]d by the |
| 143 | // caller. |
| 144 | char* getData() const { return getValueStr(); } |
| 145 | protected: |
| 146 | char* value; |
| 147 | const char* def_value; |
| 148 | }; |
| 149 | |
| 150 | class BinaryParameter : public VoidParameter { |
| 151 | public: |
| 152 | BinaryParameter(const char* name_, const char* desc_, const void* v, int l); |
| 153 | virtual ~BinaryParameter(); |
| 154 | virtual bool setParam(const char* value); |
| 155 | virtual void setParam(const void* v, int l); |
| 156 | virtual char* getDefaultStr() const; |
| 157 | virtual char* getValueStr() const; |
| 158 | |
| 159 | void getData(void** data, int* length) const; |
| 160 | |
| 161 | protected: |
| 162 | char* value; |
| 163 | int length; |
| 164 | char* def_value; |
| 165 | int def_length; |
| 166 | }; |
| 167 | |
| 168 | }; |
| 169 | |
| 170 | #endif // __RFB_CONFIGURATION_H__ |