blob: d50c1cb1842d02018cff40cc8b08bd10501f0d78 [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* 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
38namespace 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 Åstrand55855d52005-01-03 12:01:45 +000081 virtual void setHasBeenSet();
82 bool hasBeenSet();
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000083
84 VoidParameter* _next;
85 protected:
86 bool immutable;
Peter Åstrand55855d52005-01-03 12:01:45 +000087 bool _hasBeenSet;
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +000088 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;
Peter Åstrand07350592005-01-03 14:47:42 +0000100 virtual void setImmutable();
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +0000101 private:
102 VoidParameter* param;
103 };
104
105 class BoolParameter : public VoidParameter {
106 public:
107 BoolParameter(const char* name_, const char* desc_, bool v);
108 virtual bool setParam(const char* value);
109 virtual bool setParam();
110 virtual void setParam(bool b);
111 virtual char* getDefaultStr() const;
112 virtual char* getValueStr() const;
113 virtual bool isBool() const;
114 operator bool() const;
115 protected:
116 bool value;
117 bool def_value;
118 };
119
120 class IntParameter : public VoidParameter {
121 public:
122 IntParameter(const char* name_, const char* desc_, int v);
123 virtual bool setParam(const char* value);
124 virtual bool setParam(int v);
125 virtual char* getDefaultStr() const;
126 virtual char* getValueStr() const;
127 operator int() const;
128 protected:
129 int value;
130 int def_value;
131 };
132
133 class StringParameter : public VoidParameter {
134 public:
135 // StringParameter contains a null-terminated string, which CANNOT
136 // be Null, and so neither can the default value!
137 StringParameter(const char* name_, const char* desc_, const char* v);
138 virtual ~StringParameter();
139 virtual bool setParam(const char* value);
140 virtual char* getDefaultStr() const;
141 virtual char* getValueStr() const;
142
143 // getData() returns a copy of the data - it must be delete[]d by the
144 // caller.
145 char* getData() const { return getValueStr(); }
146 protected:
147 char* value;
148 const char* def_value;
149 };
150
151 class BinaryParameter : public VoidParameter {
152 public:
153 BinaryParameter(const char* name_, const char* desc_, const void* v, int l);
154 virtual ~BinaryParameter();
155 virtual bool setParam(const char* value);
156 virtual void setParam(const void* v, int l);
157 virtual char* getDefaultStr() const;
158 virtual char* getValueStr() const;
159
160 void getData(void** data, int* length) const;
161
162 protected:
163 char* value;
164 int length;
165 char* def_value;
166 int def_length;
167 };
168
169};
170
171#endif // __RFB_CONFIGURATION_H__