forked from boostorg/type_index
Merge branch 'runtime_cast' of github.com:cdglove/type_index into runtime_cast
# Conflicts: # include/boost/type_index/runtime_cast.hpp # test/runtime_cast_test.cpp
This commit is contained in:
@ -17,7 +17,9 @@
|
||||
/// or undesirable at a global level.
|
||||
|
||||
#include <boost/type_index.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <type_traits>
|
||||
#include <typeinfo>
|
||||
|
||||
#ifdef BOOST_HAS_PRAGMA_ONCE
|
||||
# pragma once
|
||||
@ -27,80 +29,112 @@ namespace boost { namespace typeindex {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<class Current, class... BaseList>
|
||||
struct find_type;
|
||||
|
||||
template<class Current>
|
||||
struct find_type<Current> {
|
||||
template<typename T>
|
||||
void* operator()(T* p, type_index const& idx) const BOOST_NOEXCEPT {
|
||||
if(idx == boost::typeindex::type_id<Current>())
|
||||
return nullptr;
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
template<class Current, class... BaseList>
|
||||
struct find_type {
|
||||
template<class T>
|
||||
Current* check_current(T* p, type_index const& idx) const BOOST_NOEXCEPT{
|
||||
if(idx == boost::typeindex::type_id<Current>())
|
||||
return nullptr;
|
||||
return nullptr;
|
||||
}
|
||||
template<class T>
|
||||
Current* check_current(T* p, type_index const& idx) const BOOST_NOEXCEPT {
|
||||
if(idx == boost::typeindex::type_id<Current>())
|
||||
return p;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template<class T, class FirstBase, class... Rest>
|
||||
void* check_bases(T* p, type_index const& idx) const BOOST_NOEXCEPT {
|
||||
if(void* result = p->FirstBase::boost_type_index_find_instance_(idx))
|
||||
return result;
|
||||
return check_bases<T, Rest...>(p, idx);
|
||||
}
|
||||
template<class T>
|
||||
void* check_bases(T* p, type_index const& idx) const BOOST_NOEXCEPT {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void* operator()(T* p, type_index const& idx) const BOOST_NOEXCEPT {
|
||||
if(Current* current = check_current(p, idx))
|
||||
return p;
|
||||
return check_bases<T, BaseList...>(p, idx);
|
||||
}
|
||||
template<class T, class FirstBase, class... Rest>
|
||||
void* check_bases(T* p, type_index const& idx) const BOOST_NOEXCEPT {
|
||||
if(void* result = p->FirstBase::boost_type_index_find_instance_(idx))
|
||||
return result;
|
||||
return check_bases<T, Rest...>(p, idx);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void* operator()(T* p, type_index const& idx) const BOOST_NOEXCEPT {
|
||||
if(Current* current = check_current(p, idx))
|
||||
return p;
|
||||
return check_bases<T, BaseList...>(p, idx);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T, typename U>
|
||||
T* runtime_cast_impl(U* u) {
|
||||
return static_cast<T*>(
|
||||
u->boost_type_index_find_instance_(boost::typeindex::type_id<T>())
|
||||
);
|
||||
T* runtime_cast_impl(U* u, std::true_type) {
|
||||
return u;
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
T const* runtime_cast_impl(U const* u) {
|
||||
return static_cast<T const*>(
|
||||
const_cast<U*>(u)->boost_type_index_find_instance_(boost::typeindex::type_id<T>())
|
||||
);
|
||||
T const* runtime_cast_impl(U const* u, std::true_type) {
|
||||
return u;
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
T* runtime_cast_impl(U* u, std::false_type) {
|
||||
return static_cast<T*>(
|
||||
u->boost_type_index_find_instance_(boost::typeindex::type_id<T>())
|
||||
);
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
T const* runtime_cast_impl(U const* u, std::false_type) {
|
||||
return static_cast<T const*>(
|
||||
const_cast<U*>(u)->boost_type_index_find_instance_(boost::typeindex::type_id<T>())
|
||||
);
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
#define BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI \
|
||||
virtual void* boost_type_index_find_instance_(boost::typeindex::type_index const& idx) BOOST_NOEXCEPT { \
|
||||
return boost::typeindex::detail::find_type<std::decay<decltype(*this)>::type>()(this, idx); \
|
||||
}
|
||||
#define BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI \
|
||||
virtual void* boost_type_index_find_instance_(boost::typeindex::type_index const& idx) BOOST_NOEXCEPT { \
|
||||
if(idx == boost::typeindex::type_id<std::decay<decltype(*this)>::type>()) \
|
||||
return this; \
|
||||
return nullptr; \
|
||||
}
|
||||
|
||||
#define BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI_BASES(...) \
|
||||
virtual void* boost_type_index_find_instance_(boost::typeindex::type_index const& idx) BOOST_NOEXCEPT { \
|
||||
if(auto ret = boost::typeindex::detail::find_type<std::decay<decltype(*this)>::type>()(this, idx)) \
|
||||
return ret; \
|
||||
return boost::typeindex::detail::find_type<std::decay<decltype(*this)>::type, __VA_ARGS__>()(this, idx);\
|
||||
}
|
||||
#define BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI_BASES(...) \
|
||||
virtual void* boost_type_index_find_instance_(boost::typeindex::type_index const& idx) BOOST_NOEXCEPT { \
|
||||
return boost::typeindex::detail::find_type<std::decay<decltype(*this)>::type, __VA_ARGS__>()(this, idx);\
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
T* runtime_cast(U* u) {
|
||||
return detail::runtime_cast_impl<T>(u);
|
||||
}
|
||||
template<typename T, typename U>
|
||||
T runtime_cast(U* u) BOOST_NOEXCEPT {
|
||||
typedef typename std::remove_pointer<T>::type impl_type;
|
||||
return detail::runtime_cast_impl<impl_type>(u, std::is_same<T, U>());
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
T const* runtime_cast(U const* u) {
|
||||
return detail::runtime_cast_impl<T>(u);
|
||||
}
|
||||
template<typename T, typename U>
|
||||
T runtime_cast(U const* u) BOOST_NOEXCEPT {
|
||||
typedef typename std::remove_pointer<T>::type impl_type;
|
||||
return detail::runtime_cast_impl<impl_type>(u, std::is_same<T, U>());
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
T runtime_cast(U& u) {
|
||||
typedef typename std::remove_reference<T>::type impl_type;
|
||||
impl_type* value = detail::runtime_cast_impl<impl_type>(&u, std::is_same<T, U>());
|
||||
if(!value)
|
||||
boost::throw_exception(std::bad_cast());
|
||||
return *value;
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
T runtime_cast(U const& u) {
|
||||
typedef typename std::remove_reference<T>::type impl_type;
|
||||
impl_type* value = detail::runtime_cast_impl<impl_type>(&u, std::is_same<T, U>());
|
||||
if(!value)
|
||||
boost::throw_exception(std::bad_cast());
|
||||
return *value;
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
T* runtime_pointer_cast(U* u) BOOST_NOEXCEPT {
|
||||
return detail::runtime_cast_impl<T>(u, std::is_same<T, U>());
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
T const* runtime_pointer_cast(U const* u) BOOST_NOEXCEPT {
|
||||
return detail::runtime_cast_impl<T>(u, std::is_same<T, U>());
|
||||
}
|
||||
|
||||
}} // namespace boost::typeindex
|
||||
|
||||
|
@ -59,13 +59,30 @@ struct multiple_virtual_derived : baseV1, baseV2 {
|
||||
IMPLEMENT_CLASS(multiple_virtual_derived)
|
||||
};
|
||||
|
||||
struct unrelated {
|
||||
BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI
|
||||
};
|
||||
|
||||
struct unrelated_with_base : base {
|
||||
BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI_BASES(base)
|
||||
};
|
||||
|
||||
struct unrelatedV1 : virtual base {
|
||||
BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI_BASES(base)
|
||||
};
|
||||
|
||||
void no_base()
|
||||
{
|
||||
using namespace boost::typeindex;
|
||||
base b;
|
||||
base* b2 = runtime_cast<base>(&b);
|
||||
base* b2 = runtime_pointer_cast<base>(&b);
|
||||
BOOST_TEST_NE(b2, (base*)nullptr);
|
||||
BOOST_TEST_EQ(b2->name, "base");
|
||||
|
||||
BOOST_TEST_EQ(runtime_pointer_cast<unrelated>(&b), (unrelated*)nullptr);
|
||||
BOOST_TEST_EQ(runtime_pointer_cast<single_derived>(&b), (single_derived*)nullptr);
|
||||
BOOST_TEST_EQ(runtime_pointer_cast<unrelatedV1>(&b), (unrelatedV1*)nullptr);
|
||||
BOOST_TEST_EQ(runtime_pointer_cast<unrelated_with_base>(&b), (unrelated_with_base*)nullptr);
|
||||
}
|
||||
|
||||
void single_base()
|
||||
@ -73,9 +90,13 @@ void single_base()
|
||||
using namespace boost::typeindex;
|
||||
single_derived d;
|
||||
base* b = &d;
|
||||
single_derived* d2 = runtime_cast<single_derived>(b);
|
||||
single_derived* d2 = runtime_pointer_cast<single_derived>(b);
|
||||
BOOST_TEST_NE(d2, (single_derived*)nullptr);
|
||||
BOOST_TEST_EQ(d2->name, "single_derived");
|
||||
|
||||
BOOST_TEST_EQ(runtime_pointer_cast<unrelated>(&d), (unrelated*)nullptr);
|
||||
BOOST_TEST_EQ(runtime_pointer_cast<unrelated>(b), (unrelated*)nullptr);
|
||||
BOOST_TEST_EQ(runtime_pointer_cast<unrelated_with_base>(b), (unrelated_with_base*)nullptr);
|
||||
}
|
||||
|
||||
void multiple_base()
|
||||
@ -83,13 +104,17 @@ void multiple_base()
|
||||
using namespace boost::typeindex;
|
||||
multiple_derived d;
|
||||
base1* b1 = &d;
|
||||
multiple_derived* d2 = runtime_cast<multiple_derived>(b1);
|
||||
multiple_derived* d2 = runtime_pointer_cast<multiple_derived>(b1);
|
||||
BOOST_TEST_NE(d2, (multiple_derived*)nullptr);
|
||||
BOOST_TEST_EQ(d2->name, "multiple_derived");
|
||||
|
||||
base2* b2 = runtime_cast<base2>(b1);
|
||||
base2* b2 = runtime_pointer_cast<base2>(b1);
|
||||
BOOST_TEST_NE(b2, (base2*)nullptr);
|
||||
BOOST_TEST_EQ(b2->name, "base2");
|
||||
|
||||
BOOST_TEST_EQ(runtime_pointer_cast<unrelated>(&d), (unrelated*)nullptr);
|
||||
BOOST_TEST_EQ(runtime_pointer_cast<unrelated>(b1), (unrelated*)nullptr);
|
||||
BOOST_TEST_EQ(runtime_pointer_cast<unrelated_with_base>(b1), (unrelated_with_base*)nullptr);
|
||||
}
|
||||
|
||||
void virtual_base()
|
||||
@ -97,9 +122,9 @@ void virtual_base()
|
||||
using namespace boost::typeindex;
|
||||
multiple_virtual_derived d;
|
||||
base* b = &d;
|
||||
multiple_virtual_derived* d2 = runtime_cast<multiple_virtual_derived>(b);
|
||||
baseV1* bv1 = runtime_cast<baseV1>(b);
|
||||
baseV2* bv2 = runtime_cast<baseV2>(b);
|
||||
multiple_virtual_derived* d2 = runtime_pointer_cast<multiple_virtual_derived>(b);
|
||||
baseV1* bv1 = runtime_pointer_cast<baseV1>(b);
|
||||
baseV2* bv2 = runtime_pointer_cast<baseV2>(b);
|
||||
|
||||
BOOST_TEST_NE(d2, (multiple_virtual_derived*)nullptr);
|
||||
BOOST_TEST_EQ(d2->name, "multiple_virtual_derived");
|
||||
@ -109,13 +134,77 @@ void virtual_base()
|
||||
|
||||
BOOST_TEST_NE(bv2, (baseV2*)nullptr);
|
||||
BOOST_TEST_EQ(bv2->name, "baseV2");
|
||||
|
||||
BOOST_TEST_EQ(runtime_pointer_cast<unrelated>(b), (unrelated*)nullptr);
|
||||
BOOST_TEST_EQ(runtime_pointer_cast<unrelated>(&d), (unrelated*)nullptr);
|
||||
BOOST_TEST_EQ(runtime_pointer_cast<unrelated_with_base>(b), (unrelated_with_base*)nullptr);
|
||||
}
|
||||
|
||||
void pointer_interface()
|
||||
{
|
||||
using namespace boost::typeindex;
|
||||
single_derived d;
|
||||
base* b = &d;
|
||||
single_derived* d2 = runtime_cast<single_derived*>(b);
|
||||
BOOST_TEST_NE(d2, (single_derived*)nullptr);
|
||||
BOOST_TEST_EQ(d2->name, "single_derived");
|
||||
BOOST_TEST_EQ(runtime_pointer_cast<unrelated>(b), (unrelated*)nullptr);
|
||||
}
|
||||
|
||||
void reference_interface()
|
||||
{
|
||||
using namespace boost::typeindex;
|
||||
single_derived d;
|
||||
base& b = d;
|
||||
single_derived& d2 = runtime_cast<single_derived&>(b);
|
||||
BOOST_TEST_EQ(d2.name, "single_derived");
|
||||
|
||||
try {
|
||||
unrelated& u = runtime_cast<unrelated&>(b);
|
||||
(void)u;
|
||||
BOOST_TEST(!"should throw bad_cast");
|
||||
}
|
||||
catch(...) {
|
||||
}
|
||||
}
|
||||
|
||||
void const_pointer_interface()
|
||||
{
|
||||
using namespace boost::typeindex;
|
||||
const single_derived d;
|
||||
base const* b = &d;
|
||||
single_derived const* d2 = runtime_cast<single_derived const*>(b);
|
||||
BOOST_TEST_NE(d2, (single_derived*)nullptr);
|
||||
BOOST_TEST_EQ(d2->name, "single_derived");
|
||||
BOOST_TEST_EQ(runtime_pointer_cast<unrelated>(b), (unrelated*)nullptr);
|
||||
}
|
||||
|
||||
void const_reference_interface()
|
||||
{
|
||||
using namespace boost::typeindex;
|
||||
const single_derived d;
|
||||
base const& b = d;
|
||||
single_derived const& d2 = runtime_cast<single_derived const&>(b);
|
||||
BOOST_TEST_EQ(d2.name, "single_derived");
|
||||
|
||||
try {
|
||||
unrelated const& u = runtime_cast<unrelated const&>(b);
|
||||
(void)u;
|
||||
BOOST_TEST(!"should throw bad_cast");
|
||||
}
|
||||
catch(...) {
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
no_base();
|
||||
single_derived();
|
||||
multiple_base();
|
||||
virtual_base();
|
||||
multiple_base();
|
||||
virtual_base();
|
||||
pointer_interface();
|
||||
reference_interface();
|
||||
const_pointer_interface();
|
||||
const_reference_interface();
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user