configuration.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "common/uri.h"
  21. #include <string>
  22. #include <map>
  23. #include <vector>
  24. #include <set>
  25. #include <istream>
  26. #include <stdint.h>
  27. #include <optional.hpp>
  28. namespace hdfs {
  29. template <class T>
  30. using optional = std::experimental::optional<T>;
  31. /**
  32. * Configuration class that parses XML.
  33. *
  34. * Files should be an XML file of the form
  35. * <configuration>
  36. * <property>
  37. * <name>Name</name>
  38. * <value>Value</value>
  39. * </property>
  40. * <configuration>
  41. *
  42. * Configuration objects should be created via the ConfigurationLoader class.
  43. * Configuration objects are immutable and can be shared between threads.
  44. *
  45. * This class is thread-safe.
  46. */
  47. class Configuration {
  48. public:
  49. // Gets values
  50. std::string GetWithDefault(const std::string &key,
  51. const std::string &default_value) const;
  52. optional<std::string> Get(const std::string &key) const;
  53. int64_t GetIntWithDefault(const std::string &key,
  54. int64_t default_value) const;
  55. optional<int64_t> GetInt(const std::string &key) const;
  56. double GetDoubleWithDefault(const std::string &key,
  57. double default_value) const;
  58. optional<double> GetDouble(const std::string &key) const;
  59. bool GetBoolWithDefault(const std::string &key,
  60. bool default_value) const;
  61. optional<bool> GetBool(const std::string &key) const;
  62. URI GetUriWithDefault(const std::string &key,
  63. std::string default_value) const;
  64. optional<URI> GetUri(const std::string &key) const;
  65. protected:
  66. friend class ConfigurationLoader;
  67. /* Transparent data holder for property values */
  68. struct ConfigData {
  69. std::string value;
  70. bool final;
  71. ConfigData() : final(false){};
  72. ConfigData(const std::string &value) : value(value), final(false) {}
  73. void operator=(const std::string &new_value) {
  74. value = new_value;
  75. final = false;
  76. }
  77. };
  78. typedef std::map<std::string, ConfigData> ConfigMap;
  79. Configuration() {};
  80. Configuration(ConfigMap &src_map) : raw_values_(src_map){};
  81. Configuration(const ConfigMap &src_map) : raw_values_(src_map){};
  82. static std::vector<std::string> GetDefaultFilenames();
  83. // While we want this to be const, it would preclude copying Configuration
  84. // objects. The Configuration class must not allow any mutations of
  85. // the raw_values
  86. ConfigMap raw_values_;
  87. static std::string fixCase(const std::string &in) {
  88. std::string result(in);
  89. for (auto & c: result) c = toupper(c);
  90. return result;
  91. }
  92. };
  93. }
  94. #endif