#include "Argument.h" #include "src/exceptions/IllegalArgumentException.h" #include "src/exceptions/IllegalArgumentValueException.h" #include "src/exceptions/IllegalFunctionCallException.h" #include "src/settings/ArgumentTypeInferationHelper.h" #include "src/utility/macros.h" namespace storm { namespace settings { template Argument::Argument(std::string const& name, std::string const& description, std::vector const& validationFunctions): ArgumentBase(name, description), argumentValue(), argumentType(inferToEnumType()), validationFunctions(validationFunctions), isOptional(false), defaultValue(), hasDefaultValue(false) { // Intentionally left empty. } template Argument::Argument(std::string const& name, std::string const& description, std::vector const& validationFunctions, bool isOptional, T defaultValue): ArgumentBase(name, description), argumentValue(), argumentType(inferToEnumType()), validationFunctions(validationFunctions), isOptional(isOptional), defaultValue(), hasDefaultValue(true) { this->setDefaultValue(defaultValue); } template bool Argument::getIsOptional() const { return this->isOptional; } template bool Argument::setFromStringValue(std::string const& fromStringValue) { bool conversionOk = false; T newValue = ArgumentBase::convertFromString(fromStringValue, conversionOk); if (!conversionOk) { return false; } return this->setFromTypeValue(newValue); } template bool Argument::setFromTypeValue(T const& newValue, bool hasBeenSet) { if (!this->validate(newValue)) { return false; } this->argumentValue = newValue; this->hasBeenSet = hasBeenSet; return true; } template ArgumentType Argument::getType() const { return this->argumentType; } template T const& Argument::getArgumentValue() const { STORM_LOG_THROW(this->getHasBeenSet() || this->getHasDefaultValue(), storm::exceptions::IllegalFunctionCallException, "Unable to retrieve value of argument '" << this->getName() << "', because it was neither set nor specifies a default value."); if (this->getHasBeenSet()) { return this->argumentValue; } else { return this->defaultValue; } } template bool Argument::getHasDefaultValue() const { return this->hasDefaultValue; } template void Argument::setFromDefaultValue() { STORM_LOG_THROW(this->hasDefaultValue, storm::exceptions::IllegalFunctionCallException, "Unable to set value from default value, because the argument has none."); bool result = this->setFromTypeValue(this->defaultValue, false); STORM_LOG_THROW(result, storm::exceptions::IllegalArgumentValueException, "Unable to assign default value to argument, because it was rejected."); } template std::string Argument::getValueAsString() const { switch (this->argumentType) { case ArgumentType::String: return inferToString(ArgumentType::String, this->getArgumentValue()); case ArgumentType::Boolean: { bool iValue = inferToBoolean(ArgumentType::Boolean, this->getArgumentValue()); if (iValue) { return "true"; } else { return "false"; } } default: return ArgumentBase::convertToString(this->argumentValue); } } template int_fast64_t Argument::getValueAsInteger() const { switch (this->argumentType) { case ArgumentType::Integer: return inferToInteger(ArgumentType::Integer, this->getArgumentValue()); default: STORM_LOG_THROW(false, storm::exceptions::IllegalFunctionCallException, "Unable to retrieve argument value as integer."); break; } } template uint_fast64_t Argument::getValueAsUnsignedInteger() const { switch (this->argumentType) { case ArgumentType::UnsignedInteger: return inferToUnsignedInteger(ArgumentType::UnsignedInteger, this->getArgumentValue()); default: STORM_LOG_THROW(false, storm::exceptions::IllegalFunctionCallException, "Unable to retrieve argument value as unsigned integer."); break; } } template double Argument::getValueAsDouble() const { switch (this->argumentType) { case ArgumentType::Double: return inferToDouble(ArgumentType::Double, this->getArgumentValue()); default: STORM_LOG_THROW(false, storm::exceptions::IllegalFunctionCallException, "Unable to retrieve argument value as double."); break; } } template bool Argument::getValueAsBoolean() const { switch (this->argumentType) { case ArgumentType::Boolean: return inferToBoolean(ArgumentType::Boolean, this->getArgumentValue()); default: STORM_LOG_THROW(false, storm::exceptions::IllegalFunctionCallException, "Unable to retrieve argument value as boolean."); break; } } template void Argument::setDefaultValue(T const& newDefault) { STORM_LOG_THROW(this->validate(newDefault), storm::exceptions::IllegalArgumentValueException, "The default value for the argument did not pass all validation functions."); this->defaultValue = newDefault; this->hasDefaultValue = true; } template bool Argument::validate(T const& value) const { bool result = true; for (auto const& lambda : validationFunctions) { result = result && lambda(value); } return result; } template class Argument; template class Argument; template class Argument; template class Argument; template class Argument; } }