LayeredConfiguration.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // LayeredConfiguration.h
  3. //
  4. // Library: Util
  5. // Package: Configuration
  6. // Module: LayeredConfiguration
  7. //
  8. // Definition of the LayeredConfiguration 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_LayeredConfiguration_INCLUDED
  16. #define Util_LayeredConfiguration_INCLUDED
  17. #include "Poco/Util/Util.h"
  18. #include "Poco/Util/AbstractConfiguration.h"
  19. #include "Poco/AutoPtr.h"
  20. #include <list>
  21. namespace Poco {
  22. namespace Util {
  23. class Util_API LayeredConfiguration: public AbstractConfiguration
  24. /// A LayeredConfiguration consists of a number of AbstractConfigurations.
  25. ///
  26. /// When reading a configuration property in a LayeredConfiguration,
  27. /// all added configurations are searched, in order of their priority.
  28. /// Configurations with lower priority values have precedence.
  29. ///
  30. /// When setting a property, the property is always written to the first writeable
  31. /// configuration (see addWriteable()).
  32. /// If no writeable configuration has been added to the LayeredConfiguration, and an
  33. /// attempt is made to set a property, a RuntimeException is thrown.
  34. ///
  35. /// Every configuration added to the LayeredConfiguration has a priority value.
  36. /// The priority determines the position where the configuration is inserted,
  37. /// with lower priority values coming before higher priority values.
  38. ///
  39. /// If no priority is specified, a priority of 0 is assumed.
  40. {
  41. public:
  42. typedef Poco::AutoPtr<AbstractConfiguration> ConfigPtr;
  43. LayeredConfiguration();
  44. /// Creates the LayeredConfiguration.
  45. void add(AbstractConfiguration* pConfig);
  46. /// Adds a read-only configuration to the back of the LayeredConfiguration.
  47. /// The LayeredConfiguration does not take ownership of the given
  48. /// configuration. In other words, the configuration's reference
  49. /// count is incremented.
  50. void add(AbstractConfiguration* pConfig, const std::string& label);
  51. /// Adds a read-only configuration with the given label to the back of the LayeredConfiguration.
  52. /// The LayeredConfiguration does not take ownership of the given
  53. /// configuration. In other words, the configuration's reference
  54. /// count is incremented.
  55. void add(AbstractConfiguration* pConfig, bool shared);
  56. /// Adds a read-only configuration to the back of the LayeredConfiguration.
  57. /// If shared is false, the LayeredConfiguration takes ownership
  58. /// of the given configuration (and the configuration's reference
  59. /// count remains unchanged).
  60. void add(AbstractConfiguration* pConfig, const std::string& label, bool shared);
  61. /// Adds a read-only configuration with the given label to the back of the LayeredConfiguration.
  62. /// If shared is false, the LayeredConfiguration takes ownership
  63. /// of the given configuration (and the configuration's reference
  64. /// count remains unchanged).
  65. void add(AbstractConfiguration* pConfig, int priority);
  66. /// Adds a read-only configuration to the LayeredConfiguration.
  67. /// The LayeredConfiguration does not take ownership of the given
  68. /// configuration. In other words, the configuration's reference
  69. /// count is incremented.
  70. void add(AbstractConfiguration* pConfig, const std::string& label, int priority);
  71. /// Adds a read-only configuration with the given label to the LayeredConfiguration.
  72. /// The LayeredConfiguration does not take ownership of the given
  73. /// configuration. In other words, the configuration's reference
  74. /// count is incremented.
  75. void add(AbstractConfiguration* pConfig, int priority, bool shared);
  76. /// Adds a read-only configuration the LayeredConfiguration.
  77. /// If shared is false, the LayeredConfiguration takes ownership
  78. /// of the given configuration (and the configuration's reference
  79. /// count remains unchanged).
  80. void add(AbstractConfiguration* pConfig, const std::string& label, int priority, bool shared);
  81. /// Adds a read-only configuration with the given label the LayeredConfiguration.
  82. /// If shared is false, the LayeredConfiguration takes ownership
  83. /// of the given configuration (and the configuration's reference
  84. /// count remains unchanged).
  85. void add(AbstractConfiguration* pConfig, int priority, bool writeable, bool shared);
  86. /// Adds a configuration to the LayeredConfiguration.
  87. /// If shared is false, the LayeredConfiguration takes ownership
  88. /// of the given configuration (and the configuration's reference
  89. /// count remains unchanged).
  90. void add(AbstractConfiguration* pConfig, const std::string& label, int priority, bool writeable, bool shared);
  91. /// Adds a configuration with the given label to the LayeredConfiguration.
  92. /// If shared is false, the LayeredConfiguration takes ownership
  93. /// of the given configuration (and the configuration's reference
  94. /// count remains unchanged).
  95. void addWriteable(AbstractConfiguration* pConfig, int priority);
  96. /// Adds a writeable configuration to the LayeredConfiguration.
  97. /// The LayeredConfiguration does not take ownership of the given
  98. /// configuration. In other words, the configuration's reference
  99. /// count is incremented.
  100. void addWriteable(AbstractConfiguration* pConfig, int priority, bool shared);
  101. /// Adds a writeable configuration to the LayeredConfiguration.
  102. /// If shared is false, the LayeredConfiguration takes ownership
  103. /// of the given configuration (and the configuration's reference
  104. /// count remains unchanged).
  105. ConfigPtr find(const std::string& label) const;
  106. /// Finds and returns the configuration with the given label.
  107. ///
  108. /// Returns null if no such configuration can be found.
  109. //@ deprecated
  110. void addFront(AbstractConfiguration* pConfig);
  111. /// Adds a read-only configuration to the front of the LayeredConfiguration.
  112. /// The LayeredConfiguration does not take ownership of the given
  113. /// configuration. In other words, the configuration's reference
  114. /// count is incremented.
  115. //@ deprecated
  116. void addFront(AbstractConfiguration* pConfig, bool shared);
  117. /// Adds a read-only configuration to the front of the LayeredConfiguration.
  118. /// If shared is true, the LayeredConfiguration takes ownership
  119. /// of the given configuration.
  120. void removeConfiguration(AbstractConfiguration* pConfig);
  121. /// Removes the given configuration from the LayeredConfiguration.
  122. ///
  123. /// Does nothing if the given configuration is not part of the
  124. /// LayeredConfiguration.
  125. protected:
  126. struct ConfigItem
  127. {
  128. ConfigPtr pConfig;
  129. int priority;
  130. bool writeable;
  131. std::string label;
  132. };
  133. bool getRaw(const std::string& key, std::string& value) const;
  134. void setRaw(const std::string& key, const std::string& value);
  135. void enumerate(const std::string& key, Keys& range) const;
  136. void removeRaw(const std::string& key);
  137. int lowest() const;
  138. int highest() const;
  139. void insert(const ConfigItem& item);
  140. ~LayeredConfiguration();
  141. private:
  142. LayeredConfiguration(const LayeredConfiguration&);
  143. LayeredConfiguration& operator = (const LayeredConfiguration&);
  144. typedef std::list<ConfigItem> ConfigList;
  145. ConfigList _configs;
  146. };
  147. } } // namespace Poco::Util
  148. #endif // Util_LayeredConfiguration_INCLUDED