blob: 1b37ac944609cdef00bfa4f2db045766667a831e [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();
81
82 VoidParameter* _next;
83 protected:
84 bool immutable;
85 const char* name;
86 const char* description;
87 };
88
89 class AliasParameter : public VoidParameter {
90 public:
91 AliasParameter(const char* name_, const char* desc_,VoidParameter* param_);
92 virtual bool setParam(const char* value);
93 virtual bool setParam();
94 virtual char* getDefaultStr() const;
95 virtual char* getValueStr() const;
96 virtual bool isBool() const;
97 private:
98 VoidParameter* param;
99 };
100
101 class BoolParameter : public VoidParameter {
102 public:
103 BoolParameter(const char* name_, const char* desc_, bool v);
104 virtual bool setParam(const char* value);
105 virtual bool setParam();
106 virtual void setParam(bool b);
107 virtual char* getDefaultStr() const;
108 virtual char* getValueStr() const;
109 virtual bool isBool() const;
110 operator bool() const;
111 protected:
112 bool value;
113 bool def_value;
114 };
115
116 class IntParameter : public VoidParameter {
117 public:
118 IntParameter(const char* name_, const char* desc_, int v);
119 virtual bool setParam(const char* value);
120 virtual bool setParam(int v);
121 virtual char* getDefaultStr() const;
122 virtual char* getValueStr() const;
123 operator int() const;
124 protected:
125 int value;
126 int def_value;
127 };
128
129 class StringParameter : public VoidParameter {
130 public:
131 // StringParameter contains a null-terminated string, which CANNOT
132 // be Null, and so neither can the default value!
133 StringParameter(const char* name_, const char* desc_, const char* v);
134 virtual ~StringParameter();
135 virtual bool setParam(const char* value);
136 virtual char* getDefaultStr() const;
137 virtual char* getValueStr() const;
138
139 // getData() returns a copy of the data - it must be delete[]d by the
140 // caller.
141 char* getData() const { return getValueStr(); }
142 protected:
143 char* value;
144 const char* def_value;
145 };
146
147 class BinaryParameter : public VoidParameter {
148 public:
149 BinaryParameter(const char* name_, const char* desc_, const void* v, int l);
150 virtual ~BinaryParameter();
151 virtual bool setParam(const char* value);
152 virtual void setParam(const void* v, int l);
153 virtual char* getDefaultStr() const;
154 virtual char* getValueStr() const;
155
156 void getData(void** data, int* length) const;
157
158 protected:
159 char* value;
160 int length;
161 char* def_value;
162 int def_length;
163 };
164
165};
166
167#endif // __RFB_CONFIGURATION_H__