Ok so i've set up boost::serialization to work with objects whose constructor and destructors are private (forcing you to make them through "factories"), but after doing the coming steps, the << operator of a boost::oarchive never returns.
I've followed what was happening exactly in a debugger, but don't know what's happening after the ending brackets of the object's serialization function. It's not even reaching the overloaded destroy function. I think it's some internal business.
- To do this i've overided the `load/save_construct_data function` and
the `access::destroy template function` with the appropriate
parameters.
- I also defined a serialization function for each factory-built
object, but it doesn't do anything as to force you to pass a pointer
instead.
- You'll also notice i've added my own Save() function as so the user
can specify a different factory to use for recreating (loading) the
object back up if needed.
**Why would this be happening and how do i fix it?**
here's the source:
oa << WallBody2; //this is the call in main, wallBody2 is a b2Body*
template<>
static void access::destroy( const b2Body * t) // const appropriate here?
{
// the const business is an MSVC 6.0 hack that should be
// benign on everything else
const_cast<b2Body*>(t)->GetWorld()->DestroyBody(const_cast<b2Body*>(t));
}
template<>
static void access::destroy( const b2Fixture * t) // const appropriate here?
{
// the const business is an MSVC 6.0 hack that should be
// benign on everything else
const_cast<b2Fixture*>(t)->GetBody()->DestroyFixture(const_cast<b2Fixture*>(t));
}
template<class Archive>
inline void save_construct_data( Archive & ar, const b2Body * t, const unsigned int file_version)
{
// save data required to construct instance
b2World* WorldPtr= const_cast<b2Body*>(t)->GetWorld();
ar & WorldPtr;
Save(ar, const_cast<b2Body*>(t), file_version);
}
template<class Archive>
inline void save_construct_data( Archive & ar, const b2Fixture * t, const unsigned int file_version)
{
// save data required to construct instance
b2Body* BodyPtr= const_cast<b2Fixture*>(t)->GetBody();
ar & BodyPtr;
Save(ar, const_cast<b2Fixture*>(t), file_version);
}
template<class Archive>
void serialize(Archive & ar, b2Fixture& b2, const unsigned int version)
{ std::cout << "b2Fixture is not serializable, only b2Fixture pointers are" << std::endl;};
template<class Archive>
void serialize(Archive & ar, b2Body& b2, const unsigned int version)
{ std::cout << "b2Fixture is not serializable, only b2Fixture pointers are" << std::endl;};
template<class Archive>
void Save(Archive & ar, b2Body* b2, const unsigned int version)
{
b2World* World = b2->GetWorld();
ar & World;
b2BodyDef InitialBodyDef;
InitialBodyDef.inertiaScale= b2->GetInertia(); // QUESTION: Is there any way to get/set this from a body (not def)
ar & InitialBodyDef; // QUESTION: wtf is inertiaScale? any relation to MassData?
b2BodyDef BodyDef;
BodyDef.angle= b2->GetAngle();
BodyDef.position= b2->GetPosition();
BodyDef.active= b2->IsActive();
BodyDef.angularDamping= b2->GetAngularDamping();
BodyDef.angularVelocity= b2->GetAngularVelocity();
BodyDef.awake= b2->IsAwake();
BodyDef.bullet= b2->IsBullet();
BodyDef.fixedRotation= b2->IsFixedRotation();
BodyDef.linearDamping= b2->GetLinearDamping();
BodyDef.linearVelocity= b2->GetLinearVelocity();
BodyDef.type= b2->GetType();
BodyDef.userData= b2->GetUserData();
ar & BodyDef;
// number of fixtures saved first so when loaded we know
// how many fixturedefs to extract
unsigned int numbFixtures=0;
for (b2Fixture* fixture = b2->GetFixtureList(); fixture; fixture = fixture->GetNext())
numbFixtures++;
ar & numbFixtures; //TODO: find out if boost will detect this as a list
for (b2Fixture* fixture = b2->GetFixtureList(); fixture; fixture = fixture->GetNext())
ar & fixture;
}
template<class Archive>
void Save(Archive & ar, b2Fixture* b2, const unsigned int version)
{
b2Body* Body = b2->GetBody();
ar & Body;
//Registered so boost can differentiate types of "shapes"
ar.register_type(static_cast<b2CircleShape *>(NULL));
ar.register_type(static_cast<b2PolygonShape *>(NULL));
b2Shape* Shape= b2->GetShape();
ar & Shape;
b2FixtureDef FixtureDef;
FixtureDef.density= b2->GetDensity();
FixtureDef.filter= b2->GetFilterData();
FixtureDef.friction= b2->GetFriction();
FixtureDef.isSensor= b2->IsSensor();
FixtureDef.restitution= b2->GetRestitution();
FixtureDef.userData= b2->GetUserData();
ar & FixtureDef;
}