Replacing Objects in Collections
This section shows example code to replace objects in collections. The example code uses:
//////////////////////////////////////////////////////////////////////
// ReplaceCollection function
//
// Example of using the CMetaObject::ReplaceDestCollection and
// ReplaceOrigCollection functions
//////////////////////////////////////////////////////////////////////
HRESULT ReplaceCollection(CMetaRepository &repos)
{
LLCMetaObject objectList;
LLOBJECTID_t inLoids; // list of loids
HRESULT result = S_OK; // return value
CMetaObject obj;
OBJECTID_t
loidEuroDiv = NULLLOID,
loidAmericanAutos = NULLLOID,
loidHurricane = NULLLOID,
loidYearling = NULLLOID,
loidTortoise = NULLLOID,
loidTurtle = NULLLOID;
// Get the ObjectIDs
result = repos.GetObjectID(OBJGUID_Hurricane, &loidHurricane);
if (SUCCEEDED(result))
result = repos.GetObjectID(OBJGUID_Turtle, &loidTurtle);
if (SUCCEEDED(result))
result = repos.GetObjectID(OBJGUID_AmericanAutos, &loidAmericanAutos);
if (SUCCEEDED(result))
result = repos.GetObjectID(OBJGUID_Yearling, &loidYearling);
if (SUCCEEDED(result))
result = repos.GetObjectID(OBJGUID_Tortoise, &loidTortoise);
if (SUCCEEDED(result))
result = repos.GetObjectID(OBJGUID_EuroDivision, &loidEuroDiv);
if (SUCCEEDED(result))
{
//add Hurricane and Turtle ObjectIDs to a list
inLoids.push_back(loidHurricane);
inLoids.push_back(loidTurtle);
// Set local object objectID to that of AmericanAutos
obj.SetObjectID(loidAmericanAutos);
// Call ReplaceDestCollection to replace the DivMakesAutos
// of AmericanAutos with Hurricane, Turtle
result = obj.ReplaceDestCollection(inLoids,
RELGUID_DivMakesAutos);
if (SUCCEEDED(result))
{
// Get and display the Dest Collection to see results
result = obj.GetDestCollection(objectList,
RELGUID_DivMakesAutos);
if(SUCCEEDED(result))
{
cout << endl;
cout << ">>> AmericanAutos Collection after replace" << endl;
PrintObjectList(objectList);
CLEARVECTOR(objectList);
}
}
}
if (SUCCEEDED(result))
{
// Initialize input list
CLEARVECTOR(inLoids);
//add AmericanAutos objectID to list.
inLoids.push_back(loidAmericanAutos);
// Initialize local object
obj.Initialize();
// Set local object objectID to that of Turtle
obj.SetObjectID(loidTurtle);
// Call ReplaceOrigCollection to replace the origin
// DivMakesAutos collection of Turtle from EuroDiv to AmericanAutos
result = obj.ReplaceOrigCollection(inLoids,
RELGUID_DivMakesAutos);
if (SUCCEEDED(result))
{
// Get and display the Orig Collection to see results
result = obj.GetOrigCollection(objectList,
RELGUID_DivMakesAutos);
if(SUCCEEDED(result))
{
cout << endl;
cout << ">>> EuroDivision collection after replace" << endl;
PrintObjectList(objectList);
CLEARVECTOR(objectList);
}
}
}
);
return result;
} // ReplaceCollection