From: Clyne Sullivan Date: Sat, 20 Aug 2022 13:46:05 +0000 (-0400) Subject: add serialization support X-Git-Url: https://code.bitgloo.com/?a=commitdiff_plain;h=e04b856006cfe6b7e8ba5d6c1434331351cc791a;p=clyne%2Fentityx.git add serialization support --- diff --git a/entityx/Entity.h b/entityx/Entity.h index 418523d..f15d779 100644 --- a/entityx/Entity.h +++ b/entityx/Entity.h @@ -242,6 +242,8 @@ struct BaseComponent { void operator delete(void *p) { fail(); } void operator delete[](void *p) { fail(); } + virtual std::string serializeName() const = 0; + virtual void internal_serialize(bool save, void *ar) = 0; protected: static void fail() { @@ -341,6 +343,7 @@ public: virtual ~BaseComponentHelper() {} virtual void remove_component(Entity e) = 0; virtual void copy_component_to(Entity source, Entity target) = 0; + virtual BaseComponent *get_component(Entity e) = 0; }; template @@ -352,6 +355,9 @@ public: void copy_component_to(Entity source, Entity target) override { target.assign_from_copy(*(source.component().get())); } + BaseComponent *get_component(Entity e) override { + return e.component().get(); + } }; /** @@ -838,6 +844,19 @@ class EntityManager : entityx::help::NonCopyable { return Component::type>::family(); } + template + void serialize(Entity entity, Serializer serializer) { + assert(entity.valid()); + auto mask = entity.component_mask(); + for (size_t i = 0; i < component_helpers_.size(); i++) { + BaseComponentHelper *helper = component_helpers_[i]; + if (helper && mask.test(i)) { + auto c = helper->get_component(entity); + serializer(c); + } + } + } + private: friend class Entity; template