forked from boostorg/type_index
Split runtime_cast into several files so clients can selectively include only what they use.
Add support for shared_ptrs.
This commit is contained in:
1 parent
0b006b6af6
commit
12751e0f83
9 files changed
+339
-124
No files matched your search
+1
-1
@@ -40,7 +40,7 @@ exe testing_crossmodule_anonymous_no_rtti : testing_crossmodule_anonymous.cpp te
|
||||
test-suite type_index
|
||||
:
|
||||
[ run type_index_test.cpp ]
|
||||
[ run runtime_cast_test.cpp ]
|
||||
[ run type_index_runtime_cast_test.cpp ]
|
||||
[ run type_index_constexpr_test.cpp ]
|
||||
[ run type_index_test.cpp : : : <rtti>off $(norttidefines) : type_index_test_no_rtti ]
|
||||
[ run ctti_print_name.cpp : : : <test-info>always_show_run_output ]
|
||||
|
||||
@@ -5,7 +5,13 @@
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// #include <boost/type_index/runtime_cast.hpp>
|
||||
// #include <boost/type_index/runtime_reference_cast.hpp>
|
||||
|
||||
#include <boost/type_index/runtime_cast.hpp>
|
||||
#include <boost/type_index/runtime_cast/std_shared_ptr_cast.hpp>
|
||||
#include <boost/type_index/runtime_cast/boost_shared_ptr_cast.hpp>
|
||||
#include <boost/smart_ptr/make_shared.hpp>
|
||||
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
@@ -20,72 +26,72 @@
|
||||
std::string name;
|
||||
|
||||
struct base {
|
||||
BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI
|
||||
BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS
|
||||
IMPLEMENT_CLASS(base)
|
||||
};
|
||||
|
||||
struct single_derived : base {
|
||||
BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI_BASES((base))
|
||||
BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS_BASES((base))
|
||||
IMPLEMENT_CLASS(single_derived)
|
||||
};
|
||||
|
||||
struct base1 {
|
||||
BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI
|
||||
BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS
|
||||
IMPLEMENT_CLASS(base1)
|
||||
};
|
||||
|
||||
struct base2 {
|
||||
BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI
|
||||
BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS
|
||||
IMPLEMENT_CLASS(base2)
|
||||
};
|
||||
|
||||
struct multiple_derived : base1, base2 {
|
||||
BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI_BASES((base1)(base2))
|
||||
BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS_BASES((base1)(base2))
|
||||
IMPLEMENT_CLASS(multiple_derived)
|
||||
};
|
||||
|
||||
struct baseV1 : virtual base {
|
||||
BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI_BASES((base))
|
||||
BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS_BASES((base))
|
||||
IMPLEMENT_CLASS(baseV1)
|
||||
};
|
||||
|
||||
struct baseV2 : virtual base {
|
||||
BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI_BASES((base))
|
||||
BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS_BASES((base))
|
||||
IMPLEMENT_CLASS(baseV2)
|
||||
};
|
||||
|
||||
struct multiple_virtual_derived : baseV1, baseV2 {
|
||||
BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI_BASES((baseV1)(baseV2))
|
||||
BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS_BASES((baseV1)(baseV2))
|
||||
IMPLEMENT_CLASS(multiple_virtual_derived)
|
||||
};
|
||||
|
||||
struct unrelated {
|
||||
BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI
|
||||
BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS
|
||||
IMPLEMENT_CLASS(unrelated)
|
||||
};
|
||||
|
||||
struct unrelated_with_base : base {
|
||||
BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI_BASES((base))
|
||||
BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS_BASES((base))
|
||||
IMPLEMENT_CLASS(unrelated_with_base)
|
||||
};
|
||||
|
||||
struct unrelatedV1 : virtual base {
|
||||
BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI_BASES((base))
|
||||
BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS_BASES((base))
|
||||
IMPLEMENT_CLASS(unrelatedV1)
|
||||
};
|
||||
|
||||
struct level1_a : base {
|
||||
BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI_BASES((base))
|
||||
BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS_BASES((base))
|
||||
IMPLEMENT_CLASS(level1_a)
|
||||
};
|
||||
|
||||
struct level1_b : base {
|
||||
BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI_BASES((base))
|
||||
BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS_BASES((base))
|
||||
IMPLEMENT_CLASS(level1_b)
|
||||
};
|
||||
|
||||
struct level2 : level1_a, level1_b {
|
||||
BOOST_TYPE_INDEX_REGISTER_CLASS_RTTI_BASES((level1_a)(level1_b))
|
||||
BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS_BASES((level1_a)(level1_b))
|
||||
IMPLEMENT_CLASS(level2)
|
||||
};
|
||||
|
||||
@@ -221,9 +227,30 @@ void diamond_non_virtual()
|
||||
level1_a* l1a = &inst;
|
||||
base* b1 = l1a;
|
||||
level1_b* l1_b = runtime_cast<level1_b*>(b1);
|
||||
BOOST_TEST_EQ(l1_b->name, "level1_b");
|
||||
BOOST_TEST_NE(l1_b, (level1_b*)nullptr);
|
||||
}
|
||||
|
||||
void boost_shared_ptr()
|
||||
{
|
||||
using namespace boost::typeindex;
|
||||
boost::shared_ptr<single_derived> d = boost::make_shared<single_derived>();
|
||||
boost::shared_ptr<base> b = d;
|
||||
boost::shared_ptr<single_derived> d2 = runtime_pointer_cast<single_derived>(b);
|
||||
BOOST_TEST_NE(d2, boost::shared_ptr<single_derived>());
|
||||
BOOST_TEST_EQ(d2->name, "single_derived");
|
||||
}
|
||||
|
||||
void std_shared_ptr()
|
||||
{
|
||||
using namespace boost::typeindex;
|
||||
std::shared_ptr<single_derived> d = std::make_shared<single_derived>();
|
||||
std::shared_ptr<base> b = d;
|
||||
std::shared_ptr<single_derived> d2 = runtime_pointer_cast<single_derived>(b);
|
||||
BOOST_TEST_NE(d2, std::shared_ptr<single_derived>());
|
||||
BOOST_TEST_EQ(d2->name, "single_derived");
|
||||
}
|
||||
|
||||
int main() {
|
||||
no_base();
|
||||
single_derived();
|
||||
Reference in new issue
Block a user