mirror of
https://github.com/TomHarte/CLK.git
synced 2025-01-23 11:30:24 +00:00
Switch to a saturating conversion.
This commit is contained in:
parent
693d46f8ea
commit
c750bdafd5
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
#include "ForceInline.hpp"
|
#include "ForceInline.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
@ -138,8 +139,11 @@ template <class T> class WrappedInt {
|
|||||||
forceinline constexpr bool operator !() const { return !length_; }
|
forceinline constexpr bool operator !() const { return !length_; }
|
||||||
// bool operator () is not supported because it offers an implicit cast to int, which is prone silently to permit misuse
|
// bool operator () is not supported because it offers an implicit cast to int, which is prone silently to permit misuse
|
||||||
|
|
||||||
/// @returns The underlying int, cast to an integral type of your choosing.
|
/// @returns The underlying int, converted to an integral type of your choosing, clamped to that int's range.
|
||||||
template<typename Type = IntType> forceinline constexpr Type as() const { return Type(length_); }
|
template<typename Type = IntType> forceinline constexpr Type as() const {
|
||||||
|
const auto clamped = std::clamp(length_, IntType(std::numeric_limits<Type>::min()), IntType(std::numeric_limits<Type>::max()));
|
||||||
|
return Type(clamped);
|
||||||
|
}
|
||||||
|
|
||||||
/// @returns The underlying int, in its native form.
|
/// @returns The underlying int, in its native form.
|
||||||
forceinline constexpr IntType as_integral() const { return length_; }
|
forceinline constexpr IntType as_integral() const { return length_; }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user