#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" #include 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 integer is greater than or equal to the given threshold. * * @param threshold The threshold. * @return The resulting validation function. */ static std::function integerGreaterValidatorIncluding(int_fast64_t threshold) { return greaterValidatorIncluding(threshold); } /*! * Creates a validation function that checks whether an integer is greater than the given threshold. * * @param threshold The threshold. * @return The resulting validation function. */ static std::function integerGreaterValidatorExcluding(int_fast64_t threshold) { return greaterValidatorExcluding(threshold); } /*! * 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 an unsigned integer is greater than or equal to the given threshold. * * @param threshold The threshold. * @return The resulting validation function. */ static std::function unsignedIntegerGreaterValidatorIncluding(uint_fast64_t threshold) { return greaterValidatorIncluding(threshold); } /*! * Creates a validation function that checks whether an unsigned integer is greater than the given threshold. * * @param threshold The threshold. * @return The resulting validation function. */ static std::function unsignedIntegerGreaterValidatorExcluding(uint_fast64_t threshold) { return greaterValidatorExcluding(threshold); } /*! * 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 double is greater than or equal to the given threshold. * * @param threshold The threshold. * @return The resulting validation function. */ static std::function doubleGreaterValidatorIncluding(double threshold) { return greaterValidatorIncluding(threshold); } /*! * Creates a validation function that checks whether a double is greater than the given threshold. * * @param threshold The threshold. * @return The resulting validation function. */ static std::function doubleGreaterValidatorExcluding(double threshold) { return greaterValidatorExcluding(threshold); } /*! * 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 { // First check existence as ifstream::good apparently als returns true for directories. struct stat info; stat(fileName.c_str(), &info); STORM_LOG_THROW(info.st_mode & S_IFREG, storm::exceptions::IllegalArgumentValueException, "Unable to read from non-existing file '" << fileName << "'."); // Now that we know it's a file, we can check its readability. std::ifstream istream(fileName); STORM_LOG_THROW(istream.good(), storm::exceptions::IllegalArgumentValueException, "Unable to read from file '" << fileName << "'."); return true; }; } /*! * 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 of 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 of range."); return true; }, lowerBound, upperBound, std::placeholders::_1); } /*! * Creates a validation function that checks whether its argument is greater than the given threshold. * * @param threshold The threshold. * @return The resulting validation function. */ template static std::function greaterValidatorExcluding(T threshold) { return std::bind([](T threshold, T value) -> bool { STORM_LOG_THROW(threshold < value, storm::exceptions::InvalidArgumentException, "Value " << value << " is out of range."); return true; }, threshold, std::placeholders::_1); } /*! * Creates a validation function that checks whether its argument is greater than or equal to the given threshold. * * @param threshold The threshold. * @return The resulting validation function. */ template static std::function greaterValidatorIncluding(T threshold) { return std::bind([](T threshold, T value) -> bool { STORM_LOG_THROW(threshold <= value, storm::exceptions::InvalidArgumentException, "Value " << value << " is out of range."); return true; }, threshold, std::placeholders::_1); } }; } } #endif // STORM_SETTINGS_ARGUMENTVALIDATORS_H_