#include "storm/settings/Argument.h" #include "storm/settings/ArgumentValidators.h" #include "storm/exceptions/IllegalArgumentException.h" #include "storm/exceptions/IllegalArgumentValueException.h" #include "storm/exceptions/IllegalFunctionCallException.h" #include "storm/settings/ArgumentTypeInferationHelper.h" #include "storm/utility/macros.h" namespace storm { namespace settings { template Argument::Argument(std::string const& name, std::string const& description, std::vector>> const& validators): ArgumentBase(name, description), argumentValue(), argumentType(inferToEnumType()), validators(validators), isOptional(false), defaultValue(), hasDefaultValue(false) { // Intentionally left empty. } template Argument::Argument(std::string const& name, std::string const& description, std::vector>> const& validators, bool isOptional, T defaultValue): ArgumentBase(name, description), argumentValue(), argumentType(inferToEnumType()), validators(validators), 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 " << name << " has none."); bool result = this->setFromTypeValue(this->defaultValue, false); STORM_LOG_THROW(result, storm::exceptions::IllegalArgumentValueException, "Unable to assign default value to argument " << name << ", 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 for " << name << " 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 for " << name << " 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 for " << name << " 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 for " << name << " 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& validator : validators) { result &= validator->isValid(value); } return result; } template void printValue(std::ostream& out, T const& value) { out << value; } template<> void printValue(std::ostream& out, std::string const& value) { if (value.empty()) { out << "empty"; } else { out << value; } } template void Argument::printToStream(std::ostream& out) const { out << std::setw(0) << std::left << "<" << this->getName() << ">"; if (!this->validators.empty() || this->hasDefaultValue) { out << " ("; if (!this->validators.empty()) { for (uint64_t i = 0; i < this->validators.size(); ++i) { out << this->validators[i]->toString(); if (i + 1 < this->validators.size()) { out << ", "; } } if (this->hasDefaultValue) { out << "; "; } } if (this->hasDefaultValue) { out << "default: "; printValue(out, defaultValue); } out << ")"; } out << ": " << this->getDescription(); } template class Argument; template class Argument; template class Argument; template class Argument; template class Argument; } }