1 /*
2  * Copyright (C) 2018 Red Hat, Inc.
3  *
4  * Licensed under the GNU Lesser General Public License Version 2.1
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "OptionEnum.hpp"
22 
23 #include "bgettext/bgettext-lib.h"
24 #include "tinyformat/tinyformat.hpp"
25 
26 #include <sstream>
27 
28 namespace libdnf {
29 
30 template <typename T>
fromString(T & out,const std::string & in,std::ios_base & (* manipulator)(std::ios_base &))31 bool fromString(T & out, const std::string & in, std::ios_base & (*manipulator)(std::ios_base &))
32 {
33    std::istringstream iss(in);
34    return !(iss >> manipulator >> out).fail();
35 }
36 
37 template <typename T>
OptionEnum(ValueType defaultValue,const std::vector<ValueType> & enumVals)38 OptionEnum<T>::OptionEnum(ValueType defaultValue, const std::vector<ValueType> & enumVals)
39 : Option(Priority::DEFAULT), enumVals(enumVals), defaultValue(defaultValue), value(defaultValue)
40 {
41     test(defaultValue);
42 }
43 
44 template <typename T>
OptionEnum(ValueType defaultValue,std::vector<ValueType> && enumVals)45 OptionEnum<T>::OptionEnum(ValueType defaultValue, std::vector<ValueType> && enumVals)
46 : Option(Priority::DEFAULT), enumVals(std::move(enumVals)), defaultValue(defaultValue), value(defaultValue)
47 {
48     test(defaultValue);
49 }
50 
51 template <typename T>
OptionEnum(ValueType defaultValue,const std::vector<ValueType> & enumVals,FromStringFunc && fromStringFunc)52 OptionEnum<T>::OptionEnum(ValueType defaultValue, const std::vector<ValueType> & enumVals, FromStringFunc && fromStringFunc)
53 : Option(Priority::DEFAULT), fromStringUser(std::move(fromStringFunc))
54 , enumVals(enumVals), defaultValue(defaultValue), value(defaultValue)
55 {
56     test(defaultValue);
57 }
58 
59 template <typename T>
OptionEnum(ValueType defaultValue,std::vector<ValueType> && enumVals,FromStringFunc && fromStringFunc)60 OptionEnum<T>::OptionEnum(ValueType defaultValue, std::vector<ValueType> && enumVals, FromStringFunc && fromStringFunc)
61 : Option(Priority::DEFAULT), fromStringUser(std::move(fromStringFunc))
62 , enumVals(std::move(enumVals)), defaultValue(defaultValue), value(defaultValue)
63 {
64     test(defaultValue);
65 }
66 
67 template <typename T>
test(ValueType value) const68 void OptionEnum<T>::test(ValueType value) const
69 {
70     auto it = std::find(enumVals.begin(), enumVals.end(), value);
71     if (it == enumVals.end())
72         throw InvalidValue(tfm::format(_("'%s' is not an allowed value"), value));
73 }
74 
75 template <typename T>
fromString(const std::string & value) const76 T OptionEnum<T>::fromString(const std::string & value) const
77 {
78     if (fromStringUser)
79         return fromStringUser(value);
80     T val;
81     if (libdnf::fromString<ValueType>(val, value, std::dec))
82         return val;
83     throw InvalidValue(_("invalid value"));
84 }
85 
86 template <typename T>
set(Priority priority,ValueType value)87 void OptionEnum<T>::set(Priority priority, ValueType value)
88 {
89     if (priority >= this->priority) {
90         test(value);
91         this->value = value;
92         this->priority = priority;
93     }
94 }
95 
96 template <typename T>
set(Priority priority,const std::string & value)97 void OptionEnum<T>::set(Priority priority, const std::string & value)
98 {
99     set(priority, fromString(value));
100 }
101 
102 template <typename T>
getValue() const103 T OptionEnum<T>::getValue() const
104 {
105     return value;
106 }
107 
108 template <typename T>
getDefaultValue() const109 T OptionEnum<T>::getDefaultValue() const
110 {
111     return defaultValue;
112 }
113 
114 template <typename T>
toString(ValueType value) const115 std::string OptionEnum<T>::toString(ValueType value) const
116 {
117     std::ostringstream oss;
118     oss << value;
119     return oss.str();
120 }
121 
122 template <typename T>
getValueString() const123 std::string OptionEnum<T>::getValueString() const
124 {
125     return toString(value);
126 }
127 
OptionEnum(const std::string & defaultValue,const std::vector<ValueType> & enumVals)128 OptionEnum<std::string>::OptionEnum(const std::string & defaultValue, const std::vector<ValueType> & enumVals)
129 : Option(Priority::DEFAULT), enumVals(enumVals), defaultValue(defaultValue), value(defaultValue)
130 {
131     test(defaultValue);
132 }
133 
OptionEnum(const std::string & defaultValue,std::vector<ValueType> && enumVals)134 OptionEnum<std::string>::OptionEnum(const std::string & defaultValue, std::vector<ValueType> && enumVals)
135 : Option(Priority::DEFAULT), enumVals(std::move(enumVals)), defaultValue(defaultValue), value(defaultValue)
136 {
137     test(defaultValue);
138 }
139 
OptionEnum(const std::string & defaultValue,const std::vector<ValueType> & enumVals,FromStringFunc && fromStringFunc)140 OptionEnum<std::string>::OptionEnum(const std::string & defaultValue, const std::vector<ValueType> & enumVals, FromStringFunc && fromStringFunc)
141 : Option(Priority::DEFAULT), fromStringUser(std::move(fromStringFunc))
142 , enumVals(enumVals), defaultValue(defaultValue), value(defaultValue)
143 {
144     test(defaultValue);
145 }
146 
OptionEnum(const std::string & defaultValue,std::vector<ValueType> && enumVals,FromStringFunc && fromStringFunc)147 OptionEnum<std::string>::OptionEnum(const std::string & defaultValue, std::vector<ValueType> && enumVals, FromStringFunc && fromStringFunc)
148 : Option(Priority::DEFAULT), fromStringUser(std::move(fromStringFunc))
149 , enumVals(std::move(enumVals)), defaultValue(defaultValue), value(defaultValue)
150 {
151     test(defaultValue);
152 }
153 
test(const std::string & value) const154 void OptionEnum<std::string>::test(const std::string & value) const
155 {
156     auto it = std::find(enumVals.begin(), enumVals.end(), value);
157     if (it == enumVals.end())
158         throw InvalidValue(tfm::format(_("'%s' is not an allowed value"), value));
159 }
160 
fromString(const std::string & value) const161 std::string OptionEnum<std::string>::fromString(const std::string & value) const
162 {
163     if (fromStringUser)
164         return fromStringUser(value);
165     return value;
166 }
167 
set(Priority priority,const std::string & value)168 void OptionEnum<std::string>::set(Priority priority, const std::string & value)
169 {
170     auto val = fromString(value);
171     if (priority >= this->priority) {
172         test(val);
173         this->value = val;
174         this->priority = priority;
175     }
176 }
177 
178 }
179