#ifndef EXACT_REF_COUNTED_CAST_INL_H_ #error "Direct inclusion of this file is not allowed, include exact_ref_counted_cast.h" // For the sake of sane code completion. #include "exact_ref_counted_cast.h" #endif #include "new.h" #include namespace NYT { //////////////////////////////////////////////////////////////////////////////// template std::conditional_t, const T, T>* ExactRefCountedCast(U* ptr) noexcept { static_assert(std::is_polymorphic_v, "ExactRefCountedCast requires a polymorphic operand type"); static_assert(std::is_base_of_v, "ExactRefCountedCast target must derive from TRefCountedBase"); static_assert(std::is_final_v>, "TRefCountedWrapper must stay final for this cast to remain O(1)"); using TWrapper = std::conditional_t, const TRefCountedWrapper, TRefCountedWrapper>; // Wrapper is final, so dynamic_cast lowers to one type_info compare. return dynamic_cast(ptr); } template std::conditional_t, const T, T>& ExactRefCountedCast(U& ref) { static_assert(std::is_polymorphic_v, "ExactRefCountedCast requires a polymorphic operand type"); static_assert(std::is_base_of_v, "ExactRefCountedCast target must derive from TRefCountedBase"); static_assert(std::is_final_v>, "TRefCountedWrapper must stay final for this cast to remain O(1)"); using TWrapper = std::conditional_t, const TRefCountedWrapper, TRefCountedWrapper>; return dynamic_cast(ref); } //////////////////////////////////////////////////////////////////////////////// } // namespace NYT