Unverified Commit c9e8ad17 authored by Dominik Charousset's avatar Dominik Charousset Committed by GitHub

Merge pull request #801

Allow copy of config_option
parents 5b4d38ad 26b30a27
...@@ -54,10 +54,18 @@ public: ...@@ -54,10 +54,18 @@ public:
config_option(string_view category, string_view name, string_view description, config_option(string_view category, string_view name, string_view description,
const meta_state* meta, void* value = nullptr); const meta_state* meta, void* value = nullptr);
config_option(const config_option&);
config_option(config_option&&) = default; config_option(config_option&&) = default;
config_option& operator=(const config_option&);
config_option& operator=(config_option&&) = default; config_option& operator=(config_option&&) = default;
// -- swap function ----------------------------------------------------------
friend void swap(config_option& first, config_option& second) noexcept;
// -- properties ------------------------------------------------------------- // -- properties -------------------------------------------------------------
/// Returns the category of the option. /// Returns the category of the option.
......
...@@ -166,8 +166,7 @@ public: ...@@ -166,8 +166,7 @@ public:
} }
/// Adds a config option to the set. /// Adds a config option to the set.
/// @private config_option_set& add(config_option opt);
config_option_set& add(config_option&& opt);
/// Generates human-readable help text for all options. /// Generates human-readable help text for all options.
std::string help_text(bool global_only = true) const; std::string help_text(bool global_only = true) const;
......
...@@ -77,6 +77,34 @@ config_option::config_option(string_view category, string_view name, ...@@ -77,6 +77,34 @@ config_option::config_option(string_view category, string_view name,
CAF_ASSERT(pos() == buf_size_); CAF_ASSERT(pos() == buf_size_);
} }
config_option::config_option(const config_option& other)
: category_separator_{other.category_separator_},
long_name_separator_{other.long_name_separator_},
short_names_separator_{other.short_names_separator_},
buf_size_{other.buf_size_},
meta_{other.meta_},
value_{other.value_} {
buf_.reset(new char[buf_size_]);
std::copy_n(other.buf_.get(), buf_size_, buf_.get());
}
config_option& config_option::operator=(const config_option& other) {
config_option tmp{other};
swap(*this, tmp);
return *this;
}
void swap(config_option& first, config_option& second) noexcept {
using std::swap;
swap(first.buf_, second.buf_);
swap(first.category_separator_, second.category_separator_);
swap(first.long_name_separator_, second.long_name_separator_);
swap(first.short_names_separator_, second.short_names_separator_);
swap(first.buf_size_, second.buf_size_);
swap(first.meta_, second.meta_);
swap(first.value_, second.value_);
}
// -- properties --------------------------------------------------------------- // -- properties ---------------------------------------------------------------
string_view config_option::category() const noexcept { string_view config_option::category() const noexcept {
......
...@@ -52,7 +52,7 @@ void insert(string_builder& builder, size_t count, char ch) { ...@@ -52,7 +52,7 @@ void insert(string_builder& builder, size_t count, char ch) {
namespace caf { namespace caf {
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;
} }
......
...@@ -87,8 +87,29 @@ void check_integer_options() { ...@@ -87,8 +87,29 @@ void check_integer_options() {
check_integer_options<T>(tk); check_integer_options<T>(tk);
} }
void compare(const config_option& lhs, const config_option& rhs) {
CAF_CHECK_EQUAL(lhs.category(), rhs.category());
CAF_CHECK_EQUAL(lhs.long_name(), rhs.long_name());
CAF_CHECK_EQUAL(lhs.short_names(), rhs.short_names());
CAF_CHECK_EQUAL(lhs.description(), rhs.description());
CAF_CHECK_EQUAL(lhs.full_name(), rhs.full_name());
}
} // namespace <anonymous> } // namespace <anonymous>
CAF_TEST(copy constructor) {
auto one = make_config_option<int>("cat1", "one", "option 1");
auto two = one;
compare(one, two);
}
CAF_TEST(copy assignment) {
auto one = make_config_option<int>("cat1", "one", "option 1");
auto two = make_config_option<int>("cat2", "two", "option 2");
two = one;
compare(one, two);
}
CAF_TEST(type_bool) { CAF_TEST(type_bool) {
CAF_CHECK_EQUAL(read<bool>("true"), true); CAF_CHECK_EQUAL(read<bool>("true"), true);
CAF_CHECK_EQUAL(read<bool>("false"), false); CAF_CHECK_EQUAL(read<bool>("false"), false);
......
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