Date: Mon, 9 Jun 2025 23:44:43 +0100
On Mon, Jun 9, 2025 at 11:17 PM Frederick Virchanza Gotham wrote:
>
> I've already shown that you can 'dynamic_cast' from a '
> void * ' to a ' void * ', so long as both the source and destination
> are polymorphic, but I want to take it a step further and try to
> 'dynamic_cast' from a ' void * ' to any polymorphic type. The idea
> being that if you have a polyhandle, then you can use a method called
> 'dynamicCast' to check if you can get any given polymorphic type from
> it
I'll have to test the following code out thoroughly tomorrow . . . but
it might work as it is without change. The stars will really star to
align if the 'dynamicCast' method is implementable for 'polyhandle'.
Oh the possibilities.
#if defined(_WIN32) || defined(_WIN64) // The Microsoft ABI
template<class Tptr> requires (is_pointer_v<Tptr> &&
is_polymorphic_v< remove_pointer_t<Tptr> >)
Tptr std::polyhandle::dynamicCast(void) const noexcept
{
return static_cast<Tptr>(
__RTDynamicCast(
this->most_derived(),
0,
&this->typeinfo(),
&typeid(remove_pointer_t<Tptr>),
false));
}
#else // the Itanium ABI
template<class Tptr> requires (is_pointer_v<Tptr> &&
is_polymorphic_v< remove_pointer_t<Tptr> >)
Tptr std::polyhandle::dynamicCast(void) const noexcept
{
return static_cast<Tptr>(abi::__dynamic_cast(
this->most_derived(),
static_cast< abi::__class_type_info const * >(&this->typeinfo()),
static_cast< abi::__class_type_info const *
>(&typeid(remove_pointer_t<Tptr>)),
-1 // Unknown offset
));
}
#endif
>
> I've already shown that you can 'dynamic_cast' from a '
> void * ' to a ' void * ', so long as both the source and destination
> are polymorphic, but I want to take it a step further and try to
> 'dynamic_cast' from a ' void * ' to any polymorphic type. The idea
> being that if you have a polyhandle, then you can use a method called
> 'dynamicCast' to check if you can get any given polymorphic type from
> it
I'll have to test the following code out thoroughly tomorrow . . . but
it might work as it is without change. The stars will really star to
align if the 'dynamicCast' method is implementable for 'polyhandle'.
Oh the possibilities.
#if defined(_WIN32) || defined(_WIN64) // The Microsoft ABI
template<class Tptr> requires (is_pointer_v<Tptr> &&
is_polymorphic_v< remove_pointer_t<Tptr> >)
Tptr std::polyhandle::dynamicCast(void) const noexcept
{
return static_cast<Tptr>(
__RTDynamicCast(
this->most_derived(),
0,
&this->typeinfo(),
&typeid(remove_pointer_t<Tptr>),
false));
}
#else // the Itanium ABI
template<class Tptr> requires (is_pointer_v<Tptr> &&
is_polymorphic_v< remove_pointer_t<Tptr> >)
Tptr std::polyhandle::dynamicCast(void) const noexcept
{
return static_cast<Tptr>(abi::__dynamic_cast(
this->most_derived(),
static_cast< abi::__class_type_info const * >(&this->typeinfo()),
static_cast< abi::__class_type_info const *
>(&typeid(remove_pointer_t<Tptr>)),
-1 // Unknown offset
));
}
#endif
Received on 2025-06-09 22:44:54