Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ArgumentBuilder.h
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <iostream>
5#include <list>
6#include <memory>
7#include <sstream>
8#include <string>
9#include <unordered_map>
10#include <utility>
11#include <vector>
12
17
21
22namespace storm {
23namespace settings {
24
28class ArgumentBuilder {
29 public:
37 static ArgumentBuilder createStringArgument(std::string const& name, std::string const& description) {
38 ArgumentBuilder ab(ArgumentType::String, name, description);
39 return ab;
40 }
41
49 static ArgumentBuilder createIntegerArgument(std::string const& name, std::string const& description) {
50 ArgumentBuilder ab(ArgumentType::Integer, name, description);
51 return ab;
52 }
53
61 static ArgumentBuilder createUnsignedIntegerArgument(std::string const& name, std::string const& description) {
62 ArgumentBuilder ab(ArgumentType::UnsignedInteger, name, description);
63 return ab;
64 }
65
73 static ArgumentBuilder createDoubleArgument(std::string const& name, std::string const& description) {
74 ArgumentBuilder ab(ArgumentType::Double, name, description);
75 return ab;
76 }
77
85 static ArgumentBuilder createBooleanArgument(std::string const& name, std::string const& description) {
86 ArgumentBuilder ab(ArgumentType::Boolean, name, description);
87 return ab;
88 }
89
95 ArgumentBuilder& makeOptional() {
96 this->isOptional = true;
97 STORM_LOG_THROW(this->hasDefaultValue, storm::exceptions::IllegalFunctionCallException,
98 "Unable to make argument '" << this->name << "' optional without default value.");
99 return *this;
100 }
101
102#define PPCAT_NX(A, B) A##B
103#define PPCAT(A, B) PPCAT_NX(A, B)
104#define MACROaddValidator(funcName, funcType) \
105 ArgumentBuilder& PPCAT(addValidator, funcName)(std::shared_ptr<ArgumentValidator<funcType>> && validator) { \
106 STORM_LOG_THROW(this->type == ArgumentType::funcName, storm::exceptions::IllegalFunctionCallException, \
107 "Illegal validation function for argument, because it takes arguments of different type."); \
108 (PPCAT(this->validators_, funcName)).emplace_back(validator); \
109 return *this; \
110 }
111
112 // Add the methods to add validation functions.
115#define MACROsetDefaultValue(funcName, funcType) \
116 ArgumentBuilder& PPCAT(setDefaultValue, funcName)(funcType const& defaultValue) { \
117 STORM_LOG_THROW(this->type == ArgumentType::funcName, storm::exceptions::IllegalFunctionCallException, \
118 "Illegal default value for argument" << this->name << ", because it is of different type."); \
119 PPCAT(this->defaultValue_, funcName) = defaultValue; \
120 this->hasDefaultValue = true; \
121 return *this; \
122 }
123
124 // Add the methods to set a default value.
127
133 std::shared_ptr<ArgumentBase> build() {
134 STORM_LOG_THROW(!this->hasBeenBuilt, storm::exceptions::IllegalFunctionCallException,
135 "Cannot rebuild argument with builder that was already used to build an argument.");
136 this->hasBeenBuilt = true;
137 switch (this->type) {
139 if (this->hasDefaultValue) {
140 return std::shared_ptr<ArgumentBase>(
141 new Argument<std::string>(this->name, this->description, this->validators_String, this->isOptional, this->defaultValue_String));
142 } else {
143 return std::shared_ptr<ArgumentBase>(new Argument<std::string>(this->name, this->description, this->validators_String));
144 }
145 break;
146 }
148 if (this->hasDefaultValue) {
149 return std::shared_ptr<ArgumentBase>(
150 new Argument<int_fast64_t>(this->name, this->description, this->validators_Integer, this->isOptional, this->defaultValue_Integer));
151 } else {
152 return std::shared_ptr<ArgumentBase>(new Argument<int_fast64_t>(this->name, this->description, this->validators_Integer));
153 }
154 break;
156 if (this->hasDefaultValue) {
157 return std::shared_ptr<ArgumentBase>(new Argument<uint_fast64_t>(this->name, this->description, this->validators_UnsignedInteger,
158 this->isOptional, this->defaultValue_UnsignedInteger));
159 } else {
160 return std::shared_ptr<ArgumentBase>(new Argument<uint_fast64_t>(this->name, this->description, this->validators_UnsignedInteger));
161 }
162 break;
164 if (this->hasDefaultValue) {
165 return std::shared_ptr<ArgumentBase>(
166 new Argument<double>(this->name, this->description, this->validators_Double, this->isOptional, this->defaultValue_Double));
167 } else {
168 return std::shared_ptr<ArgumentBase>(new Argument<double>(this->name, this->description, this->validators_Double));
169 }
170 break;
172 if (this->hasDefaultValue) {
173 return std::shared_ptr<ArgumentBase>(
174 new Argument<bool>(this->name, this->description, this->validators_Boolean, this->isOptional, this->defaultValue_Boolean));
175 } else {
176 return std::shared_ptr<ArgumentBase>(new Argument<bool>(this->name, this->description, this->validators_Boolean));
177 }
178 break;
179 }
180 STORM_LOG_THROW(false, storm::exceptions::IllegalArgumentTypeException, "Argument has illegal type.");
181 }
182
183 private:
191 ArgumentBuilder(ArgumentType type, std::string const& name, std::string const& description)
192 : hasBeenBuilt(false),
193 type(type),
194 name(name),
195 description(description),
196 isOptional(false),
197 hasDefaultValue(false),
198 defaultValue_String(),
199 defaultValue_Integer(),
200 defaultValue_UnsignedInteger(),
201 defaultValue_Double(),
202 defaultValue_Boolean() {
203 // Intentionally left empty.
204 }
205
206 // A flag that stores whether an argument has been built using this builder.
207 bool hasBeenBuilt;
208
209 // The type of the argument.
210 ArgumentType type;
211
212 // The name of te argument.
213 std::string name;
214
215 // The description of the argument.
216 std::string description;
217
218 // A flag indicating whether the argument is optional.
219 bool isOptional;
220
221 // A flag that stores whether the argument has a default value.
222 bool hasDefaultValue;
223
224 // The default value of the argument separated by its type.
225 std::string defaultValue_String;
226 int_fast64_t defaultValue_Integer;
227 uint_fast64_t defaultValue_UnsignedInteger;
228 double defaultValue_Double;
229 bool defaultValue_Boolean;
230
231 // The validation functions separated by their type.
232 std::vector<std::shared_ptr<ArgumentValidator<std::string>>> validators_String;
233 std::vector<std::shared_ptr<ArgumentValidator<int_fast64_t>>> validators_Integer;
234 std::vector<std::shared_ptr<ArgumentValidator<uint_fast64_t>>> validators_UnsignedInteger;
235 std::vector<std::shared_ptr<ArgumentValidator<double>>> validators_Double;
236 std::vector<std::shared_ptr<ArgumentValidator<bool>>> validators_Boolean;
237};
238} // namespace settings
239} // namespace storm
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:31
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:28
ArgumentType
This enum captures all possible types for arguments.