Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ArgumentBuilder.h
Go to the documentation of this file.
1#ifndef STORM_SETTINGS_ARGUMENTBUILDER_H_
2#define STORM_SETTINGS_ARGUMENTBUILDER_H_
3
4#include <functional>
5#include <iostream>
6#include <list>
7#include <memory>
8#include <sstream>
9#include <string>
10#include <unordered_map>
11#include <utility>
12#include <vector>
13
18
22
23namespace storm {
24namespace settings {
25
30 public:
38 static ArgumentBuilder createStringArgument(std::string const& name, std::string const& description) {
39 ArgumentBuilder ab(ArgumentType::String, name, description);
40 return ab;
41 }
42
50 static ArgumentBuilder createIntegerArgument(std::string const& name, std::string const& description) {
51 ArgumentBuilder ab(ArgumentType::Integer, name, description);
52 return ab;
53 }
54
62 static ArgumentBuilder createUnsignedIntegerArgument(std::string const& name, std::string const& description) {
64 return ab;
65 }
66
74 static ArgumentBuilder createDoubleArgument(std::string const& name, std::string const& description) {
75 ArgumentBuilder ab(ArgumentType::Double, name, description);
76 return ab;
77 }
78
86 static ArgumentBuilder createBooleanArgument(std::string const& name, std::string const& description) {
87 ArgumentBuilder ab(ArgumentType::Boolean, name, description);
88 return ab;
89 }
90
97 this->isOptional = true;
98 STORM_LOG_THROW(this->hasDefaultValue, storm::exceptions::IllegalFunctionCallException,
99 "Unable to make argument '" << this->name << "' optional without default value.");
100 return *this;
101 }
102
103#define PPCAT_NX(A, B) A##B
104#define PPCAT(A, B) PPCAT_NX(A, B)
105#define MACROaddValidator(funcName, funcType) \
106 ArgumentBuilder& PPCAT(addValidator, funcName)(std::shared_ptr<ArgumentValidator<funcType>> && validator) { \
107 STORM_LOG_THROW(this->type == ArgumentType::funcName, storm::exceptions::IllegalFunctionCallException, \
108 "Illegal validation function for argument, because it takes arguments of different type."); \
109 (PPCAT(this->validators_, funcName)).emplace_back(validator); \
110 return *this; \
111 }
112
113 // Add the methods to add validation functions.
116#define MACROsetDefaultValue(funcName, funcType) \
117 ArgumentBuilder& PPCAT(setDefaultValue, funcName)(funcType const& defaultValue) { \
118 STORM_LOG_THROW(this->type == ArgumentType::funcName, storm::exceptions::IllegalFunctionCallException, \
119 "Illegal default value for argument" << this->name << ", because it is of different type."); \
120 PPCAT(this->defaultValue_, funcName) = defaultValue; \
121 this->hasDefaultValue = true; \
122 return *this; \
123 }
124
125 // Add the methods to set a default value.
128
135 STORM_LOG_THROW(!this->hasBeenBuilt, storm::exceptions::IllegalFunctionCallException,
136 "Cannot rebuild argument with builder that was already used to build an argument.");
137 this->hasBeenBuilt = true;
138 switch (this->type) {
140 if (this->hasDefaultValue) {
141 return std::shared_ptr<ArgumentBase>(
142 new Argument<std::string>(this->name, this->description, this->validators_String, this->isOptional, this->defaultValue_String));
143 } else {
144 return std::shared_ptr<ArgumentBase>(new Argument<std::string>(this->name, this->description, this->validators_String));
145 }
146 break;
147 }
149 if (this->hasDefaultValue) {
150 return std::shared_ptr<ArgumentBase>(
151 new Argument<int_fast64_t>(this->name, this->description, this->validators_Integer, this->isOptional, this->defaultValue_Integer));
152 } else {
153 return std::shared_ptr<ArgumentBase>(new Argument<int_fast64_t>(this->name, this->description, this->validators_Integer));
154 }
155 break;
157 if (this->hasDefaultValue) {
158 return std::shared_ptr<ArgumentBase>(new Argument<uint_fast64_t>(this->name, this->description, this->validators_UnsignedInteger,
159 this->isOptional, this->defaultValue_UnsignedInteger));
160 } else {
161 return std::shared_ptr<ArgumentBase>(new Argument<uint_fast64_t>(this->name, this->description, this->validators_UnsignedInteger));
162 }
163 break;
165 if (this->hasDefaultValue) {
166 return std::shared_ptr<ArgumentBase>(
167 new Argument<double>(this->name, this->description, this->validators_Double, this->isOptional, this->defaultValue_Double));
168 } else {
169 return std::shared_ptr<ArgumentBase>(new Argument<double>(this->name, this->description, this->validators_Double));
170 }
171 break;
173 if (this->hasDefaultValue) {
174 return std::shared_ptr<ArgumentBase>(
175 new Argument<bool>(this->name, this->description, this->validators_Boolean, this->isOptional, this->defaultValue_Boolean));
176 } else {
177 return std::shared_ptr<ArgumentBase>(new Argument<bool>(this->name, this->description, this->validators_Boolean));
178 }
179 break;
180 }
181 STORM_LOG_THROW(false, storm::exceptions::IllegalArgumentTypeException, "Argument has illegal type.");
182 }
183
184 private:
192 ArgumentBuilder(ArgumentType type, std::string const& name, std::string const& description)
193 : hasBeenBuilt(false),
194 type(type),
195 name(name),
196 description(description),
197 isOptional(false),
198 hasDefaultValue(false),
199 defaultValue_String(),
200 defaultValue_Integer(),
201 defaultValue_UnsignedInteger(),
202 defaultValue_Double(),
203 defaultValue_Boolean() {
204 // Intentionally left empty.
205 }
206
207 // A flag that stores whether an argument has been built using this builder.
208 bool hasBeenBuilt;
209
210 // The type of the argument.
211 ArgumentType type;
212
213 // The name of te argument.
214 std::string name;
215
216 // The description of the argument.
217 std::string description;
218
219 // A flag indicating whether the argument is optional.
220 bool isOptional;
221
222 // A flag that stores whether the argument has a default value.
223 bool hasDefaultValue;
224
225 // The default value of the argument separated by its type.
226 std::string defaultValue_String;
227 int_fast64_t defaultValue_Integer;
228 uint_fast64_t defaultValue_UnsignedInteger;
229 double defaultValue_Double;
230 bool defaultValue_Boolean;
231
232 // The validation functions separated by their type.
233 std::vector<std::shared_ptr<ArgumentValidator<std::string>>> validators_String;
234 std::vector<std::shared_ptr<ArgumentValidator<int_fast64_t>>> validators_Integer;
235 std::vector<std::shared_ptr<ArgumentValidator<uint_fast64_t>>> validators_UnsignedInteger;
236 std::vector<std::shared_ptr<ArgumentValidator<double>>> validators_Double;
237 std::vector<std::shared_ptr<ArgumentValidator<bool>>> validators_Boolean;
238};
239} // namespace settings
240} // namespace storm
241
242#endif // STORM_SETTINGS_ARGUMENTBUILDER_H_
#define MACROsetDefaultValue(funcName, funcType)
#define MACROaddValidator(funcName, funcType)
This class serves as the (untemplated) base class of argument classes.
This class serves as an API for creating arguments.
ArgumentBuilder & makeOptional()
Make the argument optional.
static ArgumentBuilder createUnsignedIntegerArgument(std::string const &name, std::string const &description)
Creates an unsigned integer argument with the given parameters.
int_fast64_t double std::string uint_fast64_t bool std::shared_ptr< ArgumentBase > build()
Builds an argument based on the information that was added to the builder object.
static ArgumentBuilder createDoubleArgument(std::string const &name, std::string const &description)
Creates a double argument with the given parameters.
MACROaddValidator(String, std::string) MACROaddValidator(Integer
int_fast64_t double std::string MACROsetDefaultValue(Integer, int_fast64_t) MACROsetDefaultValue(UnsignedInteger
static ArgumentBuilder createBooleanArgument(std::string const &name, std::string const &description)
Creates a boolean argument with the given parameters.
static ArgumentBuilder createIntegerArgument(std::string const &name, std::string const &description)
Creates an integer argument with the given parameters.
static ArgumentBuilder createStringArgument(std::string const &name, std::string const &description)
Creates a string argument with the given parameters.
This class subclasses the argument base to actually implement the pure virtual functions.
Definition Argument.h:32
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
SettingsType const & getModule()
Get module.
ArgumentType
This enum captures all possible types for arguments.
LabParser.cpp.
Definition cli.cpp:18