Commit 0fbe9d15 authored by Dominik Charousset's avatar Dominik Charousset

Fix put overload resolution and return type

parent ddbdc9c3
...@@ -62,8 +62,9 @@ T get(const settings& xs, string_view name) { ...@@ -62,8 +62,9 @@ T get(const settings& xs, string_view name) {
return std::move(*result); return std::move(*result);
} }
template <class T, template <class T, class = typename std::enable_if<
class = typename std::enable_if<!std::is_pointer<T>::value>::type> !std::is_pointer<T>::value
&& !std::is_convertible<T, string_view>::value>::type>
T get_or(const settings& xs, string_view name, T default_value) { T get_or(const settings& xs, string_view name, T default_value) {
auto result = get_if<T>(&xs, name); auto result = get_if<T>(&xs, name);
if (result) if (result)
...@@ -75,21 +76,20 @@ std::string get_or(const settings& xs, string_view name, ...@@ -75,21 +76,20 @@ std::string get_or(const settings& xs, string_view name,
string_view default_value); string_view default_value);
/// @private /// @private
void put_impl(settings& dict, const std::vector<string_view>& path, config_value& put_impl(settings& dict, const std::vector<string_view>& path,
config_value& value); config_value& value);
/// @private /// @private
void put_impl(settings& dict, string_view key, config_value& put_impl(settings& dict, string_view key, config_value& value);
config_value& value);
/// Converts `value` to a `config_value` and assigns it to `key`. /// Converts `value` to a `config_value` and assigns it to `key`.
/// @param dict Dictionary of key-value pairs. /// @param dict Dictionary of key-value pairs.
/// @param key Human-readable nested keys in the form `category.key`. /// @param key Human-readable nested keys in the form `category.key`.
/// @param value New value for given `key`. /// @param value New value for given `key`.
template <class T> template <class T>
void put(settings& dict, string_view key, T&& value) { config_value& put(settings& dict, string_view key, T&& value) {
config_value tmp{std::forward<T>(value)}; config_value tmp{std::forward<T>(value)};
put_impl(dict, key, tmp); return put_impl(dict, key, tmp);
} }
/// Inserts a new list named `name` into the dictionary `xs` and returns /// Inserts a new list named `name` into the dictionary `xs` and returns
......
...@@ -28,11 +28,10 @@ std::string get_or(const settings& xs, string_view name, ...@@ -28,11 +28,10 @@ std::string get_or(const settings& xs, string_view name,
return std::string{default_value.begin(), default_value.end()}; return std::string{default_value.begin(), default_value.end()};
} }
void put_impl(settings& dict, const std::vector<string_view>& path, config_value& put_impl(settings& dict, const std::vector<string_view>& path,
config_value& value) { config_value& value) {
// Sanity check. // Sanity check.
if (path.empty()) CAF_ASSERT(!path.empty());
return;
// Navigate path. // Navigate path.
auto last = path.end(); auto last = path.end();
auto back = last - 1; auto back = last - 1;
...@@ -49,23 +48,26 @@ void put_impl(settings& dict, const std::vector<string_view>& path, ...@@ -49,23 +48,26 @@ void put_impl(settings& dict, const std::vector<string_view>& path,
} }
} }
// Set key-value pair on the leaf. // Set key-value pair on the leaf.
current->insert_or_assign(*back, std::move(value)); auto iter = current->insert_or_assign(*back, std::move(value)).first;
return iter->second;
} }
void put_impl(settings& dict, string_view key, config_value& value) { config_value& put_impl(settings& dict, string_view key, config_value& value) {
std::vector<string_view> path; std::vector<string_view> path;
split(path, key, "."); split(path, key, ".");
put_impl(dict, path, value); return put_impl(dict, path, value);
} }
config_value::list& put_list(settings& xs, std::string name) { config_value::list& put_list(settings& xs, std::string name) {
auto i = xs.insert_or_assign(std::move(name), config_value::list{}); config_value tmp{config_value::list{}};
return get<config_value::list>(i.first->second); auto& result = put_impl(xs, name, tmp);
return get<config_value::list>(result);
} }
config_value::dictionary& put_dictionary(settings& xs, std::string name) { config_value::dictionary& put_dictionary(settings& xs, std::string name) {
auto i = xs.insert_or_assign(std::move(name), settings{}); config_value tmp{settings{}};
return get<config_value::dictionary>(i.first->second); auto& result = put_impl(xs, name, tmp);
return get<config_value::dictionary>(result);
} }
} // namespace caf } // namespace caf
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