-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtti.cpp
More file actions
99 lines (66 loc) · 1.46 KB
/
rtti.cpp
File metadata and controls
99 lines (66 loc) · 1.46 KB
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
91
92
93
94
95
96
97
98
99
#include <iostream>
#include "rtti.hpp"
#include <iostream>
#include <vector>
int main(int, char** ) {
// basic tests
class base : public rtti::base<base> {
public:
//
protected:
using rtti::base<base>::base;
};
class derived : public base,
public rtti::derived<derived> {
public:
derived() : base(this) { }
};
class other : public base,
public rtti::derived<other> {
public:
other() : base(this) { }
};
base* x = new derived;
base* y = new other;
std::cout << derived::classof(x) << std::endl;
std::cout << other::classof(x) << std::endl;
if( auto cast = rtti::cast<derived>(x) ) {
std::cout << "it's a derived: " << cast << std::endl;
}
std::vector<base*> test;
for(unsigned i = 0; i < 30000000; ++i) {
base* obj = std::rand() % 2 ? (base*) new derived : (base*) new other;
test.push_back( obj );
}
int cd = 0, co = 0;
for(base* obj : test) {
if(rtti::isa<derived>(obj) ) {
cd += 1;
}
if(rtti::isa<other>(obj) ) {
co += 1;
}
}
// multiple inheritance
struct A : rtti::base<A> {
protected:
using base::base;
};
struct B : rtti::base<B> {
protected:
using base::base;
};
struct C : A, B, rtti::derived<C> {
C() : A(this), B(this) { }
};
C* c = new C;
A* a = c;
B* b = c;
if( auto cast = rtti::cast<C>(a) ) {
std::cout << "C" << std::endl;
}
if( auto cast = rtti::cast<C>(b) ) {
std::cout << "C" << std::endl;
}
return 0;
}