]> git.tdb.fi Git - libs/game.git/commitdiff
Check bases of intermediate base classes
authorMikko Rasa <tdb@tdb.fi>
Sat, 27 Jan 2024 09:13:55 +0000 (11:13 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 27 Jan 2024 09:16:36 +0000 (11:16 +0200)
Without this iterating objects of such an intermediate base type does
not work.

source/game/owned.h
source/game/reflection.h

index e51244dfd4284763eaa569d9a8a44529e626191d..359bddca55b73deb8510d6130ef303e2cdb3e3b7 100644 (file)
@@ -69,6 +69,15 @@ Owned<T>::Owned(Handle<P> parent, Args &&... args)
                throw std::invalid_argument("Owned::Owned");
 
        Stage &stage = get_stage(*parent);
+       /* TODO Add a flag to only do this when first_created in an outer call is
+       true. */
+       if(typeid(*parent)==typeid(P))
+       {
+               Reflection::Class<P> &parent_class = stage.get_reflector().get_or_create_class<P>();
+               if(!parent_class.is_up_to_date())
+                       parent_class.template set_polymorphic_base<Entity>(*parent);
+       }
+
        Pool<T> &pool = stage.get_pools().get_pool<T>();
        bool first_created = !pool.get_capacity();
        this->ptr = pool.create(parent, std::forward<Args>(args)...);
index be4b0b6cbd57a94f83200f045fbb0f3aac7145f8..a49048ffb4b23e989966ff0c353793a488cbd8ee 100644 (file)
@@ -49,6 +49,7 @@ protected:
        std::string name;
        std::vector<const ClassBase *> bases;
        std::unique_ptr<PolymorphismBase> polymorphism;
+       unsigned generation = 0;
 
        ClassBase(Reflector &, std::type_index);
 public:
@@ -67,6 +68,8 @@ public:
        template<typename T>
        bool has_polymorphic_base() const { return dynamic_cast<const RootedPolymorphism<T> *>(polymorphism.get()); }
 
+       bool is_up_to_date() const;
+
 protected:
        void increment_generation() const;
 };
@@ -165,6 +168,11 @@ inline bool ClassBase::is_instance(const T &obj) const
        return false;
 }
 
+inline bool ClassBase::is_up_to_date() const
+{
+       return generation==reflector.get_generation();
+}
+
 inline void ClassBase::increment_generation() const
 {
        ++reflector.generation;
@@ -193,6 +201,9 @@ template<typename T>
 template<typename B>
 inline void Class<T>::check_bases(const T &obj)
 {
+       if(typeid(obj)!=type)
+               throw std::invalid_argument("Class::check_bases");
+
        std::vector<const ClassBase *> candidate_bases;
        for(const ClassBase *b: reflector.find_classes_if([](const ClassBase &c){ return c.has_polymorphic_base<B>(); }))
                if(b!=this && b->is_instance<B>(obj))
@@ -211,6 +222,7 @@ inline void Class<T>::check_bases(const T &obj)
                bases = std::move(candidate_bases);
                increment_generation();
        }
+       generation = reflector.get_generation();
 }
 
 } // namespace Reflection