-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrtti.hpp
More file actions
59 lines (40 loc) · 1.04 KB
/
rtti.hpp
File metadata and controls
59 lines (40 loc) · 1.04 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
#ifndef RTTI_HPP
#define RTTI_HPP
#include <typeindex>
// llvm-style rtti using function pointers as type ids
namespace rtti {
template<class Base>
class base {
using type_id = const void* (*)();
const type_id type;
protected:
template<class Derived>
base(Derived* ) : type( &get_type_id<Derived> ) { }
public:
using base_type = Base;
type_id get_type() const { return type; }
template<class Derived>
static const void* get_type_id() {
static const char unique = 0;
return &unique;
};
};
template<class Derived>
class derived {
public:
template<class Self = Derived, class Base>
static bool classof(const Base* obj) {
return obj->get_type() == &Base::template get_type_id<Derived>;
}
};
// note: only down-casts are supported
template<class Derived, class Base>
static bool isa(const Base* obj) {
return Derived::classof(obj);
}
template<class Derived, class Base>
static Derived* cast(Base* obj) {
return Derived::classof(obj) ? static_cast<Derived*>(obj) : nullptr;
}
}
#endif