From: Mikko Rasa Date: Sun, 7 May 2023 12:27:46 +0000 (+0300) Subject: Add a generation number to Reflector X-Git-Url: https://git.tdb.fi/?a=commitdiff_plain;h=486d7f61820eaf066476d284c881e64579ee839c;p=libs%2Fgame.git Add a generation number to Reflector --- diff --git a/source/game/reflection.h b/source/game/reflection.h index 7b75541..d42c860 100644 --- a/source/game/reflection.h +++ b/source/game/reflection.h @@ -65,6 +65,9 @@ public: template bool has_polymorphic_base() const { return dynamic_cast *>(polymorphism.get()); } + +protected: + void increment_generation() const; }; @@ -88,8 +91,11 @@ private: class MSPGAME_API Reflector { + friend class ClassBase; + private: std::vector> classes; + unsigned generation = 0; std::vector>::const_iterator lower_bound(const std::type_index &) const; @@ -104,6 +110,8 @@ public: template std::vector find_classes_if(F &&) const; + + unsigned get_generation() const { return generation; } }; @@ -118,7 +126,10 @@ inline Class &Reflector::get_or_create_class() std::type_index type = typeid(T); auto i = lower_bound(type); if(i==classes.end() || (*i)->get_type()!=type) + { i = classes.emplace(i, std::make_unique>(std::ref(*this))); + ++generation; + } return static_cast &>(*i->get()); } @@ -152,6 +163,11 @@ inline bool ClassBase::is_instance(const T &obj) const return false; } +inline void ClassBase::increment_generation() const +{ + ++reflector.generation; +} + template template @@ -180,7 +196,11 @@ inline void Class::check_bases(const T &obj) ++i; } - bases = std::move(candidate_bases); + if(candidate_bases!=bases) + { + bases = std::move(candidate_bases); + increment_generation(); + } } } // namespace Reflection