Commit 49f51fdc authored by Dominik Charousset's avatar Dominik Charousset

Add rvalue overloads and require description

Instead of allowing users to skip the description, the config_value_set
now allows skipping the category and sets it to "global" by default. The
new rvalue overloads enable users to in-place construct an option set
when passing it to an object or function.
parent 84607e71
......@@ -59,10 +59,6 @@ public:
/// Categorized settings.
using config_map = caf::dictionary<dictionary>;
// -- constructors, destructors, and assignment operators --------------------
config_option_set();
// -- properties -------------------------------------------------------------
/// Returns the first `config_option` that matches the CLI name.
......@@ -84,34 +80,73 @@ public:
option_pointer qualified_name_lookup(string_view name) const;
/// Returns the number of stored config options.
inline size_t size() const noexcept {
size_t size() const noexcept {
return opts_.size();
}
/// Returns an iterator to the first ::config_option object.
inline iterator begin() noexcept {
iterator begin() noexcept {
return opts_.begin();
}
/// Returns the past-the-end iterator.
inline iterator end() noexcept {
iterator end() noexcept {
return opts_.end();
}
/// Adds a config option to the set.
template <class T>
config_option_set& add(string_view category, string_view name,
string_view description = "") {
string_view description) & {
return add(make_config_option<T>(category, name, description));
}
/// Adds a config option to the set.
template <class T>
config_option_set& add(string_view name, string_view description) & {
return add(make_config_option<T>("global", name, description));
}
/// Adds a config option to the set.
template <class T>
config_option_set&& add(string_view category, string_view name,
string_view description) && {
return std::move(add<T>(category, name, description));
}
/// Adds a config option to the set.
template <class T>
config_option_set&& add(string_view name, string_view description) && {
return std::move(add<T>("global", name, description));
}
/// Adds a config option to the set that synchronizes its value with `ref`.
template <class T>
config_option_set& add(T& ref, string_view category, string_view name,
string_view description = "") {
string_view description) & {
return add(make_config_option<T>(ref, category, name, description));
}
/// Adds a config option to the set that synchronizes its value with `ref`.
template <class T>
config_option_set& add(T& ref, string_view name, string_view description) & {
return add(ref, "global", name, description);
}
/// Adds a config option to the set that synchronizes its value with `ref`.
template <class T>
config_option_set&& add(T& ref, string_view category, string_view name,
string_view description) && {
return std::move(add(ref, category, name, description));
}
/// Adds a config option to the set that synchronizes its value with `ref`.
template <class T>
config_option_set&& add(T& ref, string_view name,
string_view description) && {
return std::move(add(ref, "global", name, description));
}
/// Adds a config option to the set.
/// @private
config_option_set& add(config_option&& opt);
......
......@@ -52,10 +52,6 @@ void insert(string_builder& builder, size_t count, char ch) {
namespace caf {
config_option_set::config_option_set() {
// nop
}
config_option_set& config_option_set::add(config_option&& opt) {
opts_.emplace_back(std::move(opt));
return *this;
......
......@@ -43,7 +43,7 @@ struct fixture {
CAF_TEST_FIXTURE_SCOPE(config_option_set_tests, fixture)
CAF_TEST(lookup) {
opts.add<int>("global", "opt1,1", "test option 1")
opts.add<int>("opt1,1", "test option 1")
.add<float>("test", "opt2,2", "test option 2")
.add<bool>("test", "flag,fl3", "test flag");
CAF_CHECK_EQUAL(opts.size(), 3u);
......@@ -101,7 +101,7 @@ CAF_TEST(parse with ref syncing) {
}
CAF_TEST(implicit global) {
opts.add<int>("global", "value").add<bool>("global", "help");
opts.add<int>("value", "some value").add<bool>("help", "print help text");
CAF_MESSAGE("test long option with argument");
dictionary<config_value::dictionary> cfg;
auto res = opts.parse(cfg, {"--value=42"});
......@@ -115,7 +115,7 @@ CAF_TEST(implicit global) {
}
CAF_TEST(atom parameters) {
opts.add<atom_value>("global", "value,v");
opts.add<atom_value>("value,v", "some value");
CAF_MESSAGE("test atom option without quotes");
auto parse_args = [&](std::vector<std::string> args) -> expected<atom_value> {
dictionary<config_value::dictionary> cfg;
......
......@@ -51,13 +51,13 @@ struct fixture {
config_option_set::config_map config;
fixture() {
options.add<bool>("global", "is_server")
.add<uint16_t>("global", "port")
.add<ls>("global", "nodes")
.add<string>("logger", "file-name")
.add<int>("scheduler", "padding")
.add<timespan>("scheduler", "timing")
.add<atom_value>("scheduler", "impl");
options.add<bool>("global", "is_server", "enables server mode")
.add<uint16_t>("global", "port", "sets local or remote port")
.add<ls>("global", "nodes", "list of remote nodes")
.add<string>("logger", "file-name", "log output file")
.add<int>("scheduler", "padding", "some integer")
.add<timespan>("scheduler", "timing", "some timespan")
.add<atom_value>("scheduler", "impl", "some atom");
}
};
......
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