config_data.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef CONFIG_DATA_H
  15. #define CONFIG_DATA_H 1
  16. #include <string>
  17. #include <vector>
  18. #include <config/module_spec.h>
  19. #include <exceptions/exceptions.h>
  20. namespace isc {
  21. namespace config {
  22. /// This exception is thrown when the caller is trying to access
  23. /// data that doesn't exist (i.e. with an identifier that does not
  24. /// point to anything defined in the .spec file)
  25. class DataNotFoundError : public isc::Exception {
  26. public:
  27. DataNotFoundError(const char* file, size_t line, const char* what) :
  28. isc::Exception(file, line, what) {}
  29. };
  30. class ConfigData {
  31. public:
  32. /// Constructs a ConfigData option with no specification and an
  33. /// empty configuration.
  34. ConfigData() { _config = isc::data::Element::createMap(); };
  35. /// Constructs a ConfigData option with the given specification
  36. /// and an empty configuration.
  37. /// \param module_spec A ModuleSpec for the relevant module
  38. ConfigData(const ModuleSpec& module_spec) : _module_spec(module_spec) {
  39. _config = isc::data::Element::createMap();
  40. }
  41. virtual ~ConfigData() {};
  42. /// Returns the value currently set for the given identifier
  43. /// If no value is set, the default value (as specified by the
  44. /// .spec file) is returned. If there is no value and no default,
  45. /// an empty ElementPtr is returned.
  46. /// Raises a DataNotFoundError if the identifier is bad.
  47. /// \param identifier The identifier pointing to the configuration
  48. /// value that is to be returned
  49. isc::data::ConstElementPtr getValue(const std::string& identifier) const;
  50. /// Returns the default value for the given identifier.
  51. ///
  52. /// \exception DataNotFoundError if the given identifier does not
  53. /// exist, or if the given value has no specified default
  54. ///
  55. /// \param identifier The identifier pointing to the configuration
  56. /// value for which the default is to be returned
  57. /// \return ElementPtr containing the default value
  58. isc::data::ConstElementPtr getDefaultValue(const std::string& identifier) const;
  59. /// Returns the value currently set for the given identifier
  60. /// If no value is set, the default value (as specified by the
  61. /// .spec file) is returned. If there is no value and no default,
  62. /// an empty ElementPtr is returned.
  63. /// Raises a DataNotFoundError if the identifier is bad.
  64. /// \param is_default will be set to true if the value is taken
  65. /// from the specifications item_default setting,
  66. /// false otherwise
  67. /// \param identifier The identifier pointing to the configuration
  68. /// value that is to be returned
  69. isc::data::ConstElementPtr getValue(bool& is_default,
  70. const std::string& identifier) const;
  71. /// Returns the ModuleSpec associated with this ConfigData object
  72. const ModuleSpec& getModuleSpec() const { return (_module_spec); }
  73. /// Set the ModuleSpec associated with this ConfigData object
  74. void setModuleSpec(ModuleSpec module_spec) { _module_spec = module_spec; };
  75. /// Set the local configuration (i.e. all non-default values)
  76. /// \param config An ElementPtr pointing to a MapElement containing
  77. /// *all* non-default configuration values. Existing values
  78. /// will be removed.
  79. void setLocalConfig(isc::data::ElementPtr config) { _config = config; }
  80. /// Returns the local (i.e. non-default) configuration.
  81. /// \return An ElementPtr pointing to a MapElement containing all
  82. /// non-default configuration options.
  83. isc::data::ElementPtr getLocalConfig() { return (_config); }
  84. /// Returns a list of all possible configuration options as specified
  85. /// by the ModuleSpec.
  86. /// \param identifier If given, show the items at the given identifier
  87. /// (iff that is also a MapElement)
  88. /// \param recurse If true, child MapElements will be traversed to
  89. /// add their identifiers to the result list
  90. /// \return An ElementPtr pointing to a ListElement containing
  91. /// StringElements that specify the identifiers at the given
  92. /// location (or all possible identifiers if identifier==""
  93. /// and recurse==false)
  94. isc::data::ConstElementPtr getItemList(const std::string& identifier = "",
  95. bool recurse = false) const;
  96. /// Returns a map of the top-level configuration items, as currently
  97. /// set or their defaults
  98. ///
  99. /// \return An ElementPtr pointing to a MapElement containing
  100. /// the top-level configuration items
  101. isc::data::ConstElementPtr getFullConfig() const;
  102. private:
  103. isc::data::ElementPtr _config;
  104. ModuleSpec _module_spec;
  105. };
  106. }
  107. }
  108. #endif
  109. // Local Variables:
  110. // mode: c++
  111. // End: