Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
Argument.cpp
Go to the documentation of this file.
2
4
10
11namespace storm {
12namespace settings {
13
14template<typename T>
15Argument<T>::Argument(std::string const& name, std::string const& description, std::vector<std::shared_ptr<ArgumentValidator<T>>> const& validators)
16 : ArgumentBase(name, description),
17 argumentValue(),
18 argumentType(inferToEnumType<T>()),
19 validators(validators),
20 isOptional(false),
21 defaultValue(),
22 hasDefaultValue(false),
23 wasSetFromDefaultValueFlag(false) {
24 // Intentionally left empty.
25}
26
27template<typename T>
28Argument<T>::Argument(std::string const& name, std::string const& description, std::vector<std::shared_ptr<ArgumentValidator<T>>> const& validators,
29 bool isOptional, T defaultValue)
30 : ArgumentBase(name, description),
31 argumentValue(),
32 argumentType(inferToEnumType<T>()),
33 validators(validators),
34 isOptional(isOptional),
35 defaultValue(),
36 hasDefaultValue(true),
37 wasSetFromDefaultValueFlag(false) {
38 this->setDefaultValue(defaultValue);
39}
40
41template<typename T>
43 return this->isOptional;
44}
45
46template<typename T>
48 bool conversionOk = false;
49 T newValue = ArgumentBase::convertFromString<T>(fromStringValue, conversionOk);
50 if (!conversionOk) {
51 return false;
52 }
53 return this->setFromTypeValue(newValue);
54}
55
56template<typename T>
57bool Argument<T>::setFromTypeValue(T const& newValue, bool hasBeenSet) {
58 if (!this->validate(newValue)) {
59 return false;
60 }
61 this->argumentValue = newValue;
62 this->hasBeenSet = hasBeenSet;
63 return true;
64}
65
66template<typename T>
68 return this->argumentType;
69}
70
71template<typename T>
73 STORM_LOG_THROW(this->getHasBeenSet() || this->getHasDefaultValue(), storm::exceptions::IllegalFunctionCallException,
74 "Unable to retrieve value of argument '" << this->getName() << "', because it was neither set nor specifies a default value.");
75 if (this->getHasBeenSet()) {
76 return this->argumentValue;
77 } else {
78 return this->defaultValue;
79 }
80}
81
82template<typename T>
84 return this->hasDefaultValue;
85}
86
87template<typename T>
89 STORM_LOG_THROW(this->hasDefaultValue, storm::exceptions::IllegalFunctionCallException,
90 "Unable to set value from default value, because the argument " << name << " has none.");
91 bool result = this->setFromTypeValue(this->defaultValue, false);
92 STORM_LOG_THROW(result, storm::exceptions::IllegalArgumentValueException,
93 "Unable to assign default value to argument " << name << ", because it was rejected.");
94 this->wasSetFromDefaultValueFlag = true;
95}
96
97template<typename T>
99 return wasSetFromDefaultValueFlag;
100}
101
102template<typename T>
103std::string Argument<T>::getValueAsString() const {
104 switch (this->argumentType) {
106 return inferToString(ArgumentType::String, this->getArgumentValue());
108 bool iValue = inferToBoolean(ArgumentType::Boolean, this->getArgumentValue());
109 if (iValue) {
110 return "true";
111 } else {
112 return "false";
113 }
114 }
115 default:
116 return ArgumentBase::convertToString(this->argumentValue);
117 }
118}
119
120template<typename T>
122 switch (this->argumentType) {
124 return inferToInteger(ArgumentType::Integer, this->getArgumentValue());
125 default:
126 STORM_LOG_THROW(false, storm::exceptions::IllegalFunctionCallException, "Unable to retrieve argument value for " << name << " as integer.");
127 break;
128 }
129}
130
131template<typename T>
133 switch (this->argumentType) {
135 return inferToUnsignedInteger(ArgumentType::UnsignedInteger, this->getArgumentValue());
136 default:
137 STORM_LOG_THROW(false, storm::exceptions::IllegalFunctionCallException,
138 "Unable to retrieve argument value for " << name << " as unsigned integer.");
139 break;
140 }
141}
142
143template<typename T>
145 switch (this->argumentType) {
147 return inferToDouble(ArgumentType::Double, this->getArgumentValue());
148 default:
149 STORM_LOG_THROW(false, storm::exceptions::IllegalFunctionCallException, "Unable to retrieve argument value for " << name << " as double.");
150 break;
151 }
152}
153
154template<typename T>
156 switch (this->argumentType) {
158 return inferToBoolean(ArgumentType::Boolean, this->getArgumentValue());
159 default:
160 STORM_LOG_THROW(false, storm::exceptions::IllegalFunctionCallException, "Unable to retrieve argument value for " << name << " as Boolean.");
161 break;
162 }
163}
164
165template<typename T>
167 STORM_LOG_THROW(this->validate(newDefault), storm::exceptions::IllegalArgumentValueException,
168 "The default value for the argument did not pass all validation functions.");
169 this->defaultValue = newDefault;
170 this->hasDefaultValue = true;
171}
172
173template<typename T>
174bool Argument<T>::validate(T const& value) const {
175 bool result = true;
176 for (auto const& validator : validators) {
177 result &= validator->isValid(value);
178 }
179 return result;
180}
181
182template<typename T>
183void printValue(std::ostream& out, T const& value) {
184 out << value;
185}
186
187template<>
188void printValue(std::ostream& out, std::string const& value) {
189 if (value.empty()) {
190 out << "empty";
191 } else {
192 out << value;
193 }
194}
195
196template<typename T>
197void Argument<T>::printToStream(std::ostream& out) const {
198 out << std::setw(0) << std::left << "<" << this->getName() << ">";
199 if (!this->validators.empty() || this->hasDefaultValue) {
200 out << " (";
201 bool previousEntry = false;
202 if (this->getIsOptional()) {
203 out << "optional";
204 previousEntry = true;
205 }
206 if (!this->validators.empty()) {
207 if (previousEntry) {
208 out << "; ";
209 }
210 for (uint64_t i = 0; i < this->validators.size(); ++i) {
211 out << this->validators[i]->toString();
212 if (i + 1 < this->validators.size()) {
213 out << ", ";
214 }
215 }
216 previousEntry = true;
217 }
218
219 if (this->hasDefaultValue) {
220 if (previousEntry) {
221 out << "; ";
222 }
223 out << "default: ";
224 printValue(out, defaultValue);
225 }
226 out << ")";
227 }
228
229 out << ": " << this->getDescription();
230}
231
232template class Argument<std::string>;
233template class Argument<int_fast64_t>;
234template class Argument<uint_fast64_t>;
235template class Argument<double>;
236template class Argument<bool>;
237} // namespace settings
238} // namespace storm
This class serves as the (untemplated) base class of argument classes.
static std::string convertToString(ValueType const &value)
Converts the given value to a string representation.
This class subclasses the argument base to actually implement the pure virtual functions.
Definition Argument.h:32
virtual bool getValueAsBoolean() const override
Retrieves the value of this argument as a boolean.
Definition Argument.cpp:155
virtual double getValueAsDouble() const override
Retrieves the value of this argument as a double.
Definition Argument.cpp:144
virtual std::string getValueAsString() const override
Retrieves the value of this argument as a string.
Definition Argument.cpp:103
virtual bool getHasDefaultValue() const override
Retrieves whether the argument has a default value.
Definition Argument.cpp:83
virtual uint_fast64_t getValueAsUnsignedInteger() const override
Retrieves the value of this argument as an unsigned integer.
Definition Argument.cpp:132
bool setFromStringValue(std::string const &fromStringValue) override
Tries to set the value of the argument from the given string.
Definition Argument.cpp:47
bool setFromTypeValue(T const &newValue, bool hasBeenSet=true)
Definition Argument.cpp:57
void setFromDefaultValue() override
Sets the value of the argument from the default value.
Definition Argument.cpp:88
virtual bool getIsOptional() const override
Retrieves whether the argument is optional.
Definition Argument.cpp:42
virtual bool wasSetFromDefaultValue() const override
Definition Argument.cpp:98
virtual void printToStream(std::ostream &out) const override
Prints a string representation of the argument to the provided stream.
Definition Argument.cpp:197
Argument(std::string const &name, std::string const &description, std::vector< std::shared_ptr< ArgumentValidator< T > > > const &validators)
Creates a new argument with the given parameters.
Definition Argument.cpp:15
virtual ArgumentType getType() const override
Retrieves the type of the argument.
Definition Argument.cpp:67
virtual int_fast64_t getValueAsInteger() const override
Retrieves the value of this argument as an integer.
Definition Argument.cpp:121
T const & getArgumentValue() const
Retrieves the value of the argument if any has been set.
Definition Argument.cpp:72
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
void printValue(std::ostream &out, T const &value)
Definition Argument.cpp:183
int_fast64_t inferToInteger(ArgumentType const &, T const &)
SettingsType const & getModule()
Get module.
bool inferToBoolean(ArgumentType const &, T const &)
double inferToDouble(ArgumentType const &, T const &)
ArgumentType
This enum captures all possible types for arguments.
uint_fast64_t inferToUnsignedInteger(ArgumentType const &, T const &)
std::string const & inferToString(ArgumentType const &, T const &)
ArgumentType inferToEnumType()
This function infers the type in our enum of possible types from the template parameter.
LabParser.cpp.
Definition cli.cpp:18