]> git.tdb.fi Git - libs/game.git/blob - source/game/reflection.cpp
Make it possible to find reflected classes by RTTI
[libs/game.git] / source / game / reflection.cpp
1 #include "reflection.h"
2 #include <msp/debug/demangle.h>
3
4 using namespace std;
5
6 namespace Msp::Game {
7 namespace Reflection {
8
9 ClassBase::ClassBase(Reflector &r, type_index t):
10         reflector(r),
11         type(t),
12         name(Debug::demangle(t.name()))
13 { }
14
15 bool ClassBase::is_direct_base_of(const ClassBase &other) const
16 {
17         return ranges::find(other.bases, this)!=other.bases.end();
18 }
19
20 bool ClassBase::is_base_of(const ClassBase &other) const
21 {
22         if(is_direct_base_of(other))
23                 return true;
24         return ranges::any_of(other.bases, [this](const ClassBase *b){ return is_base_of(*b); });
25 }
26
27
28 ClassBase *Reflector::find_class(const type_index &type) const
29 {
30         auto i = lower_bound(type);
31         return (i!=classes.end() && (*i)->get_type()==type ? i->get() : nullptr);
32 }
33
34 } // namespace Reflection
35 } // namespace Msp::Game