Codechange: Add base() method to StrongType to allow access to the base type without casting. (#11445)

This removes the ability to explicitly cast to the base type, but the requirement
to use .base() means the conversion is still explicit.
This commit is contained in:
Peter Nelson
2023-11-06 20:29:35 +00:00
committed by GitHub
parent 737775f834
commit ab535c0a86
73 changed files with 174 additions and 173 deletions

View File

@@ -37,7 +37,7 @@ struct fmt::formatter<T, Char, std::enable_if_t<std::is_base_of<StrongTypedefBas
}
fmt::format_context::iterator format(const T &t, format_context &ctx) const {
return parent::format(underlying_type(t), ctx);
return parent::format(t.base(), ctx);
}
};

View File

@@ -220,7 +220,7 @@ constexpr To ClampTo(From value)
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>(static_cast<typename From::BaseType>(value));
return ClampTo<To>(value.base());
}
/**
@@ -268,7 +268,7 @@ template <typename T, std::enable_if_t<std::disjunction_v<std::is_convertible<T,
static constexpr inline bool IsInsideMM(const T x, const size_t min, const size_t max) noexcept
{
if constexpr (std::is_base_of_v<StrongTypedefBase, T>) {
return (size_t)(static_cast<typename T::BaseType>(x) - min) < (max - min);
return (size_t)(x.base() - min) < (max - min);
} else {
return (size_t)(x - min) < (max - min);
}

View File

@@ -160,10 +160,10 @@ namespace StrongType {
constexpr Typedef &operator =(Typedef &&rhs) { this->value = std::move(rhs.value); return *this; }
constexpr Typedef &operator =(const TBaseType &rhs) { this->value = rhs; return *this; }
/* Only allow explicit conversions to BaseType. */
explicit constexpr operator TBaseType () const { return this->value; }
/* Only allow conversion to BaseType via method. */
constexpr TBaseType base() const { return this->value; }
/* Only allow TProperties classes access to the internal value. Everyone else needs to do an explicit cast. */
/* Only allow TProperties classes access to the internal value. Everyone else needs to call .base(). */
friend struct Compare;
friend struct Integer;
template <typename TCompatibleType> friend struct Compatible;
@@ -171,7 +171,7 @@ namespace StrongType {
/* GCC / MSVC don't pick up on the "friend struct" above, where CLang does.
* As in our CI we compile for all three targets, it is sufficient to have one
* that errors on this; but nobody should be using "value" directly. Instead,
* use a static_cast<> to convert to the base type. */
* use base() to convert to the base type. */
#ifdef __clang__
protected:
#endif /* __clang__ */