#pragma once #include "typetraits.h" #include "yexception.h" #include #include #include #include #include template static inline T VerifyDynamicCast(F f) { if (!f) { return nullptr; } T ret = dynamic_cast(f); Y_ABORT_UNLESS(ret, "verify cast failed"); return ret; } #if !defined(NDEBUG) #define USE_DEBUG_CHECKED_CAST #endif namespace NPrivate { template static T DynamicCast(F f) { return dynamic_cast(f); } } /* * replacement for dynamic_cast(dynamic_cast in debug mode, else static_cast) */ template static inline T CheckedCast(F f) { #if defined(USE_DEBUG_CHECKED_CAST) return VerifyDynamicCast(f); #else /* Make sure F is polymorphic. * Without this cast, CheckedCast with non-polymorphic F * incorrectly compiled without error in release mode. */ { auto&& x = &::NPrivate::DynamicCast; (void)x; } return static_cast(f); #endif // USE_DEBUG_CHECKED_CAST } /* * be polite */ #undef USE_DEBUG_CHECKED_CAST template class TInteger; template <> class TInteger { public: template static constexpr bool IsNegative(TUnsigned) noexcept { return false; } }; template <> class TInteger { public: template static constexpr bool IsNegative(const TSigned value) noexcept { return value < 0; } }; template constexpr bool IsNegative(const TType value) noexcept { return TInteger::value>::IsNegative(value); } namespace NPrivate { template using TUnderlyingTypeOrSelf = typename std::conditional< std::is_enum::value, std::underlying_type, // Lazy evaluatuion: do not call ::type here, because underlying_type is undefined if T is not an enum. std::enable_if // Wrapping T in a class, that has member ::type typedef. >::type::type; // Left ::type is for std::conditional, right ::type is for underlying_type/enable_if template struct TSafelyConvertible { using TSmallInt = TUnderlyingTypeOrSelf; using TLargeInt = TUnderlyingTypeOrSelf; static constexpr bool Result = std::is_integral::value && std::is_integral::value && ((std::is_signed::value == std::is_signed::value && sizeof(TSmallInt) >= sizeof(TLargeInt)) || (std::is_signed::value && sizeof(TSmallInt) > sizeof(TLargeInt))); }; } template constexpr std::enable_if_t<::NPrivate::TSafelyConvertible::Result, TSmallInt> SafeIntegerCast(TLargeInt largeInt) noexcept { return static_cast(largeInt); } template inline std::enable_if_t::Result, TSmall> SafeIntegerCast(TLarge largeInt) { using TSmallInt = ::NPrivate::TUnderlyingTypeOrSelf; using TLargeInt = ::NPrivate::TUnderlyingTypeOrSelf; if (std::is_unsigned::value && std::is_signed::value) { if (IsNegative(largeInt)) { ythrow TBadCastException() << "Conversion '" << TypeName() << '{' << TLargeInt(largeInt) << "}' to '" << TypeName() << "', negative value converted to unsigned"; } } TSmallInt smallInt = TSmallInt(largeInt); if (std::is_signed::value && std::is_unsigned::value) { if (IsNegative(smallInt)) { ythrow TBadCastException() << "Conversion '" << TypeName() << '{' << TLargeInt(largeInt) << "}' to '" << TypeName() << "', positive value converted to negative"; } } if (TLargeInt(smallInt) != largeInt) { ythrow TBadCastException() << "Conversion '" << TypeName() << '{' << TLargeInt(largeInt) << "}' to '" << TypeName() << "', loss of data"; } return static_cast(smallInt); } template inline TSmallInt IntegerCast(TLargeInt largeInt) noexcept { try { return SafeIntegerCast(largeInt); } catch (const yexception& exc) { Y_ABORT("IntegerCast: %s", exc.what()); } } /* Convert given enum value to its underlying type. This is just a shortcut for * `static_cast>(enum_)`. */ template constexpr std::underlying_type_t ToUnderlying(const T enum_) noexcept { return static_cast>(enum_); } // std::bit_cast from c++20 template TTarget BitCast(const TSource& source) { static_assert(sizeof(TSource) == sizeof(TTarget), "Size mismatch"); static_assert(std::is_trivially_copyable::value, "TSource is not trivially copyable"); static_assert(std::is_trivial::value, "TTarget is not trivial"); // Support volatile qualifiers. // ReadUnaligned does not work with volatile pointers, so cast away // volatileness beforehand. using TNonvolatileSource = std::remove_volatile_t; using TNonvolatileTarget = std::remove_volatile_t; return ReadUnaligned(&const_cast(source)); }