summaryrefslogtreecommitdiff
path: root/libs/type_index/examples/runtime_cast.cpp
blob: 43e7aa70f4ca1f295eda8444259ab4837fdac248 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
//
// Copyright (c) Chris Glover, 2016.
//
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

//[type_index_runtime_cast_example
/*`
    The following example shows that `runtime_cast` is able to find a valid pointer
    in various class hierarchies regardless of inheritance or type relation.

    Example works with and without RTTI."
*/

#include <boost/type_index/runtime_cast.hpp>
#include <iostream>

struct A {
    BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS(BOOST_TYPE_INDEX_NO_BASE_CLASS)
    virtual ~A()
    {}
};

struct B {
    BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS(BOOST_TYPE_INDEX_NO_BASE_CLASS)
    virtual ~B()
    {}
};

struct C : A {
    BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS((A))
};

struct D : B {
    BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS((B))
};

struct E : C, D {
    BOOST_TYPE_INDEX_REGISTER_RUNTIME_CLASS((C)(D))
};

int main() {
    C c;
    A* a = &c;

    if(C* cp = boost::typeindex::runtime_cast<C*>(a)) {
        std::cout << "Yes, a points to a C: "
                  << a << "->" << cp << '\n';
    }
    else {
        std::cout << "Error: Expected a to point to a C" << '\n';
    }

    if(E* ce = boost::typeindex::runtime_cast<E*>(a)) {
        std::cout << "Error: Expected a to not points to an E: "
                  << a << "->" << ce << '\n';
    }
    else {
        std::cout << "But, a does not point to an E" << '\n';
    }

    E e;
    C* cp2 = &e;
    if(D* dp = boost::typeindex::runtime_cast<D*>(cp2)) {
        std::cout << "Yes, we can cross-cast from a C* to a D* when we actually have an E: "
                  << cp2 << "->" << dp << '\n';
    }
    else {
        std::cout << "Error: Expected cp to point to a D" << '\n';
    }

/*`
    Alternatively, we can use runtime_pointer_cast so we don't need to specity the target as a pointer.
    This works for smart_ptr types too.
*/
    A* ap = &e;
    if(B* bp = boost::typeindex::runtime_pointer_cast<B>(ap)) {
        std::cout << "Yes, we can cross-cast and up-cast at the same time."
                  << ap << "->" << bp << '\n';
    }
    else {
        std::cout << "Error: Expected ap to point to a B" << '\n';
    }

    return 0;
}

//] [/type_index_runtime_cast_example]