AbstractConfiguration.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //
  2. // AbstractConfiguration.h
  3. //
  4. // Library: Util
  5. // Package: Configuration
  6. // Module: AbstractConfiguration
  7. //
  8. // Definition of the AbstractConfiguration class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Util_AbstractConfiguration_INCLUDED
  16. #define Util_AbstractConfiguration_INCLUDED
  17. #include "Poco/Util/Util.h"
  18. #include "Poco/Mutex.h"
  19. #include "Poco/RefCountedObject.h"
  20. #include "Poco/BasicEvent.h"
  21. #include <vector>
  22. #include <utility>
  23. namespace Poco {
  24. namespace Util {
  25. class Util_API AbstractConfiguration: public Poco::RefCountedObject
  26. /// AbstractConfiguration is an abstract base class for different
  27. /// kinds of configuration data, such as INI files, property files,
  28. /// XML configuration files or the Windows Registry.
  29. ///
  30. /// Configuration property keys have a hierarchical format, consisting
  31. /// of names separated by periods. The exact interpretation of key names
  32. /// is up to the actual subclass implementation of AbstractConfiguration.
  33. /// Keys are case sensitive.
  34. ///
  35. /// All public methods are synchronized, so the class is safe for multithreaded use.
  36. /// AbstractConfiguration implements reference counting based garbage collection.
  37. ///
  38. /// Subclasses must override the getRaw(), setRaw() and enumerate() methods.
  39. {
  40. public:
  41. typedef std::vector<std::string> Keys;
  42. class KeyValue
  43. /// A key-value pair, used as event argument.
  44. {
  45. public:
  46. KeyValue(const std::string& key, std::string& value):
  47. _key(key),
  48. _value(value)
  49. {
  50. }
  51. const std::string& key() const
  52. {
  53. return _key;
  54. }
  55. const std::string& value() const
  56. {
  57. return _value;
  58. }
  59. std::string& value()
  60. {
  61. return _value;
  62. }
  63. private:
  64. const std::string& _key;
  65. std::string& _value;
  66. };
  67. Poco::BasicEvent<KeyValue> propertyChanging;
  68. /// Fired before a property value is changed or
  69. /// a new property is created.
  70. ///
  71. /// Can be used to check or fix a property value,
  72. /// or to cancel the change by throwing an exception.
  73. ///
  74. /// The event delegate can use one of the get...() functions
  75. /// to obtain the current property value.
  76. Poco::BasicEvent<const KeyValue> propertyChanged;
  77. /// Fired after a property value has been changed
  78. /// or a property has been created.
  79. Poco::BasicEvent<const std::string> propertyRemoving;
  80. /// Fired before a property is removed by a
  81. /// call to remove().
  82. ///
  83. /// Note: This will even be fired if the key
  84. /// does not exist and the remove operation will
  85. /// fail with an exception.
  86. Poco::BasicEvent<const std::string> propertyRemoved;
  87. /// Fired after a property has been removed by
  88. /// a call to remove().
  89. AbstractConfiguration();
  90. /// Creates the AbstractConfiguration.
  91. bool hasProperty(const std::string& key) const;
  92. /// Returns true iff the property with the given key exists.
  93. bool hasOption(const std::string& key) const;
  94. /// Returns true iff the property with the given key exists.
  95. ///
  96. /// Same as hasProperty().
  97. bool has(const std::string& key) const;
  98. /// Returns true iff the property with the given key exists.
  99. ///
  100. /// Same as hasProperty().
  101. std::string getString(const std::string& key) const;
  102. /// Returns the string value of the property with the given name.
  103. /// Throws a NotFoundException if the key does not exist.
  104. /// If the value contains references to other properties (${<property>}), these
  105. /// are expanded.
  106. std::string getString(const std::string& key, const std::string& defaultValue) const;
  107. /// If a property with the given key exists, returns the property's string value,
  108. /// otherwise returns the given default value.
  109. /// If the value contains references to other properties (${<property>}), these
  110. /// are expanded.
  111. std::string getRawString(const std::string& key) const;
  112. /// Returns the raw string value of the property with the given name.
  113. /// Throws a NotFoundException if the key does not exist.
  114. /// References to other properties are not expanded.
  115. std::string getRawString(const std::string& key, const std::string& defaultValue) const;
  116. /// If a property with the given key exists, returns the property's raw string value,
  117. /// otherwise returns the given default value.
  118. /// References to other properties are not expanded.
  119. int getInt(const std::string& key) const;
  120. /// Returns the int value of the property with the given name.
  121. /// Throws a NotFoundException if the key does not exist.
  122. /// Throws a SyntaxException if the property can not be converted
  123. /// to an int.
  124. /// Numbers starting with 0x are treated as hexadecimal.
  125. /// If the value contains references to other properties (${<property>}), these
  126. /// are expanded.
  127. unsigned int getUInt(const std::string& key) const;
  128. /// Returns the unsigned int value of the property with the given name.
  129. /// Throws a NotFoundException if the key does not exist.
  130. /// Throws a SyntaxException if the property can not be converted
  131. /// to an unsigned int.
  132. /// Numbers starting with 0x are treated as hexadecimal.
  133. /// If the value contains references to other properties (${<property>}), these
  134. /// are expanded.
  135. int getInt(const std::string& key, int defaultValue) const;
  136. /// If a property with the given key exists, returns the property's int value,
  137. /// otherwise returns the given default value.
  138. /// Throws a SyntaxException if the property can not be converted
  139. /// to an int.
  140. /// Numbers starting with 0x are treated as hexadecimal.
  141. /// If the value contains references to other properties (${<property>}), these
  142. /// are expanded.
  143. unsigned int getUInt(const std::string& key, unsigned int defaultValue) const;
  144. /// If a property with the given key exists, returns the property's unsigned int
  145. /// value, otherwise returns the given default value.
  146. /// Throws a SyntaxException if the property can not be converted
  147. /// to an unsigned int.
  148. /// Numbers starting with 0x are treated as hexadecimal.
  149. /// If the value contains references to other properties (${<property>}), these
  150. /// are expanded.
  151. #if defined(POCO_HAVE_INT64)
  152. Int64 getInt64(const std::string& key) const;
  153. /// Returns the Int64 value of the property with the given name.
  154. /// Throws a NotFoundException if the key does not exist.
  155. /// Throws a SyntaxException if the property can not be converted
  156. /// to an Int64.
  157. /// Numbers starting with 0x are treated as hexadecimal.
  158. /// If the value contains references to other properties (${<property>}), these
  159. /// are expanded.
  160. UInt64 getUInt64(const std::string& key) const;
  161. /// Returns the UInt64 value of the property with the given name.
  162. /// Throws a NotFoundException if the key does not exist.
  163. /// Throws a SyntaxException if the property can not be converted
  164. /// to an UInt64.
  165. /// Numbers starting with 0x are treated as hexadecimal.
  166. /// If the value contains references to other properties (${<property>}), these
  167. /// are expanded.
  168. Int64 getInt64(const std::string& key, Int64 defaultValue) const;
  169. /// If a property with the given key exists, returns the property's Int64 value,
  170. /// otherwise returns the given default value.
  171. /// Throws a SyntaxException if the property can not be converted
  172. /// to an Int64.
  173. /// Numbers starting with 0x are treated as hexadecimal.
  174. /// If the value contains references to other properties (${<property>}), these
  175. /// are expanded.
  176. UInt64 getUInt64(const std::string& key, UInt64 defaultValue) const;
  177. /// If a property with the given key exists, returns the property's UInt64
  178. /// value, otherwise returns the given default value.
  179. /// Throws a SyntaxException if the property can not be converted
  180. /// to an UInt64.
  181. /// Numbers starting with 0x are treated as hexadecimal.
  182. /// If the value contains references to other properties (${<property>}), these
  183. /// are expanded.
  184. #endif // defined(POCO_HAVE_INT64)
  185. double getDouble(const std::string& key) const;
  186. /// Returns the double value of the property with the given name.
  187. /// Throws a NotFoundException if the key does not exist.
  188. /// Throws a SyntaxException if the property can not be converted
  189. /// to a double.
  190. /// If the value contains references to other properties (${<property>}), these
  191. /// are expanded.
  192. double getDouble(const std::string& key, double defaultValue) const;
  193. /// If a property with the given key exists, returns the property's double value,
  194. /// otherwise returns the given default value.
  195. /// Throws a SyntaxException if the property can not be converted
  196. /// to an double.
  197. /// If the value contains references to other properties (${<property>}), these
  198. /// are expanded.
  199. bool getBool(const std::string& key) const;
  200. /// Returns the boolean value of the property with the given name.
  201. /// Throws a NotFoundException if the key does not exist.
  202. /// Throws a SyntaxException if the property can not be converted
  203. /// to a boolean.
  204. /// If the value contains references to other properties (${<property>}), these
  205. /// are expanded.
  206. bool getBool(const std::string& key, bool defaultValue) const;
  207. /// If a property with the given key exists, returns the property's boolean value,
  208. /// otherwise returns the given default value.
  209. /// Throws a SyntaxException if the property can not be converted
  210. /// to a boolean.
  211. /// The following string values can be converted into a boolean:
  212. /// - numerical values: non zero becomes true, zero becomes false
  213. /// - strings: true, yes, on become true, false, no, off become false
  214. /// Case does not matter.
  215. /// If the value contains references to other properties (${<property>}), these
  216. /// are expanded.
  217. virtual void setString(const std::string& key, const std::string& value);
  218. /// Sets the property with the given key to the given value.
  219. /// An already existing value for the key is overwritten.
  220. virtual void setInt(const std::string& key, int value);
  221. /// Sets the property with the given key to the given value.
  222. /// An already existing value for the key is overwritten.
  223. virtual void setUInt(const std::string& key, unsigned int value);
  224. /// Sets the property with the given key to the given value.
  225. /// An already existing value for the key is overwritten.
  226. #if defined(POCO_HAVE_INT64)
  227. virtual void setInt64(const std::string& key, Int64 value);
  228. /// Sets the property with the given key to the given value.
  229. /// An already existing value for the key is overwritten.
  230. virtual void setUInt64(const std::string& key, UInt64 value);
  231. /// Sets the property with the given key to the given value.
  232. /// An already existing value for the key is overwritten.
  233. #endif // defined(POCO_HAVE_INT64)
  234. virtual void setDouble(const std::string& key, double value);
  235. /// Sets the property with the given key to the given value.
  236. /// An already existing value for the key is overwritten.
  237. virtual void setBool(const std::string& key, bool value);
  238. /// Sets the property with the given key to the given value.
  239. /// An already existing value for the key is overwritten.
  240. void keys(Keys& range) const;
  241. /// Returns in range the names of all keys at root level.
  242. void keys(const std::string& key, Keys& range) const;
  243. /// Returns in range the names of all subkeys under the given key.
  244. /// If an empty key is passed, all root level keys are returned.
  245. const AbstractConfiguration* createView(const std::string& prefix) const;
  246. /// Creates a non-mutable view (see ConfigurationView) into the configuration.
  247. AbstractConfiguration* createView(const std::string& prefix);
  248. /// Creates a view (see ConfigurationView) into the configuration.
  249. std::string expand(const std::string& value) const;
  250. /// Replaces all occurrences of ${<property>} in value with the
  251. /// value of the <property>. If <property> does not exist,
  252. /// nothing is changed.
  253. ///
  254. /// If a circular property reference is detected, a
  255. /// CircularReferenceException will be thrown.
  256. void remove(const std::string& key);
  257. /// Removes the property with the given key.
  258. ///
  259. /// Does nothing if the key does not exist.
  260. void enableEvents(bool enable = true);
  261. /// Enables (or disables) events.
  262. bool eventsEnabled() const;
  263. /// Returns true iff events are enabled.
  264. protected:
  265. virtual bool getRaw(const std::string& key, std::string& value) const = 0;
  266. /// If the property with the given key exists, stores the property's value
  267. /// in value and returns true. Otherwise, returns false.
  268. ///
  269. /// Must be overridden by subclasses.
  270. virtual void setRaw(const std::string& key, const std::string& value) = 0;
  271. /// Sets the property with the given key to the given value.
  272. /// An already existing value for the key is overwritten.
  273. ///
  274. /// Must be overridden by subclasses.
  275. virtual void enumerate(const std::string& key, Keys& range) const = 0;
  276. /// Returns in range the names of all subkeys under the given key.
  277. /// If an empty key is passed, all root level keys are returned.
  278. virtual void removeRaw(const std::string& key);
  279. /// Removes the property with the given key.
  280. ///
  281. /// Does nothing if the key does not exist.
  282. ///
  283. /// Should be overridden by subclasses; the default
  284. /// implementation throws a Poco::NotImplementedException.
  285. static int parseInt(const std::string& value);
  286. static unsigned parseUInt(const std::string& value);
  287. #if defined(POCO_HAVE_INT64)
  288. static Int64 parseInt64(const std::string& value);
  289. static UInt64 parseUInt64(const std::string& value);
  290. #endif // defined(POCO_HAVE_INT64)
  291. static bool parseBool(const std::string& value);
  292. void setRawWithEvent(const std::string& key, std::string value);
  293. virtual ~AbstractConfiguration();
  294. private:
  295. std::string internalExpand(const std::string& value) const;
  296. std::string uncheckedExpand(const std::string& value) const;
  297. AbstractConfiguration(const AbstractConfiguration&);
  298. AbstractConfiguration& operator = (const AbstractConfiguration&);
  299. mutable int _depth;
  300. bool _eventsEnabled;
  301. mutable Poco::Mutex _mutex;
  302. friend class LayeredConfiguration;
  303. friend class ConfigurationView;
  304. friend class ConfigurationMapper;
  305. };
  306. } } // namespace Poco::Util
  307. #endif // Util_AbstractConfiguration_INCLUDED