diff options
Diffstat (limited to 'includes/image_io/base/validated_number.h')
-rw-r--r-- | includes/image_io/base/validated_number.h | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/includes/image_io/base/validated_number.h b/includes/image_io/base/validated_number.h new file mode 100644 index 0000000..ead5ab3 --- /dev/null +++ b/includes/image_io/base/validated_number.h @@ -0,0 +1,38 @@ +#ifndef IMAGE_IO_BASE_VALIDATED_NUMBER_H_ // NOLINT +#define IMAGE_IO_BASE_VALIDATED_NUMBER_H_ // NOLINT + +#include <sstream> +#include <string> + +namespace photos_editing_formats { +namespace image_io { + +template <class T> +struct ValidatedNumber { + ValidatedNumber() : ValidatedNumber(T(), false) {} + ValidatedNumber(const T& value_, bool is_valid_) + : value(value_), is_valid(is_valid_) {} + using value_type = T; + T value; + bool is_valid; +}; + +template <class T> +ValidatedNumber<T> GetValidatedNumber(const std::string& str) { + std::stringstream ss(str); + ValidatedNumber<T> result; + ss >> result.value; + if (!ss.fail()) { + std::string extra; + ss >> extra; + if (extra.empty()) { + result.is_valid = true; + } + } + return result; +} + +} // namespace image_io +} // namespace photos_editing_formats + +#endif // IMAGE_IO_BASE_VALIDATED_NUMBER_H_ // NOLINT |