CatapultServer  v0.5.0.1 (Elephant)
ConfigurationBag.h
Go to the documentation of this file.
1 
21 #pragma once
23 #include "catapult/exceptions.h"
24 #include <string>
25 #include <unordered_map>
26 #include <unordered_set>
27 #include <vector>
28 
29 namespace catapult { namespace utils {
30 
33  public:
35  };
36 
39  public:
41  };
42 
46  constexpr ConfigurationKey(const char* section, const char* name) : Section(section), Name(name)
47  {}
48 
50  const char* Section;
51 
53  const char* Name;
54  };
55 
58  public:
60  template<typename TValue>
61  using OrderedKeyValueMap = std::vector<std::pair<std::string, TValue>>;
62 
64  template<typename TValue>
65  using UnorderedKeyValueMap = std::unordered_map<std::string, TValue>;
66 
68  using ValuesContainer = std::unordered_map<std::string, OrderedKeyValueMap<std::string>>;
69 
70  public:
73  {}
74 
75  public:
77  static ConfigurationBag FromStream(std::istream& input);
78 
80  static ConfigurationBag FromPath(const std::string& path);
81 
82  public:
84  size_t size() const {
85  size_t count = 0;
86  for (const auto& section : m_values)
87  count += section.second.size();
88 
89  return count;
90  }
91 
93  size_t size(const char* section) const {
94  auto sectionIter = m_values.find(section);
95  return m_values.cend() == sectionIter ? 0 : sectionIter->second.size();
96  }
97 
99  bool contains(const ConfigurationKey& key) const {
100  return !!lookup(key);
101  }
102 
104  std::unordered_set<std::string> sections() const {
105  std::unordered_set<std::string> sections;
106  for (const auto& pair : m_values)
107  sections.insert(pair.first);
108 
109  return sections;
110  }
111 
114  template<typename T>
115  bool tryGet(const ConfigurationKey& key, T& value) const {
116  auto pUnparsedValue = lookup(key);
117  if (!pUnparsedValue)
118  return false;
119 
120  if (!TryParseValue(*pUnparsedValue, value)) {
121  auto message = "property could not be parsed";
122  CATAPULT_THROW_AND_LOG_2(property_malformed_error, message, std::string(key.Section), std::string(key.Name));
123  }
124 
125  return true;
126  }
127 
129  template<typename T>
130  T get(const ConfigurationKey& key) const {
131  T value;
132  if (!tryGet(key, value)) {
133  auto message = "property not found";
134  CATAPULT_THROW_AND_LOG_2(property_not_found_error, message, std::string(key.Section), std::string(key.Name));
135  }
136 
137  return value;
138  }
139 
141  template<typename T>
142  UnorderedKeyValueMap<T> getAll(const char* section) const {
143  auto values = getAllOrdered<T>(section);
144 
145  UnorderedKeyValueMap<T> unorderedValues;
146  for (const auto& value : values)
147  unorderedValues.emplace(value);
148 
149  return unorderedValues;
150  }
151 
153  template<typename T>
154  OrderedKeyValueMap<T> getAllOrdered(const char* section) const {
155  OrderedKeyValueMap<T> values;
156 
157  auto sectionIter = m_values.find(section);
158  if (m_values.cend() == sectionIter)
159  return values;
160 
161  for (const auto& pair : sectionIter->second)
162  values.emplace_back(pair.first, get<T>(ConfigurationKey(section, pair.first.c_str())));
163 
164  return values;
165  }
166 
167  private:
168  const std::string* lookup(const ConfigurationKey& key) const {
169  auto sectionIter = m_values.find(key.Section);
170  if (m_values.cend() == sectionIter)
171  return nullptr;
172 
173  const auto& sectionValues = sectionIter->second;
174  auto itemIter = std::find_if(sectionValues.cbegin(), sectionValues.cend(), [&name = key.Name](const auto& pair) {
175  return name == pair.first;
176  });
177 
178  if (sectionValues.cend() == itemIter)
179  return nullptr;
180 
181  return &itemIter->second;
182  }
183 
184  private:
186  };
187 }}
catapult::utils::ConfigurationBag::size
size_t size(const char *section) const
Returns the number of section properties in this bag.
Definition: ConfigurationBag.h:93
catapult::utils::ConfigurationBag::UnorderedKeyValueMap
std::unordered_map< std::string, TValue > UnorderedKeyValueMap
A strongly typed unordered key to value map.
Definition: ConfigurationBag.h:65
exceptions.h
catapult::utils::ConfigurationKey::Section
const char * Section
Section containing the key.
Definition: ConfigurationBag.h:50
catapult::utils::ConfigurationBag::lookup
const std::string * lookup(const ConfigurationKey &key) const
Definition: ConfigurationBag.h:168
CATAPULT_THROW_AND_LOG_2
#define CATAPULT_THROW_AND_LOG_2(TYPE, MESSAGE, PARAM1, PARAM2)
Macro used to throw a catapult exception with two parameters.
Definition: exceptions.h:159
catapult::utils::ConfigurationKey::ConfigurationKey
constexpr ConfigurationKey(const char *section, const char *name)
Creates a configuration key for a key with name in section.
Definition: ConfigurationBag.h:46
catapult::utils::ConfigurationKey::Name
const char * Name
Key name.
Definition: ConfigurationBag.h:53
catapult::utils::ConfigurationBag
A simple bag of properties.
Definition: ConfigurationBag.h:57
catapult::utils::ConfigurationBag::sections
std::unordered_set< std::string > sections() const
Gets the names of all sections in this bag.
Definition: ConfigurationBag.h:104
catapult::utils::ConfigurationKey
A configuration key.
Definition: ConfigurationBag.h:44
catapult::utils::ConfigurationBag::ValuesContainer
std::unordered_map< std::string, OrderedKeyValueMap< std::string > > ValuesContainer
Underlying container that a configuration bag is created around.
Definition: ConfigurationBag.h:68
catapult::utils::ConfigurationBag::ConfigurationBag
ConfigurationBag(ValuesContainer &&values)
Creates a new configuration bag around values.
Definition: ConfigurationBag.h:72
catapult::utils::ConfigurationBag::OrderedKeyValueMap
std::vector< std::pair< std::string, TValue > > OrderedKeyValueMap
A strongly typed ordered key to value map.
Definition: ConfigurationBag.h:61
catapult::catapult_runtime_error
catapult_error< std::runtime_error > catapult_runtime_error
Definition: exceptions.h:87
catapult::utils::ConfigurationBag::get
T get(const ConfigurationKey &key) const
Gets the property identified by key from this bag.
Definition: ConfigurationBag.h:130
catapult::utils::ConfigurationBag::FromPath
static ConfigurationBag FromPath(const std::string &path)
Loads a configuration bag from the specified path.
Definition: ConfigurationBag.cpp:45
catapult::utils::ConfigurationBag::FromStream
static ConfigurationBag FromStream(std::istream &input)
Loads a configuration bag from the specified stream (input).
Definition: ConfigurationBag.cpp:29
catapult::utils::property_not_found_error
Exception class that is thrown when a required configuration property is missing.
Definition: ConfigurationBag.h:32
ConfigurationBag.h
ConfigurationValueParsers.h
VISIBLE_EXCEPTION_ATTRIBUTE
#define VISIBLE_EXCEPTION_ATTRIBUTE
Definition: exceptions.h:35
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::ConfigurationBag::tryGet
bool tryGet(const ConfigurationKey &key, T &value) const
Definition: ConfigurationBag.h:115
catapult::catapult_error
Base class for all catapult exceptions that derives from both std::exception and boost::exception.
Definition: exceptions.h:42
catapult::utils::property_malformed_error
Exception class that is thrown when a configuration property is malformed.
Definition: ConfigurationBag.h:38
catapult::utils::ConfigurationBag::contains
bool contains(const ConfigurationKey &key) const
Returns true if the property identified by key is contained in this bag.
Definition: ConfigurationBag.h:99
catapult::catapult_invalid_argument
catapult_error< std::invalid_argument > catapult_invalid_argument
Definition: exceptions.h:88
catapult::utils::TryParseValue
bool TryParseValue(const std::string &str, LogLevel &parsedValue)
Tries to parse str into a log level (parsedValue).
Definition: ConfigurationValueParsers.cpp:60
catapult::utils::ConfigurationBag::getAllOrdered
OrderedKeyValueMap< T > getAllOrdered(const char *section) const
Gets all section properties from this bag preserving source order.
Definition: ConfigurationBag.h:154
catapult::utils::ConfigurationBag::size
size_t size() const
Returns the number of properties in this bag.
Definition: ConfigurationBag.h:84
catapult::utils::ConfigurationBag::m_values
ValuesContainer m_values
Definition: ConfigurationBag.h:185
catapult::utils::ConfigurationBag::getAll
UnorderedKeyValueMap< T > getAll(const char *section) const
Gets all section properties from this bag.
Definition: ConfigurationBag.h:142