For posterity, It's worth noting that I think I figured it out. Boosts documentation isn't really comprehensive on the subject, but supplemented with Stack overflow, I think its working, though I still have some questions on HOW exactly.
It seems as though that boost's archive system serializes the MOST derived object of a base class if it is polymorphic (
https://www.boost.org/doc/libs/1_71_0/libs/serialization/doc/serialization.html#derivedpointers), so I think a lot of this is just handled by the library by default and all you have to do is "register" the derived classes with the archive beforehand.
The one piece I'm confused on is the memory management piece. The first time a player is created, its race will be set with
m_race = new Human();
and thus `m_race` will point to some allocated memory on the heap, but when deserializing an object that contains a pointer to a polymorphic type, it looks like the library uses "placement new" to construct the object in memory that (I ASSUME) the library creates for you?? Because it certainly doesn't appear to be happening explicitly in the serialize function itself. (
https://www.boost.org/doc/libs/1_71_0/libs/serialization/doc/serialization.html#constructors)
1 2 3 4 5 6 7 8 9
|
// load data required for construction and invoke constructor in place
template<class Archive, class T>
inline void load_construct_data(
Archive & ar, T * t, const unsigned int file_version
){
// default just uses the default constructor to initialize
// previously allocated memory.
::new(t)T();
}
| |
Because there is no "placement delete" this creates a disparity with how I manage the memory I think...As if I try to delete a pointer that has been allocated with placement new, I *think* I get a segfault, but it's obviously correct to delete a pointer that was created explicitly with new.
I tried something like
if(m_race) delete m_race;
but that also seemed to toss a segfault. I dunno. I'll do some more work around it and see.
In any case, I'm assuming the
::new(t)T();
portion of the code evaluates to something like
::new(Human)Race();
such that a derived Human() type of base class Race will get constructed.
Sorry for rambling. Just trying to record what I'm in the process of learning so that if someone finds this thread they'll at least have my ramblings.