[gme-users] Removing/deleting model elements in BON1

Zoltan Molnar zolmol at isis.vanderbilt.edu
Thu Feb 2 16:28:17 CST 2006


Hi,

If case of BON1 interpreters: let me make clear what I meant by ''used
in destructors'': BON1 uses that method internally in its destructors.

> The protected RemoveAtom & co. methods release objects only from the 
> Builder structure, [they are used mostly in destructors], and they do 
> not provide the functionality you would like to have: to remove an 
> atom from the gme model.
> 


As Akos suggested if you subclass the CBuilderObject then you may access
some of its protected members.
Here you can find a COM based Delete implemented, using subclassing,
thus acquiring the ciObject [a COM pointer] member of CBuilderObject and
invoking the DestroyObject COM method on it. This DestroyObject deletes
the MGA object (the object sitting in GME's window). 
The C++ builder objects need to be deleted also, that is why
RemoveReference( obj) is called in CMySmartModel::DeleteSelected.

CMySmartObj is an object which I could delete, CMySmartModel is its
parent model.

Br, Zoli

-----------------put these into Component.h---------------------
class CMySmartObj;
typedef CTypedPtrList<CPtrList, CMySmartObj*> CMySmartObjList;

class CMySmartModel : public CBuilderModel
{
	DECLARE_CUSTOMMODEL(CMySmartModel, CBuilderModel)
public:
	virtual void Traverse(CMySmartObjList &list);
	void DeleteSelected( CMySmartObjList &list);
	virtual ~CMySmartModel() { }
};

class CMySmartObj : public CBuilderReference
{
	DECLARE_CUSTOMREF(CMySmartObj, CBuilderReference)
public:
	virtual ~CMySmartObj() { }
	void deleteMySelf();
};

----------------------put these into component.cpp---------------


IMPLEMENT_CUSTOMMODEL(CMySmartModel, CBuilderModel, "configuration")
IMPLEMENT_CUSTOMREF( CMySmartObj, CBuilderReference, "interface_ref")

void CMySmartModel::Traverse(CMySmartObjList &lst)
{
	const CBuilderReferenceList *ref_list =
GetReferences("interface_ref");
	POSITION pos = ref_list->GetHeadPosition();
	while(pos)
	{
		CMySmartObj *obj = (CMySmartObj
*)ref_list->GetNext(pos);
		ASSERT( obj);
		lst.AddTail( obj);
	}
}


void CMySmartModel::DeleteSelected(CMySmartObjList &smart_lst)
{
	POSITION pos = smart_lst.GetHeadPosition();
	while(pos)
	{
		CMySmartObj *obj = (CMySmartObj
*)smart_lst.GetNext(pos);
		ASSERT( obj);
		this->RemoveReference( obj);
		obj->deleteMySelf();
	}

	// test:
	// trying to acquire one more time 
	const CBuilderReferenceList *ref_list =
GetReferences("interface_ref");
	pos = ref_list->GetHeadPosition();
	while(pos)
	{
		// it should not enter this loop
		CMySmartObj *obj = (CMySmartObj
*)ref_list->GetNext(pos);
		ASSERT( obj);
		obj->deleteMySelf();
	}
	// end test
}

void CMySmartObj::deleteMySelf()
{
	CComPtr<IMgaFCO> my_mga_ptr = this->ciObject;
	my_mga_ptr->DestroyObject();
}


// traverses models inside topfolders and deletes all occurences of a
certain reference kind
void test2(CBuilder &builder,CBuilderObject *focus, CBuilderObjectList
&selected, long param) 
{
	CBuilderFolder *rf = builder.GetRootFolder();

	CBuilderFolder *lastFolder = 0;
	const CBuilderFolderList * flds = rf->GetSubFolders();
	POSITION pos = flds->GetHeadPosition();
	while( pos)
	{
		CBuilderFolder *fld = flds->GetNext( pos);
		lastFolder = fld;
		
		const CBuilderObjectList *lst =
lastFolder->GetRootObjects();
		POSITION pos = lst->GetHeadPosition();
		while( pos)
		{
			CBuilderObject *obj = lst->GetNext( pos);
			if(obj->IsKindOf(RUNTIME_CLASS(CMySmartModel)))
			{
				CMySmartObjList list;
				// fill the list with MySmartObjs
				CMySmartModel *smodel = (CMySmartModel
*) obj;
				smodel->Traverse(list);

				// now delete them
				smodel->DeleteSelected( list);
			}
		}
	}
}

void CComponent::InvokeEx(CBuilder &builder,CBuilderObject *focus,
CBuilderObjectList &selected, long param) 
{
	test2( builder, focus, selected, param);
}


More information about the gme-users mailing list