From 06ccfd8b34c702da9ad695982e8210823d7b501c Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 23 Feb 2023 07:25:55 +0100 Subject: wip --- entity/erased-constraints.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++ entity/erased-constraints.hpp | 58 ++--------------------------------- 2 files changed, 74 insertions(+), 55 deletions(-) create mode 100644 entity/erased-constraints.cpp (limited to 'entity') diff --git a/entity/erased-constraints.cpp b/entity/erased-constraints.cpp new file mode 100644 index 00000000..599b4164 --- /dev/null +++ b/entity/erased-constraints.cpp @@ -0,0 +1,71 @@ +#include "erased-constraints.hpp" +#include "compat/assert.hpp" +#include +#include +#include + +static_assert(sizeof(std::size_t) == sizeof(std::uintptr_t)); +static_assert(sizeof(std::size_t) == sizeof(std::ptrdiff_t)); + +namespace floormat::entities::erased_constraints { + +template std::pair range::convert() const +{ + static_assert(sizeof(T) <= sizeof(std::size_t)); + using limits = std::numeric_limits; + + if (type == type_none) + return { limits::min(), limits::max() }; + else + { + if constexpr (std::is_integral_v) + { + if (std::is_signed_v) + { + fm_assert(type == type_int); + return { T(min.i), T(max.i) }; + } + else + { + fm_assert(type == type_uint); + return { T(min.u), T(max.u) }; + } + } + else + { + static_assert(std::is_floating_point_v); + fm_assert(type == type_float); + return { T(min.f), T(max.f) }; + } + } +} + +template std::pair range::convert() const; +template std::pair range::convert() const; +template std::pair range::convert() const; +template std::pair range::convert() const; +template std::pair range::convert() const; +template std::pair range::convert() const; +template std::pair range::convert() const; +template std::pair range::convert() const; +template std::pair range::convert() const; +template std::pair range::convert() const; + +bool operator==(const range& a, const range& b) +{ + if (a.type != b.type) + return false; + + constexpr float eps = 1e-6f; + + switch (a.type) + { + default: return false; + case range::type_none: return true; + case range::type_float: return std::fabs(a.min.f - b.min.f) < eps && std::fabs(a.max.f - b.max.f) < eps; + case range::type_uint: return a.min.u == b.min.u && a.max.u == b.max.u; + case range::type_int: return a.min.i == b.min.i && a.max.i == b.max.i; + } +} + +} // namespace floormat::entities::erased_constraints diff --git a/entity/erased-constraints.hpp b/entity/erased-constraints.hpp index 4fe5c853..5a6cb939 100644 --- a/entity/erased-constraints.hpp +++ b/entity/erased-constraints.hpp @@ -1,15 +1,8 @@ #pragma once -#include -#include -#include -#include #include namespace floormat::entities::erased_constraints { -static_assert(sizeof(std::size_t) == sizeof(std::uintptr_t)); -static_assert(sizeof(std::size_t) == sizeof(std::ptrdiff_t)); - struct range final { using U = std::size_t; @@ -24,57 +17,13 @@ struct range final element min {.i = 0}, max {.i = 0}; type_ type = type_none; - template constexpr std::pair convert() const; + template std::pair convert() const; + friend bool operator==(const range& a, const range& b); }; -template constexpr std::pair range::convert() const -{ - static_assert(sizeof(T) <= sizeof(std::size_t)); - using limits = std::numeric_limits; - - if (type == type_none) - return { limits::min(), limits::max() }; - else - { - if constexpr (std::is_integral_v && std::is_signed_v) - { - fm_assert(type == type_int); - return { T(min.i), T(max.i) }; - } - else if constexpr (std::is_integral_v && std::is_unsigned_v) - { - fm_assert(type == type_uint); - return { T(min.u), T(max.u) }; - } - else - { - fm_assert(type == type_float); - return { T(min.i), T(max.i) }; - } - } -} - -constexpr bool operator==(const range& a, const range& b) -{ - if (a.type != b.type) - return false; - - constexpr float eps = 1e-6f; - - switch (a.type) - { - default: return false; - case range::type_none: return true; - case range::type_float: return std::fabs(a.min.f - b.min.f) < eps && std::fabs(a.max.f - b.max.f) < eps; - case range::type_uint: return a.min.u == b.min.u && a.max.u == b.max.u; - case range::type_int: return a.min.i == b.min.i && a.max.i == b.max.i; - } -} - struct max_length final { - std::size_t value = std::numeric_limits::max(); + std::size_t value = std::size_t(-1); constexpr operator std::size_t() const { return value; } - //constexpr bool operator==(const max_length&) const noexcept = default; }; struct group final { @@ -82,7 +31,6 @@ struct group final { constexpr operator StringView() const { return group_name; } constexpr group() = default; constexpr group(StringView name) : group_name{name} {} - //constexpr bool operator==(const group&) const noexcept = default; }; } // namespace floormat::entities::erased_constraints -- cgit v1.2.3