/* * Copyright (C) 2018 Red Hat, Inc. * * Licensed under the GNU Lesser General Public License Version 2.1 * * This library 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. * * This library 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 this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "OptionEnum.hpp" #include "bgettext/bgettext-lib.h" #include "tinyformat/tinyformat.hpp" #include namespace libdnf { template bool fromString(T & out, const std::string & in, std::ios_base & (*manipulator)(std::ios_base &)) { std::istringstream iss(in); return !(iss >> manipulator >> out).fail(); } template OptionEnum::OptionEnum(ValueType defaultValue, const std::vector & enumVals) : Option(Priority::DEFAULT), enumVals(enumVals), defaultValue(defaultValue), value(defaultValue) { test(defaultValue); } template OptionEnum::OptionEnum(ValueType defaultValue, std::vector && enumVals) : Option(Priority::DEFAULT), enumVals(std::move(enumVals)), defaultValue(defaultValue), value(defaultValue) { test(defaultValue); } template OptionEnum::OptionEnum(ValueType defaultValue, const std::vector & enumVals, FromStringFunc && fromStringFunc) : Option(Priority::DEFAULT), fromStringUser(std::move(fromStringFunc)) , enumVals(enumVals), defaultValue(defaultValue), value(defaultValue) { test(defaultValue); } template OptionEnum::OptionEnum(ValueType defaultValue, std::vector && enumVals, FromStringFunc && fromStringFunc) : Option(Priority::DEFAULT), fromStringUser(std::move(fromStringFunc)) , enumVals(std::move(enumVals)), defaultValue(defaultValue), value(defaultValue) { test(defaultValue); } template void OptionEnum::test(ValueType value) const { auto it = std::find(enumVals.begin(), enumVals.end(), value); if (it == enumVals.end()) throw InvalidValue(tfm::format(_("'%s' is not an allowed value"), value)); } template T OptionEnum::fromString(const std::string & value) const { if (fromStringUser) return fromStringUser(value); T val; if (libdnf::fromString(val, value, std::dec)) return val; throw InvalidValue(_("invalid value")); } template void OptionEnum::set(Priority priority, ValueType value) { if (priority >= this->priority) { test(value); this->value = value; this->priority = priority; } } template void OptionEnum::set(Priority priority, const std::string & value) { set(priority, fromString(value)); } template T OptionEnum::getValue() const { return value; } template T OptionEnum::getDefaultValue() const { return defaultValue; } template std::string OptionEnum::toString(ValueType value) const { std::ostringstream oss; oss << value; return oss.str(); } template std::string OptionEnum::getValueString() const { return toString(value); } OptionEnum::OptionEnum(const std::string & defaultValue, const std::vector & enumVals) : Option(Priority::DEFAULT), enumVals(enumVals), defaultValue(defaultValue), value(defaultValue) { test(defaultValue); } OptionEnum::OptionEnum(const std::string & defaultValue, std::vector && enumVals) : Option(Priority::DEFAULT), enumVals(std::move(enumVals)), defaultValue(defaultValue), value(defaultValue) { test(defaultValue); } OptionEnum::OptionEnum(const std::string & defaultValue, const std::vector & enumVals, FromStringFunc && fromStringFunc) : Option(Priority::DEFAULT), fromStringUser(std::move(fromStringFunc)) , enumVals(enumVals), defaultValue(defaultValue), value(defaultValue) { test(defaultValue); } OptionEnum::OptionEnum(const std::string & defaultValue, std::vector && enumVals, FromStringFunc && fromStringFunc) : Option(Priority::DEFAULT), fromStringUser(std::move(fromStringFunc)) , enumVals(std::move(enumVals)), defaultValue(defaultValue), value(defaultValue) { test(defaultValue); } void OptionEnum::test(const std::string & value) const { auto it = std::find(enumVals.begin(), enumVals.end(), value); if (it == enumVals.end()) throw InvalidValue(tfm::format(_("'%s' is not an allowed value"), value)); } std::string OptionEnum::fromString(const std::string & value) const { if (fromStringUser) return fromStringUser(value); return value; } void OptionEnum::set(Priority priority, const std::string & value) { auto val = fromString(value); if (priority >= this->priority) { test(val); this->value = val; this->priority = priority; } } }