Codechange: add concept of ConvertibleThroughBase for strong types

This makes it possible to write templated alternatives for ConvertibleThroughBase
which are then available for any (strong) type that implements a base() function
In the end making adding new ConvertibleThroughBase types less awkward.
This commit is contained in:
Rubidium
2025-02-01 12:57:01 +01:00
committed by rubidium42
parent 3e747397f0
commit 55588b052e
11 changed files with 68 additions and 49 deletions
+7 -10
View File
@@ -10,7 +10,7 @@
#ifndef MATH_FUNC_HPP
#define MATH_FUNC_HPP
#include "strong_typedef_type.hpp"
#include "convertible_through_base.hpp"
/**
* Returns the absolute value of (scalar) variable.
@@ -217,8 +217,8 @@ constexpr To ClampTo(From value)
/**
* 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)
template <typename To>
constexpr To ClampTo(ConvertibleThroughBase auto value)
{
return ClampTo<To>(value.base());
}
@@ -264,16 +264,13 @@ constexpr 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, std::enable_if_t<std::disjunction_v<std::is_convertible<T, size_t>, std::is_base_of<StrongTypedefBase, T>>, int> = 0>
constexpr bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
{
if constexpr (std::is_base_of_v<StrongTypedefBase, T>) {
return static_cast<size_t>(x.base() - min) < (max - min);
} else {
return static_cast<size_t>(x - min) < (max - min);
}
return static_cast<size_t>(x - min) < (max - min);
}
constexpr bool IsInsideMM(const ConvertibleThroughBase auto x, const size_t min, const size_t max) noexcept { return IsInsideMM(x.base(), min, max); }
/**
* Type safe swap operation
* @param a variable to swap with b