Go to the documentation of this file.
11 #ifndef __CONFIG_STORE_H___
12 #define __CONFIG_STORE_H___
21 template<
typename S1,
typename S2>
25 for (; i1 !=
end(s1) && i2 !=
end(s2); ++i1, ++i2) {
26 if (std::tolower(*i1) != std::tolower(*i2)) {
31 return i1 ==
end(s1) && i2 ==
end(s2);
36 for (
const char* test : {
"on",
"true",
"yes"}) {
46 for (
const char* test : {
"off",
"false",
"no"}) {
59 template<typename T, typename std::enable_if_t<std::is_integral<T>::value && not std::is_same<bool,T>::value,
bool> =
true>
61 template<typename T, typename std::enable_if_t<std::is_floating_point<T>::value,
bool> =
true>
67 template<typename T, typename std::enable_if_t<std::is_same<bool,T>::value,
bool> =
true>
104 ConfigStore(std::initializer_list<std::pair<std::string, ImplicitlyToString>> ilist)
107 for (
auto& kv : ilist) {
120 bool addString(std::string key, std::string value) {
return backing_store.emplace(std::move(key), std::move(value)).second; }
136 const std::string&
getString(
const std::string& key)
const {
139 }
catch (
const std::out_of_range& oor) {
140 throw std::out_of_range(
141 "ConfigStore accessed with non-existent key `" + key +
"'. Original exception's .what() = `" + oor.what() +
"'"
146 long long getInt(
const std::string& key)
const {
148 std::size_t final_pos = 0;
149 auto result = std::stoll(value, &final_pos);
150 if (final_pos != value.size()) {
156 double getReal(
const std::string& key)
const {
158 std::size_t final_pos = 0;
159 auto result = std::stod(value, &final_pos);
160 if (final_pos != value.size()) {
181 const std::string&
getStringOr(
const std::string& key,
const std::string& otherwise)
const {
183 else {
return otherwise; }
186 std::string
getStringOr(
const std::string& key, std::string&& otherwise)
const {
188 else {
return std::move(otherwise); }
191 long long getIntOr(
const std::string& key,
long long otherwise)
const {
193 else {
return otherwise; }
196 double getRealOr(
const std::string& key,
double otherwise)
const {
198 else {
return otherwise; }
201 bool getBoolOr(
const std::string& key,
bool otherwise)
const {
203 else {
return otherwise; }
209 bool hasKey(
const std::string& key)
const {
226 std::ptrdiff_t max_key_length = 0;
228 max_key_length = std::max(max_key_length, (std::ptrdiff_t)key_and_value.first.size());
233 os << std::setw(max_key_length + 2) << key_and_value.first <<
" = " << key_and_value.second <<
'\n';
241 return std::invalid_argument(
"value `" + value +
"' at key `" + key +
"' could not be completely converted");
258 for (
auto& kv : from) {
259 if (not into.
hasKey(kv.first)) {
throw std::out_of_range(
260 "attempted to override non-existent key " + kv.first +
" with value " + kv.second
287 for (
const auto& kv : cs) {
288 if (kv.first.substr(0, prefix.size()) == prefix) {
289 result.
setString(kv.first.substr(prefix.size()), kv.second);
300 for (
const auto& kv : cs) {
301 result.
setString(prefix + kv.first, kv.second);
310 template<
typename Filter>
313 for (
const auto& kv : cs) {
314 if (std::forward<Filter>(f)(kv.first)) {
324 template<
typename Filter>
327 for (
const auto& kv : cs) {
328 if (std::forward<Filter>(f)(kv.first, kv.second)) {
friend std::ostream & operator<<(std::ostream &os, const ConfigStore &cs)
bool addReal(std::string key, double value)
bool isTruthString(const S &s)
ConfigStore with_added(ConfigStore into, const ConfigStore &from1, const CSes &... froms)
void setString(std::string key, std::string value)
bool getBoolOr(const std::string &key, bool otherwise) const
ConfigStore & operator=(const ConfigStore &)=default
auto begin(const SingleItemImmutableSet< VertexID > &siis)
double getRealOr(const std::string &key, double otherwise) const
bool operator!=(const ConfigStore &rhs) const
void setInt(std::string key, long long value)
ConfigStore & override_all(ConfigStore &into, const ConfigStore &from)
bool addInt(std::string key, long long value)
bool equalsIgnoreCase(const S1 &s1, const S2 &s2)
const char * makeBoolString(bool v)
ImplicitlyToString(const T &b)
ConfigStore with_overridden(ConfigStore into, const ConfigStore &from1, const CSes &... froms)
std::invalid_argument makeIncompleteConversionException(const std::string &key, const std::string &value) const
const std::string & to_string(const OpGraphOpCode &opcode)
const std::string & getStringOr(const std::string &key, const std::string &otherwise) const
ConfigStore(std::initializer_list< std::pair< std::string, ImplicitlyToString >> ilist)
Flexible constructor that converts everything value it is given to a string.
bool hasKey(const std::string &key) const
bool addString(std::string key, std::string value)
ConfigStore & add_all(ConfigStore &into, const ConfigStore &from)
Add (or set) all keys from from in into.
void setBool(std::string key, bool value)
ImplicitlyToString(const T &number)
std::string getStringOr(const std::string &key, std::string &&otherwise) const
ImplicitlyToString(const char *s)
bool isFarceString(const S &s)
ConfigStore(BackingStore src)
auto filterKeys(const ConfigStore &cs, Filter &&f) -> decltype(f(std::string()), ConfigStore())
ImplicitlyToString(std::string s)
ConfigStore addPrefix(const ConfigStore &cs, const std::string &prefix)
auto end(const SingleItemImmutableSet< VertexID > &siis)
long long getIntOr(const std::string &key, long long otherwise) const
ConfigStore & set_all(ConfigStore &into, const ConfigStore &from)
bool operator==(const ConfigStore &rhs) const
BackingStore backing_store
const std::string & getString(const std::string &key) const
ConfigStore with_set(ConfigStore into, const ConfigStore &from1, const CSes &... froms)
bool addBool(std::string key, bool value)
ConfigStore getEntriesForPrefix(const ConfigStore &cs, const std::string &prefix)
long long getInt(const std::string &key) const
double getReal(const std::string &key) const
auto insert_or_assign(ASSOCIATIVE_COLLECTION &assoc_collection, KEY &&key, VALUE &&value)
void setReal(std::string key, double value)
bool getBool(const std::string &key) const
std::map< std::string, std::string > BackingStore