1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include <typeinfo>
#include <typeindex>
#include <vector>
#include <memory>
#include <iostream>
struct entity
{
virtual ~entity() {}
virtual bool is( const std::type_index& t ) const
{ return t == typeid(entity) ; }
// ...
};
struct A : virtual entity
{
virtual bool is( const std::type_index& t ) const override
{ return t == typeid(A) || entity::is(t) ; }
// ...
};
struct B : virtual entity
{
virtual bool is( const std::type_index& t ) const override
{ return t == typeid(B) || entity::is(t) ; }
// ...
};
struct C : A
{
virtual bool is( const std::type_index& t ) const override
{ return t == typeid(C) || A::is(t) ; }
};
struct D : B
{
virtual bool is( const std::type_index& t ) const override
{ return t == typeid(D) || B::is(t) ; }
};
struct E : C, D
{
virtual bool is( const std::type_index& t ) const override
{ return t == typeid(E) || C::is(t) || D::is(t) ; }
};
int main()
{
std::vector< std::shared_ptr<entity> > seq =
{
std::make_shared<E>(),
std::make_shared<A>(),
std::make_shared<B>(),
std::make_shared<entity>(),
std::make_shared<C>(),
std::make_shared<D>(),
std::make_shared<A>(),
std::make_shared<E>(),
std::make_shared<C>(),
std::make_shared<entity>(),
std::make_shared<B>(),
std::make_shared<E>(),
std::make_shared<D>()
};
const std::vector< std::type_index > type_keys =
{ typeid(entity), typeid(A), typeid(B), typeid(C), typeid(D), typeid(E) } ;
for( const auto& type : type_keys )
{
std::cout << "is of type " << type.name() << ": " ;
for( std::size_t i = 0 ; i < seq.size() ; ++i )
if( seq[i]->is(type) ) std::cout << i << ' ' ;
std::cout << '\n' ;
}
}
|