Okay so this is starting to really bother me. And introduce bugs since my constructor is never called and my subclass members have wild initialized values after "promotion. Is there a better alternative to doing the below? This seems like a giant hack to me.
If I must stick with this, then please explain how I can init my data members post promotion, since the cost reference to my new object doesn’t allow me to modify anything… (reinit() call doesn’t compile)
void CSuperDEventWatcher::promoteRhinoMeshObjectToSuperDObject(const CRhinoMeshObject *meshObj)
{
static void *CSuperDMeshObject__vtable_ptr = 0;
static void *CRhinoMeshObject__vtable_ptr = 0;
if(!meshObj)
return;
if(!CSuperDMeshObject__vtable_ptr)
{
// The first time we call this function, the statics are initialized
CRhinoSuperDMeshObject a;
CSuperDMeshObject__vtable_ptr = ON_ClassVtable((void *)(&a));
CRhinoMeshObject b;
CRhinoMeshObject__vtable_ptr = ON_ClassVtable((void *)(&b));
}
if(CRhinoMeshObject__vtable_ptr == *((void **)meshObj))
{
// Change from CRhinoMeshObject vtable to CSuperDMeshObject__vtable_ptr vtable
*((void**)meshObj) = CSuperDMeshObject__vtable_ptr;
if(auto *superDmesh = CRhinoSuperDMeshObject::Cast(meshObj))
{
superDmesh->reinit();
}
}
}
Edit
I’ve modified my code to make a copy instead below, is there anything that will break rhino object consistency if I do this?
if(CRhinoMeshObject__vtable_ptr == *((void **)meshObj))
{
//Change from CRhinoMeshObject vtable to CSuperDMeshObject__vtable_ptr vtable
//*((void**)meshObj) = CSuperDMeshObject__vtable_ptr;
/*if(auto *superDmesh = CRhinoSuperDMeshObject::Cast(meshObj))
{
superDmesh->reinit();
}*/
auto userDataCopy = new CSuperDUserData(*CSuperDUserData::Cast(meshObj->GetAttributeUserData(CSuperDUserData::Id())));
auto superDMesh = new CRhinoSuperDMeshObject();
superDMesh->DeleteAttributeUserData(CSuperDUserData::Id());
superDMesh->AttachAttributeUserData(userDataCopy);
superDMesh->resurface();
doc.ReplaceObject(CRhinoObjRef(meshObj), superDMesh);
}