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