1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// Libdnf is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "libdnf5/conf/option_enum.hpp"
#include "libdnf5/utils/bgettext/bgettext-mark-domain.h"
#include <algorithm>
namespace libdnf5 {
class OptionEnum::Impl {
public:
Impl(std::string && default_value, std::vector<std::string> && enum_vals)
: enum_vals(std::move(enum_vals)),
default_value(default_value),
value(std::move(default_value)) {}
Impl(std::string && default_value, std::vector<std::string> && enum_vals, FromStringFunc && from_string_func)
: from_string_user(std::move(from_string_func)),
enum_vals(std::move(enum_vals)),
default_value(default_value),
value(std::move(default_value)) {}
private:
friend OptionEnum;
FromStringFunc from_string_user;
std::vector<std::string> enum_vals;
std::string default_value;
std::string value;
};
OptionEnum::OptionEnum(std::string default_value, std::vector<std::string> enum_vals)
: Option(Priority::DEFAULT),
p_impl(new Impl(std::move(default_value), std::move(enum_vals))) {
test(p_impl->value);
}
OptionEnum::OptionEnum(
std::string default_value, std::vector<std::string> enum_vals, FromStringFunc && from_string_func)
: Option(Priority::DEFAULT),
p_impl(new Impl(std::move(default_value), std::move(enum_vals), std::move(from_string_func))) {
test(p_impl->value);
}
OptionEnum::~OptionEnum() = default;
void OptionEnum::test(const std::string & value) const {
auto it = std::find(p_impl->enum_vals.begin(), p_impl->enum_vals.end(), value);
if (it == p_impl->enum_vals.end()) {
throw OptionValueNotAllowedError(M_("Enum option value \"{}\" not allowed"), value);
}
}
std::string OptionEnum::from_string(const std::string & value) const {
if (p_impl->from_string_user) {
return p_impl->from_string_user(value);
}
return value;
}
void OptionEnum::set(Priority priority, const std::string & value) {
assert_not_locked();
auto val = from_string(value);
if (priority >= get_priority()) {
test(val);
p_impl->value = val;
set_priority(priority);
}
}
void OptionEnum::set(const std::string & value) {
set(Priority::RUNTIME, value);
}
OptionEnum * OptionEnum::clone() const {
return new OptionEnum(*this);
}
const std::string & OptionEnum::get_value() const {
return p_impl->value;
}
const std::string & OptionEnum::get_default_value() const {
return p_impl->default_value;
}
std::string OptionEnum::get_value_string() const {
return p_impl->value;
}
} // namespace libdnf5
|