#ifndef STORM_SETTINGS_ARGUMENTVALIDATORS_H_ #define STORM_SETTINGS_ARGUMENTVALIDATORS_H_ #include #include #include #include #include #include #include #include #include #include "src/settings/Argument.h" #include "src/utility/macros.h" #include "src/exceptions/InvalidArgumentException.h" #include "src/exceptions/IllegalArgumentException.h" #include "src/exceptions/IllegalArgumentValueException.h" #include "src/exceptions/IllegalFunctionCallException.h" namespace storm { namespace settings { class ArgumentValidators { public: /*! * Creates a validation function that checks whether an integer is in the given range (including the bounds). * * @param lowerBound The lower bound of the valid range. * @param upperBound The upper bound of the valid range. * @return The resulting validation function. */ static std::function integerRangeValidatorIncluding(int_fast64_t lowerBound, int_fast64_t upperBound) { return rangeValidatorIncluding(lowerBound, upperBound); } /*! * Creates a validation function that checks whether an integer is in the given range (excluding the bounds). * * @param lowerBound The lower bound of the valid range. * @param upperBound The upper bound of the valid range. * @return The resulting validation function. */ static std::function integerRangeValidatorExcluding(int_fast64_t lowerBound, int_fast64_t upperBound) { return rangeValidatorExcluding(lowerBound, upperBound); } /*! * Creates a validation function that checks whether an unsigned integer is in the given range (including the bounds). * * @param lowerBound The lower bound of the valid range. * @param upperBound The upper bound of the valid range. * @return The resulting validation function. */ static std::function unsignedIntegerRangeValidatorIncluding(uint_fast64_t lowerBound, uint_fast64_t upperBound) { return rangeValidatorIncluding(lowerBound, upperBound); } /*! * Creates a validation function that checks whether an unsigned integer is in the given range (excluding the bounds). * * @param lowerBound The lower bound of the valid range. * @param upperBound The upper bound of the valid range. * @return The resulting validation function. */ static std::function unsignedIntegerRangeValidatorExcluding(uint_fast64_t lowerBound, uint_fast64_t upperBound) { return rangeValidatorExcluding(lowerBound, upperBound); } /*! * Creates a validation function that checks whether a double is in the given range (including the bounds). * * @param lowerBound The lower bound of the valid range. * @param upperBound The upper bound of the valid range. * @return The resulting validation function. */ static std::function doubleRangeValidatorIncluding(double lowerBound, double upperBound) { return rangeValidatorIncluding(lowerBound, upperBound); } /*! * Creates a validation function that checks whether a double is in the given range (excluding the bounds). * * @param lowerBound The lower bound of the valid range. * @param upperBound The upper bound of the valid range. * @return The resulting validation function. */ static std::function doubleRangeValidatorExcluding(double lowerBound, double upperBound) { return rangeValidatorExcluding(lowerBound, upperBound); } /*! * Creates a validation function that checks whether a given string corresponds to an existing and readable * file. * * @return The resulting validation function. */ static std::function existingReadableFileValidator() { return [] (std::string const fileName) -> bool { std::ifstream targetFile(fileName); bool isFileGood = targetFile.good(); STORM_LOG_THROW(isFileGood, storm::exceptions::IllegalArgumentValueException, "The file " << fileName << " does not exist or is not readable."); return isFileGood; }; } /*! * Creates a validation function that checks whether a given string is in a provided list of strings. * * @param list The list of valid strings. * @return The resulting validation function. */ static std::function stringInListValidator(std::vector const& list) { return [list] (std::string const& inputString) -> bool { for (auto const& validString : list) { if (inputString == validString) { return true; } } STORM_LOG_THROW(false, storm::exceptions::IllegalArgumentValueException, "Value '" << inputString << "' does not match any entry in the list of valid items."); return false; }; } private: /*! * Creates a validation function that checks whether its argument is in a given range (including the bounds). * * @param lowerBound The lower bound of the valid range. * @param upperBound The upper bound of the valid range. * @return The resulting validation function. */ template static std::function rangeValidatorIncluding(T lowerBound, T upperBound) { return std::bind([](T lowerBound, T upperBound, T value) -> bool { STORM_LOG_THROW(lowerBound <= value && value <= upperBound, storm::exceptions::InvalidArgumentException, "Value " << value << " is out range."); return true; }, lowerBound, upperBound, std::placeholders::_1); } /*! * Creates a validation function that checks whether its argument is in a given range (excluding the bounds). * * @param lowerBound The lower bound of the valid range. * @param upperBound The upper bound of the valid range. * @return The resulting validation function. */ template static std::function rangeValidatorExcluding(T lowerBound, T upperBound) { return std::bind([](T lowerBound, T upperBound, T value) -> bool { STORM_LOG_THROW(lowerBound < value && value < upperBound, storm::exceptions::InvalidArgumentException, "Value " << value << " is out range."); return true; }, lowerBound, upperBound, std::placeholders::_1); } }; } } #endif // STORM_SETTINGS_ARGUMENTVALIDATORS_H_