Strong typedef: Add missing ClampTo, IsInsideMM specialisations

wip-string
Jonathan G Rennison 5 months ago
parent 1d23ae5fdd
commit 1e7b56e13a

@ -10,6 +10,8 @@
#ifndef MATH_FUNC_HPP
#define MATH_FUNC_HPP
#include "strong_typedef_type.hpp"
#include <limits>
#include <type_traits>
@ -164,7 +166,7 @@ static inline uint ClampU(const uint a, const uint min, const uint max)
* for the return type.
* @see Clamp(int, int, int)
*/
template <typename To, typename From>
template <typename To, typename From, std::enable_if_t<std::is_integral<From>::value, int> = 0>
constexpr To ClampTo(From value)
{
static_assert(std::numeric_limits<To>::is_integer, "Do not clamp from non-integer values");
@ -215,6 +217,15 @@ constexpr To ClampTo(From value)
return static_cast<To>(std::min<BiggerType>(value, std::numeric_limits<To>::max()));
}
/**
* Specialization of ClampTo for #StrongType::Typedef.
*/
template <typename To, typename From, std::enable_if_t<std::is_base_of<StrongTypedefBase, From>::value, int> = 0>
constexpr To ClampTo(From value)
{
return ClampTo<To>(value.base());
}
/**
* Returns the (absolute) difference between two (scalar) variables
*
@ -256,10 +267,14 @@ static inline bool IsInsideBS(const T x, const size_t base, const size_t size)
* @param max The maximum of the interval
* @see IsInsideBS()
*/
template <typename T>
static inline constexpr bool IsInsideMM(const T x, const size_t min, const size_t max)
template <typename T, std::enable_if_t<std::disjunction_v<std::is_convertible<T, size_t>, std::is_base_of<StrongTypedefBase, T>>, int> = 0>
static constexpr inline bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
{
return (size_t)(x - min) < (max - min);
if constexpr (std::is_base_of_v<StrongTypedefBase, T>) {
return (size_t)(x.base() - min) < (max - min);
} else {
return (size_t)(x - min) < (max - min);
}
}
/**

Loading…
Cancel
Save