選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Configuration.h 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. // -=- Configuration.h
  19. //
  20. // This header defines a set of classes used to represent configuration
  21. // parameters of different types. Instances of the different parameter
  22. // types are associated with instances of the Configuration class, and
  23. // are each given a unique name. The Configuration class provides a
  24. // generic API through which parameters may be located by name and their
  25. // value set, thus removing the need to write platform-specific code.
  26. // Simply defining a new parameter and associating it with a Configuration
  27. // will allow it to be configured by the user.
  28. //
  29. // If no Configuration is specified when creating a Parameter, then the
  30. // global Configuration will be assumed.
  31. //
  32. // Configurations can be "chained" into groups. Each group has a root
  33. // Configuration, a pointer to which should be passed to the constructors
  34. // of the other group members. set() and get() operations called on the
  35. // root will iterate through all of the group's members.
  36. //
  37. // NB: On platforms that support Threading, locking is performed to protect
  38. // complex parameter types from concurrent access (e.g. strings).
  39. // NB: NO LOCKING is performed when linking Configurations to groups
  40. // or when adding Parameters to Configurations.
  41. #ifndef __RFB_CONFIGURATION_H__
  42. #define __RFB_CONFIGURATION_H__
  43. #include <rfb/util.h>
  44. namespace os { class Mutex; }
  45. namespace rfb {
  46. class VoidParameter;
  47. struct ParameterIterator;
  48. enum ConfigurationObject { ConfGlobal, ConfServer, ConfViewer };
  49. // -=- Configuration
  50. // Class used to access parameters.
  51. class Configuration {
  52. public:
  53. // - Create a new Configuration object
  54. Configuration(const char* name_) : name(strDup(name_)), head(0), _next(0) {}
  55. // - Return the buffer containing the Configuration's name
  56. const char* getName() const { return name.buf; }
  57. // - Set named parameter to value
  58. bool set(const char* param, const char* value, bool immutable=false);
  59. // - Set parameter to value (separated by "=")
  60. bool set(const char* config, bool immutable=false);
  61. // - Set named parameter to value, with name truncated at len
  62. bool set(const char* name, int len,
  63. const char* val, bool immutable);
  64. // - Get named parameter
  65. VoidParameter* get(const char* param);
  66. // - List the parameters of this Configuration group
  67. void list(int width=79, int nameWidth=10);
  68. // - Remove a parameter from this Configuration group
  69. bool remove(const char* param);
  70. // - readFromFile
  71. // Read configuration parameters from the specified file.
  72. void readFromFile(const char* filename);
  73. // - writeConfigToFile
  74. // Write a new configuration parameters file, then mv it
  75. // over the old file.
  76. void writeToFile(const char* filename);
  77. // - Get the Global Configuration object
  78. // NB: This call does NOT lock the Configuration system.
  79. // ALWAYS ensure that if you have ANY global Parameters,
  80. // then they are defined as global objects, to ensure that
  81. // global() is called when only the main thread is running.
  82. static Configuration* global();
  83. // Enable server/viewer specific parameters
  84. static void enableServerParams() { global()->appendConfiguration(server()); }
  85. static void enableViewerParams() { global()->appendConfiguration(viewer()); }
  86. // - Container for process-wide Global parameters
  87. static bool setParam(const char* param, const char* value, bool immutable=false) {
  88. return global()->set(param, value, immutable);
  89. }
  90. static bool setParam(const char* config, bool immutable=false) {
  91. return global()->set(config, immutable);
  92. }
  93. static bool setParam(const char* name, int len,
  94. const char* val, bool immutable) {
  95. return global()->set(name, len, val, immutable);
  96. }
  97. static VoidParameter* getParam(const char* param) { return global()->get(param); }
  98. static void listParams(int width=79, int nameWidth=10) {
  99. global()->list(width, nameWidth);
  100. }
  101. static bool removeParam(const char* param) {
  102. return global()->remove(param);
  103. }
  104. private:
  105. friend class VoidParameter;
  106. friend struct ParameterIterator;
  107. // Name for this Configuration
  108. CharArray name;
  109. // - Pointer to first Parameter in this group
  110. VoidParameter* head;
  111. // Pointer to next Configuration in this group
  112. Configuration* _next;
  113. // The process-wide, Global Configuration object
  114. static Configuration* global_;
  115. // The server only Configuration object
  116. static Configuration* server_;
  117. // The viewer only Configuration object
  118. static Configuration* viewer_;
  119. // Get server/viewer specific configuration object
  120. static Configuration* server();
  121. static Configuration* viewer();
  122. // Append configuration object to this instance.
  123. // NOTE: conf instance can be only one configuration object
  124. void appendConfiguration(Configuration *conf) {
  125. conf->_next = _next; _next = conf;
  126. }
  127. };
  128. // -=- VoidParameter
  129. // Configuration parameter base-class.
  130. class VoidParameter {
  131. public:
  132. VoidParameter(const char* name_, const char* desc_, ConfigurationObject co=ConfGlobal);
  133. virtual ~VoidParameter();
  134. const char* getName() const;
  135. const char* getDescription() const;
  136. virtual bool setParam(const char* value) = 0;
  137. virtual bool setParam();
  138. virtual char* getDefaultStr() const = 0;
  139. virtual char* getValueStr() const = 0;
  140. virtual bool isBool() const;
  141. virtual void setImmutable();
  142. protected:
  143. friend class Configuration;
  144. friend struct ParameterIterator;
  145. VoidParameter* _next;
  146. bool immutable;
  147. const char* name;
  148. const char* description;
  149. os::Mutex* mutex;
  150. };
  151. class AliasParameter : public VoidParameter {
  152. public:
  153. AliasParameter(const char* name_, const char* desc_,VoidParameter* param_,
  154. ConfigurationObject co=ConfGlobal);
  155. virtual bool setParam(const char* value);
  156. virtual bool setParam();
  157. virtual char* getDefaultStr() const;
  158. virtual char* getValueStr() const;
  159. virtual bool isBool() const;
  160. virtual void setImmutable();
  161. private:
  162. VoidParameter* param;
  163. };
  164. class BoolParameter : public VoidParameter {
  165. public:
  166. BoolParameter(const char* name_, const char* desc_, bool v,
  167. ConfigurationObject co=ConfGlobal);
  168. virtual bool setParam(const char* value);
  169. virtual bool setParam();
  170. virtual void setParam(bool b);
  171. virtual char* getDefaultStr() const;
  172. virtual char* getValueStr() const;
  173. virtual bool isBool() const;
  174. operator bool() const;
  175. protected:
  176. bool value;
  177. bool def_value;
  178. };
  179. class IntParameter : public VoidParameter {
  180. public:
  181. IntParameter(const char* name_, const char* desc_, int v,
  182. int minValue=INT_MIN, int maxValue=INT_MAX,
  183. ConfigurationObject co=ConfGlobal);
  184. using VoidParameter::setParam;
  185. virtual bool setParam(const char* value);
  186. virtual bool setParam(int v);
  187. virtual char* getDefaultStr() const;
  188. virtual char* getValueStr() const;
  189. operator int() const;
  190. protected:
  191. int value;
  192. int def_value;
  193. int minValue, maxValue;
  194. };
  195. class StringParameter : public VoidParameter {
  196. public:
  197. // StringParameter contains a null-terminated string, which CANNOT
  198. // be Null, and so neither can the default value!
  199. StringParameter(const char* name_, const char* desc_, const char* v,
  200. ConfigurationObject co=ConfGlobal);
  201. virtual ~StringParameter();
  202. virtual bool setParam(const char* value);
  203. virtual char* getDefaultStr() const;
  204. virtual char* getValueStr() const;
  205. void setDefaultStr(const char* v);
  206. operator const char*() const;
  207. // getData() returns a copy of the data - it must be delete[]d by the
  208. // caller.
  209. char* getData() const { return getValueStr(); }
  210. protected:
  211. char* value;
  212. const char* def_value;
  213. };
  214. class BinaryParameter : public VoidParameter {
  215. public:
  216. BinaryParameter(const char* name_, const char* desc_, const void* v, int l,
  217. ConfigurationObject co=ConfGlobal);
  218. using VoidParameter::setParam;
  219. virtual ~BinaryParameter();
  220. virtual bool setParam(const char* value);
  221. virtual void setParam(const void* v, int l);
  222. virtual char* getDefaultStr() const;
  223. virtual char* getValueStr() const;
  224. // getData() will return length zero if there is no data
  225. // NB: data may be set to zero, OR set to a zero-length buffer
  226. void getData(void** data, int* length) const;
  227. protected:
  228. char* value;
  229. int length;
  230. char* def_value;
  231. int def_length;
  232. };
  233. // -=- ParameterIterator
  234. // Iterates over all enabled parameters (global + server/viewer).
  235. // Current Parameter is accessed via param, the current Configuration
  236. // via config. The next() method moves on to the next Parameter.
  237. struct ParameterIterator {
  238. ParameterIterator() : config(Configuration::global()), param(config->head) {}
  239. void next() {
  240. param = param->_next;
  241. while (!param) {
  242. config = config->_next;
  243. if (!config) break;
  244. param = config->head;
  245. }
  246. }
  247. Configuration* config;
  248. VoidParameter* param;
  249. };
  250. };
  251. #endif // __RFB_CONFIGURATION_H__