From: Mikko Rasa Date: Wed, 7 Dec 2022 09:52:03 +0000 (+0200) Subject: Make it possible to find reflected classes by RTTI X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=3643555f600e4580059f75032135ee4f452cf18b;p=libs%2Fgame.git Make it possible to find reflected classes by RTTI --- diff --git a/source/game/reflection.cpp b/source/game/reflection.cpp index d188e50..43dd13a 100644 --- a/source/game/reflection.cpp +++ b/source/game/reflection.cpp @@ -24,5 +24,12 @@ bool ClassBase::is_base_of(const ClassBase &other) const return ranges::any_of(other.bases, [this](const ClassBase *b){ return is_base_of(*b); }); } + +ClassBase *Reflector::find_class(const type_index &type) const +{ + auto i = lower_bound(type); + return (i!=classes.end() && (*i)->get_type()==type ? i->get() : nullptr); +} + } // namespace Reflection } // namespace Msp::Game diff --git a/source/game/reflection.h b/source/game/reflection.h index df974b1..6d8535d 100644 --- a/source/game/reflection.h +++ b/source/game/reflection.h @@ -93,8 +93,10 @@ private: std::vector>::const_iterator lower_bound(const std::type_index &) const; public: + ClassBase *find_class(const std::type_index &) const; + template - Class *find_class() const; + Class *find_class() const { return static_cast *>(find_class(typeid(T))); } template Class &get_or_create_class(); @@ -109,14 +111,6 @@ inline std::vector>::const_iterator Reflector::lower_ return std::ranges::lower_bound(classes, type, {}, [](auto &c){ return c->get_type(); }); } -template -inline Class *Reflector::find_class() const -{ - std::type_index type = typeid(T); - auto i = lower_bound(type); - return (i!=classes.end() && (*i)->get_type()==type ? static_cast *>(i->get()) : nullptr); -} - template inline Class &Reflector::get_or_create_class() {