configuration.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef COMMON_CONFIGURATION_H_
  19. #define COMMON_CONFIGURATION_H_
  20. #include <string>
  21. #include <map>
  22. #include <vector>
  23. #include <set>
  24. #include <istream>
  25. #include <stdint.h>
  26. #include <optional.hpp>
  27. namespace hdfs {
  28. template <class T>
  29. using optional = std::experimental::optional<T>;
  30. /**
  31. * Configuration class that parses XML.
  32. *
  33. * Files should be an XML file of the form
  34. * <configuration>
  35. * <property>
  36. * <name>Name</name>
  37. * <value>Value</value>
  38. * </property>
  39. * <configuration>
  40. *
  41. * Configuration objects should be created via the ConfigurationLoader class.
  42. * Configuration objects are immutable and can be shared between threads.
  43. *
  44. * This class is thread-safe.
  45. */
  46. class Configuration {
  47. public:
  48. // Gets values
  49. std::string GetWithDefault(const std::string &key,
  50. const std::string &default_value) const;
  51. optional<std::string> Get(const std::string &key) const;
  52. int64_t GetIntWithDefault(const std::string &key,
  53. int64_t default_value) const;
  54. optional<int64_t> GetInt(const std::string &key) const;
  55. double GetDoubleWithDefault(const std::string &key,
  56. double default_value) const;
  57. optional<double> GetDouble(const std::string &key) const;
  58. bool GetBoolWithDefault(const std::string &key,
  59. bool default_value) const;
  60. optional<bool> GetBool(const std::string &key) const;
  61. protected:
  62. friend class ConfigurationLoader;
  63. /* Transparent data holder for property values */
  64. struct ConfigData {
  65. std::string value;
  66. bool final;
  67. ConfigData() : final(false){};
  68. ConfigData(const std::string &value) : value(value), final(false) {}
  69. void operator=(const std::string &new_value) {
  70. value = new_value;
  71. final = false;
  72. }
  73. };
  74. typedef std::map<std::string, ConfigData> ConfigMap;
  75. Configuration() {};
  76. Configuration(ConfigMap &src_map) : raw_values_(src_map){};
  77. static std::vector<std::string> GetDefaultFilenames();
  78. const ConfigMap raw_values_;
  79. };
  80. }
  81. #endif