config_data.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // $Id$
  15. #ifndef __CONFIG_DATA_H
  16. #define __CONFIG_DATA_H 1
  17. #include <string>
  18. #include <vector>
  19. #include <config/module_spec.h>
  20. #include <exceptions/exceptions.h>
  21. namespace isc {
  22. namespace config {
  23. /// This exception is thrown when the caller is trying to access
  24. /// data that doesn't exist (i.e. with an identifier that does not
  25. /// point to anything defined in the .spec file)
  26. class DataNotFoundError : public isc::Exception {
  27. public:
  28. DataNotFoundError(const char* file, size_t line, const std::string& what) :
  29. isc::Exception(file, line, what) {}
  30. };
  31. class ConfigData {
  32. public:
  33. /// Constructs a ConfigData option with no specification and an
  34. /// empty configuration.
  35. ConfigData() { _config = Element::createMap(); };
  36. /// Constructs a ConfigData option with the given specification
  37. /// and an empty configuration.
  38. /// \param module_spec A ModuleSpec for the relevant module
  39. ConfigData(const ModuleSpec& module_spec) : _module_spec(module_spec) { _config = Element::createMap(); }
  40. virtual ~ConfigData() {};
  41. /// Returns the value currently set for the given identifier
  42. /// If no value is set, the default value (as specified by the
  43. /// .spec file) is returned. If there is no value and no default,
  44. /// an empty ElementPtr is returned.
  45. /// Raises a DataNotFoundError if the identifier is bad.
  46. /// \param identifier The identifier pointing to the configuration
  47. /// value that is to be returned
  48. ElementPtr getValue(const std::string& identifier);
  49. /// Returns the value currently set for the given identifier
  50. /// If no value is set, the default value (as specified by the
  51. /// .spec file) is returned. If there is no value and no default,
  52. /// an empty ElementPtr is returned.
  53. /// Raises a DataNotFoundError if the identifier is bad.
  54. /// \param is_default will be set to true if the value is taken
  55. /// from the specifications item_default setting,
  56. /// false otherwise
  57. /// \param identifier The identifier pointing to the configuration
  58. /// value that is to be returned
  59. ElementPtr getValue(bool &is_default, const std::string& identifier);
  60. /// Returns the ModuleSpec associated with this ConfigData object
  61. const ModuleSpec getModuleSpec() { return (_module_spec); }
  62. /// Set the ModuleSpec associated with this ConfigData object
  63. void setModuleSpec(ModuleSpec module_spec) { _module_spec = module_spec; };
  64. /// Set the local configuration (i.e. all non-default values)
  65. /// \param config An ElementPtr pointing to a MapElement containing
  66. /// *all* non-default configuration values. Existing values
  67. /// will be removed.
  68. void setLocalConfig(ElementPtr config) { _config = config; }
  69. /// Returns the local (i.e. non-default) configuration.
  70. /// \returns An ElementPtr pointing to a MapElement containing all
  71. /// non-default configuration options.
  72. ElementPtr getLocalConfig() { return (_config); }
  73. /// Returns a list of all possible configuration options as specified
  74. /// by the ModuleSpec.
  75. /// \param identifier If given, show the items at the given identifier
  76. /// (iff that is also a MapElement)
  77. /// \param recurse If true, child MapElements will be traversed to
  78. /// add their identifiers to the result list
  79. /// \return An ElementPtr pointing to a ListElement containing
  80. /// StringElements that specify the identifiers at the given
  81. /// location (or all possible identifiers if identifier==""
  82. /// and recurse==false)
  83. ElementPtr getItemList(const std::string& identifier = "", bool recurse = false);
  84. /// Returns all current configuration settings (both non-default and default).
  85. /// \return An ElementPtr pointing to a MapElement containing
  86. /// string->value elements, where the string is the
  87. /// full identifier of the configuration option and the
  88. /// value is an ElementPtr with the value.
  89. ElementPtr getFullConfig();
  90. private:
  91. ElementPtr _config;
  92. ModuleSpec _module_spec;
  93. };
  94. }
  95. }
  96. #endif