I am currently developing an ECS system for a game engine, and stumbled across a problem with serialization. The data structure I use for component storage in my ECS implementation, is a pool of preconstructed components, which are recycled. Therefore adding an entity is as trivial as assigning values to the preconstructed components. The system is designed to utilize component indetifiers instead of types, and this is where the problem lies. When serializing and deserializing the components are treated as BaseComponent pointers.
Component struct hierachy
struct BaseComponent {}; // struct used as base to store components
template<typename T>
struct Component : public BaseComponent {
static const uint_fast32_t ID; // identifier used by the rest of the system
}
struct SomeComponent : public Component<SomeComponent> {
// component specific data
}
It is the component specific data that I want to serialize, deserialize and assign the data to the appropriate fields in SomeComponent.
I have a simple solution, however it is quite shady when it comes to clean code. The solution I found is to dump the components memory directly into a file and read it into memory via a char buffer. This, however, does not allow pointers, and is, in my opinion, quite disgusting. The other solution I have found is to use, if it is possible, a variadic function which constructs a temporary with aggregate initialization using variadic expansion. However this method does not solve the serialization and deserialization, only the assignment.
My question is therefore: Is there a good way of serializing and deserializing polymorphic types, generically, when no type information is known, and if not, is there a better way of doing it?