#include "sanitizerimpl.hpp" #include #include #include #include #include #include #include #include #include #include namespace Settings { namespace { template struct Max final : Sanitizer { T mMax; explicit Max(const T& max) : mMax(max) { } T apply(const T& value) const override { return std::max(value, mMax); } }; template struct MaxStrict final : Sanitizer { static_assert(std::is_floating_point_v); T mMax; explicit MaxStrict(const T& max) : mMax(std::nextafter(max, std::numeric_limits::max())) { } T apply(const T& value) const override { return std::max(value, mMax); } }; template <> struct MaxStrict final : Sanitizer { osg::Vec3f mMax; explicit MaxStrict(const osg::Vec3f& max) : mMax(std::nextafter(max.x(), std::numeric_limits::max()), std::nextafter(max.y(), std::numeric_limits::max()), std::nextafter(max.z(), std::numeric_limits::max())) { } osg::Vec3f apply(const osg::Vec3f& value) const override { return osg::Vec3f( std::max(value.x(), mMax.x()), std::max(value.y(), mMax.y()), std::max(value.z(), mMax.z())); } }; template struct Clamp final : Sanitizer { T mMin; T mMax; explicit Clamp(const T& min, const T& max) : mMin(min) , mMax(max) { } T apply(const T& value) const override { return std::clamp(value, mMin, mMax); } }; template auto getPrev(const T& value) -> std::enable_if_t, T> { assert(value > -std::numeric_limits::max()); return std::nextafter(value, -std::numeric_limits::max()); } template struct ClampStrictMax final : Sanitizer { T mMin; T mMax; explicit ClampStrictMax(const T& min, const T& max) : mMin(min) , mMax(getPrev(max)) { } T apply(const T& value) const override { return std::clamp(value, mMin, mMax); } }; template struct Enum final : Sanitizer { std::vector mValues; explicit Enum(std::initializer_list value) : mValues(std::make_move_iterator(value.begin()), std::make_move_iterator(value.end())) { } T apply(const T& value) const override { if (std::find(mValues.begin(), mValues.end(), value) == mValues.end()) { std::ostringstream message; message << "Invalid enum value: " << value; throw std::runtime_error(message.str()); } return value; } }; template struct EqualOrMax final : Sanitizer { T mEqual; T mMax; explicit EqualOrMax(const T& equal, const T& max) : mEqual(equal) , mMax(max) { } T apply(const T& value) const override { if (value == mEqual) return value; return std::max(value, mMax); } }; } std::unique_ptr> makeMaxSanitizerFloat(float max) { return std::make_unique>(max); } std::unique_ptr> makeMaxSanitizerInt(int max) { return std::make_unique>(max); } std::unique_ptr> makeMaxSanitizerSize(std::size_t max) { return std::make_unique>(max); } std::unique_ptr> makeMaxSanitizerUInt64(std::uint64_t max) { return std::make_unique>(max); } std::unique_ptr> makeMaxStrictSanitizerFloat(float max) { return std::make_unique>(max); } std::unique_ptr> makeMaxStrictSanitizerVec3f(const osg::Vec3f& max) { return std::make_unique>(max); } std::unique_ptr> makeClampSanitizerFloat(float min, float max) { return std::make_unique>(min, max); } std::unique_ptr> makeClampSanitizerDouble(double min, double max) { return std::make_unique>(min, max); } std::unique_ptr> makeClampSanitizerInt(int min, int max) { return std::make_unique>(min, max); } std::unique_ptr> makeClampStrictMaxSanitizerFloat(float min, float max) { return std::make_unique>(min, max); } std::unique_ptr> makeEnumSanitizerInt(std::initializer_list values) { return std::make_unique>(values); } std::unique_ptr> makeEnumSanitizerString(std::initializer_list values) { return std::make_unique>(values); } std::unique_ptr> makeEqualOrMaxSanitizerFloat(float equal, float max) { return std::make_unique>(equal, max); } }