Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libdnf5::OptionEnum: remove template, add pImpl #1346

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions bindings/libdnf5/conf.i
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ wrap_unique_ptr(StringUniquePtr, std::string);
%include "libdnf5/conf/option.hpp"
%include "libdnf5/conf/option_bool.hpp"
%include "libdnf5/conf/option_enum.hpp"
%template(OptionEnumString) libdnf5::OptionEnum<std::string>;

%include "libdnf5/conf/option_number.hpp"
%template(OptionNumberInt32) libdnf5::OptionNumber<std::int32_t>;
Expand All @@ -81,7 +80,7 @@ wrap_unique_ptr(StringUniquePtr, std::string);
%template(OptionChildNumberInt32) libdnf5::OptionChild<libdnf5::OptionNumber<std::int32_t>>;
%template(OptionChildNumberUInt32) libdnf5::OptionChild<libdnf5::OptionNumber<std::uint32_t>>;
%template(OptionChildNumberFloat) libdnf5::OptionChild<libdnf5::OptionNumber<float>>;
%template(OptionChildEnumString) libdnf5::OptionChild<libdnf5::OptionEnum<std::string>>;
%template(OptionChildEnum) libdnf5::OptionChild<libdnf5::OptionEnum>;
%template(OptionChildSeconds) libdnf5::OptionChild<libdnf5::OptionSeconds>;


Expand Down
6 changes: 3 additions & 3 deletions dnf5-plugins/automatic_plugin/config_automatic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ class ConfigAutomaticCommands : public libdnf5::Config {
ConfigAutomaticCommands();
~ConfigAutomaticCommands() = default;

libdnf5::OptionEnum<std::string> upgrade_type{"default", {"default", "security"}};
libdnf5::OptionEnum upgrade_type{"default", {"default", "security"}};
libdnf5::OptionNumber<std::int32_t> random_sleep{0};
libdnf5::OptionNumber<std::int32_t> network_online_timeout{60};
libdnf5::OptionBool download_updates{true};
libdnf5::OptionBool apply_updates{false};
libdnf5::OptionEnum<std::string> reboot{"never", {"never", "when-changed", "when-needed"}};
libdnf5::OptionEnum reboot{"never", {"never", "when-changed", "when-needed"}};
libdnf5::OptionString reboot_command{"shutdown -r +5 'Rebooting after applying package updates'"};
};

Expand Down Expand Up @@ -73,7 +73,7 @@ class ConfigAutomaticEmail : public libdnf5::Config {
libdnf5::OptionString email_from{"root"};
libdnf5::OptionString email_host{"localhost"};
libdnf5::OptionNumber<std::int32_t> email_port{25};
libdnf5::OptionEnum<std::string> email_tls{"no", {"no", "yes", "starttls"}};
libdnf5::OptionEnum email_tls{"no", {"no", "yes", "starttls"}};
};


Expand Down
8 changes: 4 additions & 4 deletions dnf5/commands/repoquery/repoquery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ void RepoqueryCommand::set_argument_parser() {
"supplements",
"", // empty when option is not used
};
providers_of_option = dynamic_cast<libdnf5::OptionEnum<std::string> *>(
parser.add_init_value(std::make_unique<libdnf5::OptionEnum<std::string>>("", pkg_attrs_options)));
providers_of_option = dynamic_cast<libdnf5::OptionEnum *>(
parser.add_init_value(std::make_unique<libdnf5::OptionEnum>("", pkg_attrs_options)));
auto * providers_of = parser.add_new_named_arg("providersof");
std::string allowed_values = libdnf5::utils::string::join(pkg_attrs_options, ", ");
// Drop the empty option from the description of supported values
Expand Down Expand Up @@ -412,8 +412,8 @@ void RepoqueryCommand::set_argument_parser() {
// Add additional supported package attribute getters, all pkg_attrs_options get turned into options
pkg_attrs_options.insert(pkg_attrs_options.begin(), {"files", "sourcerpm", "location"});

pkg_attr_option = dynamic_cast<libdnf5::OptionEnum<std::string> *>(
parser.add_init_value(std::make_unique<libdnf5::OptionEnum<std::string>>("", pkg_attrs_options)));
pkg_attr_option = dynamic_cast<libdnf5::OptionEnum *>(
parser.add_init_value(std::make_unique<libdnf5::OptionEnum>("", pkg_attrs_options)));
// remove the last empty ("") option, it should not be an arg
pkg_attrs_options.pop_back();
for (auto & pkg_attr : pkg_attrs_options) {
Expand Down
4 changes: 2 additions & 2 deletions dnf5/commands/repoquery/repoquery.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class RepoqueryCommand : public Command {

libdnf5::OptionBool * querytags_option{nullptr};
libdnf5::OptionString * query_format_option{nullptr};
libdnf5::OptionEnum<std::string> * pkg_attr_option{nullptr};
libdnf5::OptionEnum<std::string> * providers_of_option{nullptr};
libdnf5::OptionEnum * pkg_attr_option{nullptr};
libdnf5::OptionEnum * providers_of_option{nullptr};

std::unique_ptr<AdvisoryOption> advisory_name{nullptr};
std::unique_ptr<SecurityOption> advisory_security{nullptr};
Expand Down
5 changes: 2 additions & 3 deletions dnf5daemon-client/commands/repolist/repolist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ void RepolistCommand::set_argument_parser() {
auto & cmd = *get_argument_parser_command();


enable_disable_option = dynamic_cast<libdnf5::OptionEnum<std::string> *>(
parser.add_init_value(std::unique_ptr<libdnf5::OptionEnum<std::string>>(
new libdnf5::OptionEnum<std::string>("enabled", {"all", "enabled", "disabled"}))));
enable_disable_option = dynamic_cast<libdnf5::OptionEnum *>(parser.add_init_value(
std::unique_ptr<libdnf5::OptionEnum>(new libdnf5::OptionEnum("enabled", {"all", "enabled", "disabled"}))));

auto all = parser.add_new_named_arg("all");
all->set_long_name("all");
Expand Down
2 changes: 1 addition & 1 deletion dnf5daemon-client/commands/repolist/repolist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class RepolistCommand : public DaemonCommand {
void run() override;

private:
libdnf5::OptionEnum<std::string> * enable_disable_option{nullptr};
libdnf5::OptionEnum * enable_disable_option{nullptr};
std::vector<std::unique_ptr<libdnf5::Option>> * patterns_options{nullptr};
const std::string command;
};
Expand Down
20 changes: 10 additions & 10 deletions include/libdnf5/conf/config_main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ class ConfigMain : public Config {
const OptionBool & get_reset_nice_option() const;
OptionPath & get_system_cachedir_option();
const OptionPath & get_system_cachedir_option() const;
OptionEnum<std::string> & get_cacheonly_option();
const OptionEnum<std::string> & get_cacheonly_option() const;
OptionEnum & get_cacheonly_option();
const OptionEnum & get_cacheonly_option() const;
OptionBool & get_keepcache_option();
const OptionBool & get_keepcache_option() const;
OptionPath & get_logdir_option();
Expand Down Expand Up @@ -130,8 +130,8 @@ class ConfigMain : public Config {
const OptionSeconds & get_metadata_timer_sync_option() const;
OptionStringList & get_disable_excludes_option();
const OptionStringList & get_disable_excludes_option() const;
OptionEnum<std::string> & get_multilib_policy_option(); // :api
const OptionEnum<std::string> & get_multilib_policy_option() const;
OptionEnum & get_multilib_policy_option(); // :api
const OptionEnum & get_multilib_policy_option() const;
OptionBool & get_best_option(); // :api
const OptionBool & get_best_option() const;
OptionBool & get_install_weak_deps_option();
Expand All @@ -142,8 +142,8 @@ class ConfigMain : public Config {
const OptionString & get_bugtracker_url_option() const;
OptionBool & get_zchunk_option();
const OptionBool & get_zchunk_option() const;
OptionEnum<std::string> & get_color_option();
const OptionEnum<std::string> & get_color_option() const;
OptionEnum & get_color_option();
const OptionEnum & get_color_option() const;
OptionString & get_color_list_installed_older_option();
const OptionString & get_color_list_installed_older_option() const;
OptionString & get_color_list_installed_newer_option();
Expand Down Expand Up @@ -187,8 +187,8 @@ class ConfigMain : public Config {
const OptionBool & get_autocheck_running_kernel_option() const;
OptionBool & get_clean_requirements_on_remove_option();
const OptionBool & get_clean_requirements_on_remove_option() const;
OptionEnum<std::string> & get_history_list_view_option();
const OptionEnum<std::string> & get_history_list_view_option() const;
OptionEnum & get_history_list_view_option();
const OptionEnum & get_history_list_view_option() const;
OptionBool & get_upgrade_group_objects_upgrade_option();
const OptionBool & get_upgrade_group_objects_upgrade_option() const;
OptionPath & get_destdir_option();
Expand Down Expand Up @@ -256,8 +256,8 @@ class ConfigMain : public Config {
const OptionNumber<std::uint32_t> & get_bandwidth_option() const;
OptionNumber<std::uint32_t> & get_minrate_option();
const OptionNumber<std::uint32_t> & get_minrate_option() const;
OptionEnum<std::string> & get_ip_resolve_option();
const OptionEnum<std::string> & get_ip_resolve_option() const;
OptionEnum & get_ip_resolve_option();
const OptionEnum & get_ip_resolve_option() const;
OptionNumber<float> & get_throttle_option();
const OptionNumber<float> & get_throttle_option() const;
OptionSeconds & get_timeout_option();
Expand Down
42 changes: 5 additions & 37 deletions include/libdnf5/conf/option.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#define LIBDNF5_CONF_OPTION_HPP

#include "libdnf5/common/exception.hpp"
#include "libdnf5/common/impl_ptr.hpp"

#include <string>

Expand Down Expand Up @@ -77,8 +78,8 @@ class Option {
};

explicit Option(Priority priority = Priority::EMPTY);
Option(const Option & src) = default;
virtual ~Option() = default;
Option(const Option & src);
virtual ~Option();

/// Makes copy (clone) of this object.
// @replaces libdnf:conf/Option.hpp:method:Option.clone()
Expand Down Expand Up @@ -127,43 +128,10 @@ class Option {
const std::string & get_lock_comment() const noexcept;

private:
Priority priority;
bool locked{false};
std::string lock_comment;
class Impl;
ImplPtr<Impl> p_impl;
};

inline Option::Option(Priority priority) : priority(priority) {}

inline Option::Priority Option::get_priority() const {
return priority;
}

inline bool Option::empty() const noexcept {
return priority == Priority::EMPTY;
}

inline void Option::set_priority(Priority priority) {
this->priority = priority;
}

inline void Option::lock(const std::string & first_comment) {
if (!locked) {
lock_comment = first_comment;
locked = true;
}
}

inline bool Option::is_locked() const noexcept {
return locked;
}

inline void Option::assert_not_locked() const {
libdnf_user_assert(!locked, "Attempting to write to a locked option: {}", get_lock_comment());
}

inline const std::string & Option::get_lock_comment() const noexcept {
return lock_comment;
}

} // namespace libdnf5

Expand Down
105 changes: 9 additions & 96 deletions include/libdnf5/conf/option_enum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,84 +28,19 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

namespace libdnf5 {

/// Option that stores value from enumeration. The type of value is template parameter.
/// Support default value and user defined function for conversion from string.
// @replaces libdnf:conf/OptionEnum.hpp:class:OptionEnum<T>
template <typename T>
class OptionEnum : public Option {
public:
using ValueType = T;
using FromStringFunc = std::function<ValueType(const std::string &)>;

OptionEnum(ValueType default_value, const std::vector<ValueType> & enum_vals);
OptionEnum(ValueType default_value, std::vector<ValueType> && enum_vals);
OptionEnum(ValueType default_value, const std::vector<ValueType> & enum_vals, FromStringFunc && from_string_func);
OptionEnum(ValueType default_value, std::vector<ValueType> && enum_vals, FromStringFunc && from_string_func);

/// Makes copy (clone) of this object.
// @replaces libdnf:conf/OptionEnum.hpp:method:OptionEnum<T>.clone()
OptionEnum * clone() const override;

/// Sets new value and priority (source).
/// The value and priority are stored only if the new priority is equal to or higher than the stored priority.
// @replaces libdnf:conf/OptionEnum.hpp:method:OptionEnum<T>.set(Priority priority, bool value)
void set(Priority priority, ValueType value);

/// Sets new value and runtime priority.
void set(ValueType value);

/// Parses input string and sets new value and priority.
/// The value and priority are stored only if the new priority is equal to or higher than the stored priority.
// @replaces libdnf:conf/OptionEnum.hpp:method:OptionEnum<T>.set(Priority priority, const std::string & value)
void set(Priority priority, const std::string & value) override;

/// Parses input string and sets new value and runtime priority.
void set(const std::string & value) override;

/// Gets the stored value.
// @replaces libdnf:conf/OptionEnum.hpp:method:OptionEnum<T>.getValue()
T get_value() const;

/// Gets the default value. Default value is used until it is replaced by set() method.
// @replaces libdnf:conf/OptionEnum.hpp:method:OptionEnum<T>.getValueString()
T get_default_value() const;

/// Gets a string representation of the stored value.
// @replaces libdnf:conf/OptionEnum.hpp:method:OptionEnum<T>.getValueString()
std::string get_value_string() const override;

/// Tests input value and throws exception if the value is not allowed.
// @replaces libdnf:conf/OptionEnum.hpp:method:OptionEnum<T>.test(ValueType value)
void test(ValueType value) const;

/// Parses input string and returns result.
// @replaces libdnf:conf/OptionEnum.hpp:method:OptionEnum<T>.fromString(const std::string & value)
ValueType from_string(const std::string & value) const;

/// Converts input value to the string.
// @replaces libdnf:conf/OptionEnum.hpp:method:OptionEnum<T>.toString(ValueType value)
std::string to_string(ValueType value) const;

private:
FromStringFunc from_string_user;
std::vector<ValueType> enum_vals;
ValueType default_value;
ValueType value;
};


/// Option that stores value from enumeration. Specialized template for enumeration of strings.
/// Option that stores value from enumeration of strings.
/// It supports default value.
/// It supports user defined function for conversion from string.
// @replaces libdnf:conf/OptionEnum.hpp:class:OptionEnum<std::string>
template <>
class OptionEnum<std::string> : public Option {
class OptionEnum : public Option {
public:
using ValueType = std::string;
using FromStringFunc = std::function<ValueType(const std::string &)>;
using FromStringFunc = std::function<std::string(const std::string &)>;

OptionEnum(std::string default_value, std::vector<std::string> enum_vals);
OptionEnum(std::string default_value, std::vector<std::string> enum_vals, FromStringFunc && from_string_func);

OptionEnum(const std::string & default_value, std::vector<ValueType> enum_vals);
OptionEnum(const std::string & default_value, std::vector<ValueType> enum_vals, FromStringFunc && from_string_func);
~OptionEnum() override;

/// Makes copy (clone) of this object.
// @replaces libdnf:conf/OptionEnum.hpp:method:OptionEnum<std::string>.clone()
Expand Down Expand Up @@ -140,32 +75,10 @@ class OptionEnum<std::string> : public Option {
std::string from_string(const std::string & value) const;

private:
FromStringFunc from_string_user;
std::vector<ValueType> enum_vals;
ValueType default_value;
ValueType value;
class Impl;
ImplPtr<Impl> p_impl;
};

template <typename T>
inline OptionEnum<T> * OptionEnum<T>::clone() const {
return new OptionEnum<T>(*this);
}

inline OptionEnum<std::string> * OptionEnum<std::string>::clone() const {
return new OptionEnum<std::string>(*this);
}

inline const std::string & OptionEnum<std::string>::get_value() const {
return value;
}

inline const std::string & OptionEnum<std::string>::get_default_value() const {
return default_value;
}

inline std::string OptionEnum<std::string>::get_value_string() const {
return value;
}

} // namespace libdnf5

Expand Down
8 changes: 4 additions & 4 deletions include/libdnf5/repo/config_repo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ class ConfigRepo : public Config {
const OptionChild<OptionNumber<std::uint32_t>> & get_bandwidth_option() const;
OptionChild<OptionNumber<std::uint32_t>> & get_minrate_option();
const OptionChild<OptionNumber<std::uint32_t>> & get_minrate_option() const;
OptionChild<OptionEnum<std::string>> & get_ip_resolve_option();
const OptionChild<OptionEnum<std::string>> & get_ip_resolve_option() const;
OptionChild<OptionEnum> & get_ip_resolve_option();
const OptionChild<OptionEnum> & get_ip_resolve_option() const;
OptionChild<OptionNumber<float>> & get_throttle_option();
const OptionChild<OptionNumber<float>> & get_throttle_option() const;
OptionChild<OptionSeconds> & get_timeout_option();
Expand Down Expand Up @@ -141,8 +141,8 @@ class ConfigRepo : public Config {
OptionChild<OptionBool> & get_countme_option();
const OptionChild<OptionBool> & get_countme_option() const;
// yum compatibility options
OptionEnum<std::string> & get_failovermethod_option();
const OptionEnum<std::string> & get_failovermethod_option() const;
OptionEnum & get_failovermethod_option();
const OptionEnum & get_failovermethod_option() const;

/// @return A unique ID of the repository, consisting of its id and a hash
/// computed from its source URLs (metalink, mirrorlist or baseurl, first
Expand Down
Loading
Loading