Commit 435cfae4 authored by Dominik Charousset's avatar Dominik Charousset

Use dictionary<T> instead of std::map<string, T>

parent f77c7091
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "caf/config_option.hpp" #include "caf/config_option.hpp"
#include "caf/config_option_set.hpp" #include "caf/config_option_set.hpp"
#include "caf/config_value.hpp" #include "caf/config_value.hpp"
#include "caf/dictionary.hpp"
#include "caf/fwd.hpp" #include "caf/fwd.hpp"
#include "caf/is_typed_actor.hpp" #include "caf/is_typed_actor.hpp"
#include "caf/named_actor_config.hpp" #include "caf/named_actor_config.hpp"
...@@ -79,7 +80,7 @@ public: ...@@ -79,7 +80,7 @@ public:
using group_module_factory_vector = std::vector<group_module_factory>; using group_module_factory_vector = std::vector<group_module_factory>;
using config_map = std::map<std::string, config_value::dictionary>; using config_map = dictionary<config_value::dictionary>;
using string_list = std::vector<std::string>; using string_list = std::vector<std::string>;
...@@ -101,7 +102,7 @@ public: ...@@ -101,7 +102,7 @@ public:
// -- properties ------------------------------------------------------------- // -- properties -------------------------------------------------------------
/// @private /// @private
std::map<std::string, config_value::dictionary> content; dictionary<config_value::dictionary> content;
/// Sets a config by using its INI name `config_name` to `config_value`. /// Sets a config by using its INI name `config_name` to `config_value`.
template <class T> template <class T>
......
...@@ -54,10 +54,10 @@ public: ...@@ -54,10 +54,10 @@ public:
using iterator = option_vector::iterator; using iterator = option_vector::iterator;
/// Maps string keys to arbitrary (config) values. /// Maps string keys to arbitrary (config) values.
using dictionary = std::map<std::string, config_value>; using dictionary = dictionary<config_value>;
/// Categorized settings. /// Categorized settings.
using config_map = std::map<std::string, dictionary>; using config_map = caf::dictionary<dictionary>;
// -- constructors, destructors, and assignment operators -------------------- // -- constructors, destructors, and assignment operators --------------------
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include "caf/atom.hpp" #include "caf/atom.hpp"
#include "caf/detail/bounds_checker.hpp" #include "caf/detail/bounds_checker.hpp"
#include "caf/detail/type_traits.hpp" #include "caf/detail/type_traits.hpp"
#include "caf/dictionary.hpp"
#include "caf/fwd.hpp" #include "caf/fwd.hpp"
#include "caf/optional.hpp" #include "caf/optional.hpp"
#include "caf/string_algorithms.hpp" #include "caf/string_algorithms.hpp"
...@@ -59,7 +60,7 @@ public: ...@@ -59,7 +60,7 @@ public:
using list = std::vector<config_value>; using list = std::vector<config_value>;
using dictionary = std::map<std::string, config_value>; using dictionary = dictionary<config_value>;
using types = detail::type_list<integer, boolean, real, atom, timespan, using types = detail::type_list<integer, boolean, real, atom, timespan,
string, list, dictionary>; string, list, dictionary>;
...@@ -175,7 +176,7 @@ private: ...@@ -175,7 +176,7 @@ private:
/// Organizes config values into categories. /// Organizes config values into categories.
/// @relates config_value /// @relates config_value
using config_value_map = std::map<std::string, config_value::dictionary>; using config_value_map = dictionary<config_value::dictionary>;
// -- SumType-like access ------------------------------------------------------ // -- SumType-like access ------------------------------------------------------
...@@ -351,12 +352,12 @@ struct config_value_access<std::vector<T>> { ...@@ -351,12 +352,12 @@ struct config_value_access<std::vector<T>> {
} }
}; };
/// Implements automagic unboxing of `std::map<std::string, V>` from a /// Implements automagic unboxing of `dictionary<V>` from a homogeneous
/// homogeneous `config_value::dictionary`. /// `config_value::dictionary`.
/// @relates config_value /// @relates config_value
template <class V> template <class V>
struct config_value_access<std::map<std::string, V>> { struct config_value_access<dictionary<V>> {
using map_type = std::map<std::string, V>; using map_type = dictionary<V>;
using kvp = std::pair<const std::string, config_value>; using kvp = std::pair<const std::string, config_value>;
...@@ -401,10 +402,9 @@ struct config_value_access<std::map<std::string, V>> { ...@@ -401,10 +402,9 @@ struct config_value_access<std::map<std::string, V>> {
/// Tries to retrieve the value associated to `name` from `xs`. /// Tries to retrieve the value associated to `name` from `xs`.
/// @relates config_value /// @relates config_value
template <class T> template <class T>
optional<T> get_if(const config_value::dictionary* xs, optional<T> get_if(const config_value::dictionary* xs, string_view name) {
const std::string& name) {
// Split the name into a path. // Split the name into a path.
std::vector<std::string> path; std::vector<string_view> path;
split(path, name, "."); split(path, name, ".");
// Sanity check. // Sanity check.
if (path.size() == 0) if (path.size() == 0)
...@@ -433,7 +433,7 @@ optional<T> get_if(const config_value::dictionary* xs, ...@@ -433,7 +433,7 @@ optional<T> get_if(const config_value::dictionary* xs,
/// Retrieves the value associated to `name` from `xs`. /// Retrieves the value associated to `name` from `xs`.
/// @relates config_value /// @relates config_value
template <class T> template <class T>
T get(const config_value::dictionary& xs, const std::string& name) { T get(const config_value::dictionary& xs, string_view name) {
auto result = get_if<T>(&xs, name); auto result = get_if<T>(&xs, name);
if (result) if (result)
return std::move(*result); return std::move(*result);
...@@ -444,7 +444,7 @@ T get(const config_value::dictionary& xs, const std::string& name) { ...@@ -444,7 +444,7 @@ T get(const config_value::dictionary& xs, const std::string& name) {
/// `default_value`. /// `default_value`.
/// @relates config_value /// @relates config_value
template <class T, class E = detail::enable_if_t<!std::is_pointer<T>::value>> template <class T, class E = detail::enable_if_t<!std::is_pointer<T>::value>>
T get_or(const config_value::dictionary& xs, const std::string& name, T get_or(const config_value::dictionary& xs, string_view name,
const T& default_value) { const T& default_value) {
auto result = get_if<T>(&xs, name); auto result = get_if<T>(&xs, name);
if (result) if (result)
...@@ -455,13 +455,13 @@ T get_or(const config_value::dictionary& xs, const std::string& name, ...@@ -455,13 +455,13 @@ T get_or(const config_value::dictionary& xs, const std::string& name,
/// Retrieves the value associated to `name` from `xs` or returns /// Retrieves the value associated to `name` from `xs` or returns
/// `default_value`. /// `default_value`.
/// @relates config_value /// @relates config_value
std::string get_or(const config_value::dictionary& xs, const std::string& name, std::string get_or(const config_value::dictionary& xs, string_view name,
const char* default_value); const char* default_value);
/// Tries to retrieve the value associated to `name` from `xs`. /// Tries to retrieve the value associated to `name` from `xs`.
/// @relates config_value /// @relates config_value
template <class T> template <class T>
optional<T> get_if(const config_value_map* xs, const std::string& name) { optional<T> get_if(const config_value_map* xs, string_view name) {
// Get the category. // Get the category.
auto pos = name.find('.'); auto pos = name.find('.');
if (pos == std::string::npos) { if (pos == std::string::npos) {
...@@ -479,7 +479,7 @@ optional<T> get_if(const config_value_map* xs, const std::string& name) { ...@@ -479,7 +479,7 @@ optional<T> get_if(const config_value_map* xs, const std::string& name) {
/// Retrieves the value associated to `name` from `xs`. /// Retrieves the value associated to `name` from `xs`.
/// @relates config_value /// @relates config_value
template <class T> template <class T>
T get(const config_value_map& xs, const std::string& name) { T get(const config_value_map& xs, string_view name) {
auto result = get_if<T>(&xs, name); auto result = get_if<T>(&xs, name);
if (result) if (result)
return std::move(*result); return std::move(*result);
...@@ -490,8 +490,7 @@ T get(const config_value_map& xs, const std::string& name) { ...@@ -490,8 +490,7 @@ T get(const config_value_map& xs, const std::string& name) {
/// `default_value`. /// `default_value`.
/// @relates config_value /// @relates config_value
template <class T, class E = detail::enable_if_t<!std::is_pointer<T>::value>> template <class T, class E = detail::enable_if_t<!std::is_pointer<T>::value>>
T get_or(const config_value_map& xs, const std::string& name, T get_or(const config_value_map& xs, string_view name, const T& default_value) {
const T& default_value) {
auto result = get_if<T>(&xs, name); auto result = get_if<T>(&xs, name);
if (result) if (result)
return std::move(*result); return std::move(*result);
...@@ -501,20 +500,20 @@ T get_or(const config_value_map& xs, const std::string& name, ...@@ -501,20 +500,20 @@ T get_or(const config_value_map& xs, const std::string& name,
/// Retrieves the value associated to `name` from `xs` or returns /// Retrieves the value associated to `name` from `xs` or returns
/// `default_value`. /// `default_value`.
/// @relates config_value /// @relates config_value
std::string get_or(const config_value_map& xs, const std::string& name, std::string get_or(const config_value_map& xs, string_view name,
const char* default_value); const char* default_value);
/// Tries to retrieve the value associated to `name` from `cfg`. /// Tries to retrieve the value associated to `name` from `cfg`.
/// @relates config_value /// @relates config_value
template <class T> template <class T>
optional<T> get_if(const actor_system_config* cfg, const std::string& name) { optional<T> get_if(const actor_system_config* cfg, string_view name) {
return get_if<T>(&content(*cfg), name); return get_if<T>(&content(*cfg), name);
} }
/// Retrieves the value associated to `name` from `cfg`. /// Retrieves the value associated to `name` from `cfg`.
/// @relates config_value /// @relates config_value
template <class T> template <class T>
T get(const actor_system_config& cfg, const std::string& name) { T get(const actor_system_config& cfg, string_view name) {
return get<T>(content(cfg), name); return get<T>(content(cfg), name);
} }
...@@ -522,12 +521,12 @@ T get(const actor_system_config& cfg, const std::string& name) { ...@@ -522,12 +521,12 @@ T get(const actor_system_config& cfg, const std::string& name) {
/// `default_value`. /// `default_value`.
/// @relates config_value /// @relates config_value
template <class T, class E = detail::enable_if_t<!std::is_pointer<T>::value>> template <class T, class E = detail::enable_if_t<!std::is_pointer<T>::value>>
T get_or(const actor_system_config& cfg, const std::string& name, T get_or(const actor_system_config& cfg, string_view name,
const T& default_value) { const T& default_value) {
return get_or(content(cfg), name, default_value); return get_or(content(cfg), name, default_value);
} }
std::string get_or(const actor_system_config& cfg, const std::string& name, std::string get_or(const actor_system_config& cfg, string_view name,
const char* default_value); const char* default_value);
/// @relates config_value /// @relates config_value
bool operator<(const config_value& x, const config_value& y); bool operator<(const config_value& x, const config_value& y);
......
...@@ -18,11 +18,11 @@ ...@@ -18,11 +18,11 @@
#pragma once #pragma once
#include <map>
#include <string> #include <string>
#include "caf/config_option_set.hpp" #include "caf/config_option_set.hpp"
#include "caf/config_value.hpp" #include "caf/config_value.hpp"
#include "caf/dictionary.hpp"
namespace caf { namespace caf {
namespace detail { namespace detail {
...@@ -183,7 +183,7 @@ public: ...@@ -183,7 +183,7 @@ public:
using super = abstract_ini_consumer; using super = abstract_ini_consumer;
using config_map = std::map<std::string, config_value::dictionary>; using config_map = dictionary<config_value::dictionary>;
// -- constructors, destructors, and assignment operators -------------------- // -- constructors, destructors, and assignment operators --------------------
......
...@@ -18,11 +18,11 @@ ...@@ -18,11 +18,11 @@
#pragma once #pragma once
#include <map>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include <vector> #include <vector>
#include "caf/dictionary.hpp"
#include "caf/fwd.hpp" #include "caf/fwd.hpp"
#include "caf/timestamp.hpp" #include "caf/timestamp.hpp"
...@@ -127,7 +127,7 @@ struct type_name_builder<std::vector<T>, false> { ...@@ -127,7 +127,7 @@ struct type_name_builder<std::vector<T>, false> {
}; };
template <class T> template <class T>
struct type_name_builder<std::map<std::string, T>, false> { struct type_name_builder<dictionary<T>, false> {
void operator()(std::string& result) const { void operator()(std::string& result) const {
result += "dictionary of "; result += "dictionary of ";
type_name_builder<T> g; type_name_builder<T> g;
......
...@@ -32,6 +32,7 @@ namespace caf { ...@@ -32,6 +32,7 @@ namespace caf {
// -- 1 param templates -------------------------------------------------------- // -- 1 param templates --------------------------------------------------------
template <class> class behavior_type_of; template <class> class behavior_type_of;
template <class> class dictionary;
template <class> class downstream; template <class> class downstream;
template <class> class expected; template <class> class expected;
template <class> class intrusive_ptr; template <class> class intrusive_ptr;
...@@ -162,8 +163,7 @@ using stream_slot = uint16_t; ...@@ -162,8 +163,7 @@ using stream_slot = uint16_t;
// -- functions ---------------------------------------------------------------- // -- functions ----------------------------------------------------------------
/// @relates actor_system_config /// @relates actor_system_config
const std::map<std::string, std::map<std::string, config_value>>& const dictionary<dictionary<config_value>>& content(const actor_system_config&);
content(const actor_system_config&);
// -- intrusive containers ----------------------------------------------------- // -- intrusive containers -----------------------------------------------------
......
...@@ -453,7 +453,7 @@ void actor_system_config::extract_config_file_path(string_list& args) { ...@@ -453,7 +453,7 @@ void actor_system_config::extract_config_file_path(string_list& args) {
args.erase(i); args.erase(i);
} }
const std::map<std::string, std::map<std::string, config_value>>& const dictionary<dictionary<config_value>>&
content(const actor_system_config& cfg) { content(const actor_system_config& cfg) {
return cfg.content; return cfg.content;
} }
......
...@@ -132,15 +132,15 @@ std::string get_or(const config_value::dictionary& xs, const std::string& name, ...@@ -132,15 +132,15 @@ std::string get_or(const config_value::dictionary& xs, const std::string& name,
return default_value; return default_value;
} }
std::string get_or(const std::map<std::string, config_value::dictionary>& xs, std::string get_or(const dictionary<config_value::dictionary>& xs,
const std::string& name, const char* default_value) { string_view name, const char* default_value) {
auto result = get_if<std::string>(&xs, name); auto result = get_if<std::string>(&xs, name);
if (result) if (result)
return std::move(*result); return std::move(*result);
return default_value; return default_value;
} }
std::string get_or(const actor_system_config& cfg, const std::string& name, std::string get_or(const actor_system_config& cfg, string_view name,
const char* default_value) { const char* default_value) {
return get_or(content(cfg), name, default_value); return get_or(content(cfg), name, default_value);
} }
......
...@@ -62,7 +62,7 @@ CAF_TEST(lookup) { ...@@ -62,7 +62,7 @@ CAF_TEST(lookup) {
CAF_TEST(parse with ref syncing) { CAF_TEST(parse with ref syncing) {
using ls = vector<string>; // list of strings using ls = vector<string>; // list of strings
using ds = std::map<string, string>; // dictionary of strings using ds = dictionary<string>; // dictionary of strings
auto foo_i = 0; auto foo_i = 0;
auto foo_f = 0.f; auto foo_f = 0.f;
auto foo_b = false; auto foo_b = false;
...@@ -74,8 +74,8 @@ CAF_TEST(parse with ref syncing) { ...@@ -74,8 +74,8 @@ CAF_TEST(parse with ref syncing) {
.add<bool>(foo_b, "foo", "b,b", "") .add<bool>(foo_b, "foo", "b,b", "")
.add<string>(bar_s, "bar", "s,s", "") .add<string>(bar_s, "bar", "s,s", "")
.add<vector<string>>(bar_l, "bar", "l,l", "") .add<vector<string>>(bar_l, "bar", "l,l", "")
.add<std::map<string, string>>(bar_d, "bar", "d,d", ""); .add<dictionary<string>>(bar_d, "bar", "d,d", "");
std::map<std::string, config_value::dictionary> cfg; dictionary<config_value::dictionary> cfg;
vector<string> args{"-i42", vector<string> args{"-i42",
"-f", "-f",
"1e12", "1e12",
...@@ -103,7 +103,7 @@ CAF_TEST(parse with ref syncing) { ...@@ -103,7 +103,7 @@ CAF_TEST(parse with ref syncing) {
CAF_TEST(implicit global) { CAF_TEST(implicit global) {
opts.add<int>("global", "value").add<bool>("global", "help"); opts.add<int>("global", "value").add<bool>("global", "help");
CAF_MESSAGE("test long option with argument"); CAF_MESSAGE("test long option with argument");
std::map<std::string, config_value::dictionary> cfg; dictionary<config_value::dictionary> cfg;
auto res = opts.parse(cfg, {"--value=42"}); auto res = opts.parse(cfg, {"--value=42"});
CAF_CHECK_EQUAL(res.first, pec::success); CAF_CHECK_EQUAL(res.first, pec::success);
CAF_CHECK_EQUAL(get_if<int>(&cfg, "global.value"), 42); CAF_CHECK_EQUAL(get_if<int>(&cfg, "global.value"), 42);
......
...@@ -31,8 +31,10 @@ ...@@ -31,8 +31,10 @@
#include "caf/none.hpp" #include "caf/none.hpp"
#include "caf/pec.hpp" #include "caf/pec.hpp"
#include "caf/variant.hpp" #include "caf/variant.hpp"
#include "caf/string_view.hpp"
using std::string;
using namespace std;
using namespace caf; using namespace caf;
namespace { namespace {
...@@ -44,7 +46,7 @@ using dictionary = config_value::dictionary; ...@@ -44,7 +46,7 @@ using dictionary = config_value::dictionary;
struct dictionary_builder { struct dictionary_builder {
dictionary dict; dictionary dict;
dictionary_builder&& add(const char* key, config_value value) && { dictionary_builder&& add(string_view key, config_value value) && {
dict.emplace(key, std::move(value)); dict.emplace(key, std::move(value));
return std::move(*this); return std::move(*this);
} }
...@@ -69,8 +71,8 @@ config_value cfg_lst(Ts&&... xs) { ...@@ -69,8 +71,8 @@ config_value cfg_lst(Ts&&... xs) {
} }
// TODO: switch to std::operator""s when switching to C++14 // TODO: switch to std::operator""s when switching to C++14
std::string operator"" _s(const char* str, size_t size) { string operator"" _s(const char* str, size_t size) {
return std::string(str, size); return string(str, size);
} }
} // namespace <anonymous> } // namespace <anonymous>
...@@ -158,7 +160,7 @@ CAF_TEST(append) { ...@@ -158,7 +160,7 @@ CAF_TEST(append) {
} }
CAF_TEST(homogeneous dictionary) { CAF_TEST(homogeneous dictionary) {
using integer_map = std::map<std::string, int64_t>; using integer_map = caf::dictionary<int64_t>;
auto xs = dict() auto xs = dict()
.add("value-1", config_value{100000}) .add("value-1", config_value{100000})
.add("value-2", config_value{2}) .add("value-2", config_value{2})
...@@ -182,7 +184,7 @@ CAF_TEST(homogeneous dictionary) { ...@@ -182,7 +184,7 @@ CAF_TEST(homogeneous dictionary) {
} }
CAF_TEST(heterogeneous dictionary) { CAF_TEST(heterogeneous dictionary) {
using string_list = std::vector<std::string>; using string_list = std::vector<string>;
auto xs = dict() auto xs = dict()
.add("scheduler", dict() .add("scheduler", dict()
.add("policy", config_value{atom("none")}) .add("policy", config_value{atom("none")})
...@@ -206,7 +208,7 @@ CAF_TEST(successful parsing) { ...@@ -206,7 +208,7 @@ CAF_TEST(successful parsing) {
// references when comparing values. Since we call get<T>() on the result of // references when comparing values. Since we call get<T>() on the result of
// parse(), we would end up with a reference to a temporary. // parse(), we would end up with a reference to a temporary.
config_value parsed; config_value parsed;
auto parse = [&](const std::string& str) -> config_value& { auto parse = [&](const string& str) -> config_value& {
auto x = config_value::parse(str); auto x = config_value::parse(str);
if (!x) if (!x)
CAF_FAIL("cannot parse " << str << ": assumed a result but error " CAF_FAIL("cannot parse " << str << ": assumed a result but error "
...@@ -214,7 +216,7 @@ CAF_TEST(successful parsing) { ...@@ -214,7 +216,7 @@ CAF_TEST(successful parsing) {
parsed = std::move(*x); parsed = std::move(*x);
return parsed; return parsed;
}; };
using di = std::map<string, int>; // Dictionary-of-integers. using di = caf::dictionary<int>; // Dictionary-of-integers.
using ls = std::vector<string>; // List-of-strings. using ls = std::vector<string>; // List-of-strings.
using li = std::vector<int>; // List-of-integers. using li = std::vector<int>; // List-of-integers.
using lli = std::vector<li>; // List-of-list-of-integers. using lli = std::vector<li>; // List-of-list-of-integers.
...@@ -235,7 +237,7 @@ CAF_TEST(successful parsing) { ...@@ -235,7 +237,7 @@ CAF_TEST(successful parsing) {
} }
CAF_TEST(unsuccessful parsing) { CAF_TEST(unsuccessful parsing) {
auto parse = [](const std::string& str) { auto parse = [](const string& str) {
auto x = config_value::parse(str); auto x = config_value::parse(str);
if (x) if (x)
CAF_FAIL("assumed an error but got a result"); CAF_FAIL("assumed an error but got a result");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment