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