#pragma once #include #include #include /** * Helper functions for performing casts. */ namespace dusk::helpers::cast { /** * Implementation details of dusk::helpers::cast. */ namespace _impl { [[noreturn]] void overrun_high(); [[noreturn]] void overrun_low(); } template concept IntCastable = std::is_integral_v && std::is_trivially_copyable_v; /** * Helper type that allows easily casting between integer types, * that safely aborts if an overflow were to occur. * Usage: * @code * size_t foobar = 20; * int real = bounded_cast(foobar); * @endcode */ template struct bounded_cast { Source src; template [[nodiscard]] constexpr operator Target() const { if (std::cmp_greater(src, std::numeric_limits::max())) [[unlikely]] { _impl::overrun_high(); } if (std::cmp_less(src, std::numeric_limits::min())) [[unlikely]] { _impl::overrun_low(); } return static_cast(src); } }; }