summaryrefslogtreecommitdiffhomepage
path: root/entity/constraints.hpp
blob: 6ebf2d908d29c23d1db24767cde11cce2ee3122c (plain)
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
#pragma once
#include "erased-constraints.hpp"
#include <type_traits>
#include <limits>
#include <utility>
#include <Corrade/Containers/StringView.h>

namespace floormat::entities::constraints {

template<typename T> struct range
{
    using limits = std::numeric_limits<T>;
    T min = limits::min(), max = limits::max();

    constexpr operator erased_constraints::range() const noexcept;
    constexpr operator std::pair<T, T>() const noexcept;
    constexpr bool operator==(const range&) const noexcept = default;
};

template<typename T> range(T min, T max) -> range<T>;

template<typename T>
constexpr range<T>::operator erased_constraints::range() const noexcept
{
    using enum erased_constraints::range::type_;
    if constexpr (std::is_floating_point_v<T>)
        return { { .f = min }, { .f = max }, type_float };
    if constexpr (std::is_integral_v<T> && std::is_unsigned_v<T>)
        return { {.u = min}, {.u = max}, type_uint };
    if constexpr (std::is_integral_v<T> && std::is_signed_v<T>)
        return { {.i = min}, {.i = max}, type_int };
    return { {}, {}, type_none };
}

template<typename T> constexpr range<T>::operator std::pair<T, T>() const noexcept { return { min, max }; }

template<> struct range<String>
{
    constexpr operator erased_constraints::range() const noexcept { return {}; }
};

using max_length = erased_constraints::max_length;

} // namespace floormat::entities::constraints