File: sanitizerimpl.cpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (210 lines) | stat: -rw-r--r-- 5,944 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "sanitizerimpl.hpp"

#include <osg/Vec3f>

#include <algorithm>
#include <cassert>
#include <cmath>
#include <limits>
#include <sstream>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>

namespace Settings
{
    namespace
    {
        template <class T>
        struct Max final : Sanitizer<T>
        {
            T mMax;

            explicit Max(const T& max)
                : mMax(max)
            {
            }

            T apply(const T& value) const override { return std::max(value, mMax); }
        };

        template <class T>
        struct MaxStrict final : Sanitizer<T>
        {
            static_assert(std::is_floating_point_v<T>);

            T mMax;

            explicit MaxStrict(const T& max)
                : mMax(std::nextafter(max, std::numeric_limits<T>::max()))
            {
            }

            T apply(const T& value) const override { return std::max(value, mMax); }
        };

        template <>
        struct MaxStrict<osg::Vec3f> final : Sanitizer<osg::Vec3f>
        {
            osg::Vec3f mMax;

            explicit MaxStrict(const osg::Vec3f& max)
                : mMax(std::nextafter(max.x(), std::numeric_limits<float>::max()),
                    std::nextafter(max.y(), std::numeric_limits<float>::max()),
                    std::nextafter(max.z(), std::numeric_limits<float>::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 <class T>
        struct Clamp final : Sanitizer<T>
        {
            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 <class T>
        auto getPrev(const T& value) -> std::enable_if_t<std::is_floating_point_v<T>, T>
        {
            assert(value > -std::numeric_limits<T>::max());
            return std::nextafter(value, -std::numeric_limits<T>::max());
        }

        template <class T>
        struct ClampStrictMax final : Sanitizer<T>
        {
            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 <class T>
        struct Enum final : Sanitizer<T>
        {
            std::vector<T> mValues;

            explicit Enum(std::initializer_list<T> 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 <class T>
        struct EqualOrMax final : Sanitizer<T>
        {
            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<Sanitizer<float>> makeMaxSanitizerFloat(float max)
    {
        return std::make_unique<Max<float>>(max);
    }

    std::unique_ptr<Sanitizer<int>> makeMaxSanitizerInt(int max)
    {
        return std::make_unique<Max<int>>(max);
    }

    std::unique_ptr<Sanitizer<std::size_t>> makeMaxSanitizerSize(std::size_t max)
    {
        return std::make_unique<Max<std::size_t>>(max);
    }

    std::unique_ptr<Sanitizer<std::uint64_t>> makeMaxSanitizerUInt64(std::uint64_t max)
    {
        return std::make_unique<Max<std::uint64_t>>(max);
    }

    std::unique_ptr<Sanitizer<float>> makeMaxStrictSanitizerFloat(float max)
    {
        return std::make_unique<MaxStrict<float>>(max);
    }

    std::unique_ptr<Sanitizer<osg::Vec3f>> makeMaxStrictSanitizerVec3f(const osg::Vec3f& max)
    {
        return std::make_unique<MaxStrict<osg::Vec3f>>(max);
    }

    std::unique_ptr<Sanitizer<float>> makeClampSanitizerFloat(float min, float max)
    {
        return std::make_unique<Clamp<float>>(min, max);
    }

    std::unique_ptr<Sanitizer<double>> makeClampSanitizerDouble(double min, double max)
    {
        return std::make_unique<Clamp<double>>(min, max);
    }

    std::unique_ptr<Sanitizer<int>> makeClampSanitizerInt(int min, int max)
    {
        return std::make_unique<Clamp<int>>(min, max);
    }

    std::unique_ptr<Sanitizer<float>> makeClampStrictMaxSanitizerFloat(float min, float max)
    {
        return std::make_unique<ClampStrictMax<float>>(min, max);
    }

    std::unique_ptr<Sanitizer<int>> makeEnumSanitizerInt(std::initializer_list<int> values)
    {
        return std::make_unique<Enum<int>>(values);
    }

    std::unique_ptr<Sanitizer<std::string>> makeEnumSanitizerString(std::initializer_list<std::string> values)
    {
        return std::make_unique<Enum<std::string>>(values);
    }

    std::unique_ptr<Sanitizer<float>> makeEqualOrMaxSanitizerFloat(float equal, float max)
    {
        return std::make_unique<EqualOrMax<float>>(equal, max);
    }
}