Getting Collections of Objects
This section shows example code to get collections of objects. The example code uses:
The example also shows how to iterate through the returned vector of objects.
//////////////////////////////////////////////////////////////////
// GetDestColl function
//
// Example of using the CMetaObject::GetDestCollection function
//////////////////////////////////////////////////////////////////
HRESULT GetDestColl()
{
LLCMetaObject objectList; // returned set of collection objects
HRESULT result = S_OK;
CMetaObject obj;
obj.SetClassGUID(CLSGUID_AutoDiv);
obj.SetObjectName(_T("AmericanAutos"));
if (SUCCEEDED(result = obj.ReadObject()))
{
// Get collection of all Autos of the AmericanAutos
result = obj.GetDestCollection(objectList, RELGUID_DivMakesAutos);
if (SUCCEEDED(result))
{
cout << endl;
cout << ">>> Collection of all Autos of the AmericanAutos Division" << endl;
PrintObjectList(objectList);
CLEARVECTOR(objectList);
}
// Get collection of all Autos of the AmericanAutos Division named Yearling
result = obj.GetDestCollection(objectList, RELGUID_DivMakesAutos,
0,_T("Yearling"));
if (SUCCEEDED(result))
{
cout << endl;
cout << ">>> Collection of Autos of the AmericanAutos Division named Yearling";
cout << endl;
PrintObjectList(objectList);
CLEARVECTOR(objectList);
}
}
return (result);
}
////////////////////////////////////////////////////////////////////////////////
// GetOrigColl function
//
// Example of using the CMetaObject::GetOrigCollection function
////////////////////////////////////////////////////////////////////////////////
HRESULT GetOrigColl()
{
LLCMetaObject objectList; // returned set of collection objects
HRESULT result = S_OK;
CMetaObject obj;
obj.SetClassGUID(CLSGUID_Auto);
obj.SetObjectName(_T("Yearling"));
if (SUCCEEDED(result = obj.ReadObject()))
{
// Get collection of all Auto Divisions of Yearling
result = obj.GetOrigCollection(objectList, RELGUID_DivMakesAutos);
if (SUCCEEDED(result))
{
cout << endl;
cout << ">>> Collection of all Auto Divisions of Yearling" << endl;
PrintObjectList(objectList);
CLEARVECTOR(objectList);
}
if (SUCCEEDED(result))
{
// Get collection of all Auto Divisions of Yearling
// by the name AmericanAutos
result = obj.GetOrigCollection(objectList, RELGUID_DivMakesAutos,
0,_T("AmericanAutos"));
if (SUCCEEDED(result))
{
cout << endl;
cout << ">>> Collection of all Auto Divisions of Yearling " << endl;
cout << ">>> by the name AmericanAutos" << endl;
PrintObjectList(objectList);
CLEARVECTOR(objectList);
}
}
}
return (result);
}
////////////////////////////////////////////////////////////////////////////
// GetCollectionByProperty function
//
// Read Auto Object and Get origin and destination collections by Property.
//
// Example of using the CMetaObject::GetDestCollectionByProperty function
////////////////////////////////////////////////////////////////////////////
HRESULT GetCollectionByProperty()
{
HRESULT result = S_OK;
CMetaObject obj;
LLCMetaObject objectList;
CMetaFilterInfo filterInfo; // one property filter entry
CMetaObjectKey propID;
MetaFilterInfoVector propFilter; // property filter specification
MetaObjectKeyVector sortList;
// Read by ClassGUID and Name
obj.SetClassGUID(CLSGUID_AutoDiv);
obj.SetObjectName(_T("Euro Division"));
if (SUCCEEDED(result = obj.ReadObject()))
{
// search for objects in collection with Weight=3000
CLEARVECTOR(propFilter); filterInfo.Initialize();
filterInfo.value.SetName(NMWEIGHT);
filterInfo.SetComparisonOperator(EQUAL);
filterInfo.value.SetInt(3000);
propFilter.push_back(filterInfo);
// Sort By Name & Object ID
propID.SetObjectID(PID_CMN_NAME);
sortList.push_back(propID);
propID.SetObjectID(PID_CMN_LOID);
sortList.push_back(propID);
// Call GetDestCollectionByProperty
result = obj.GetDestCollectionByProperty(objectList,
propFilter, sortList, RELGUID_DivMakesAutos);
if (SUCCEEDED(result))
{
cout << endl;
cout << ">>> Objects in collection with Weight=3000" << endl;
PrintObjectList(objectList);
}
DisplayResult(result);
if (SUCCEEDED(result))
{
// return all objects in collection
CLEARVECTOR(propFilter); filterInfo.Initialize();
// Sort By Name
propID.SetObjectID(PID_CMN_NAME);
sortList.push_back(propID);
// Call GetDestCollectionByProperty
result = obj.GetDestCollectionByProperty(objectList,
propFilter, sortList, RELGUID_DivMakesAutos);
if (SUCCEEDED(result))
{
cout << endl;
cout << ">>> All objects in Auto collection" << endl;
PrintObjectList(objectList);
}
DisplayResult(result);
}
if (SUCCEEDED(result))
{
// return objects in collection with a BasePrice < 12000
// or a BasePrice > 20000
CLEARVECTOR(propFilter); filterInfo.Initialize();
filterInfo.value.SetPropertyID(PID_AUTO_BASEPRICE);
filterInfo.SetComparisonOperator(LESS_THAN);
filterInfo.value.SetShort(12000);
filterInfo.SetLogicalOperator(META_OR);
propFilter.push_back(filterInfo);
filterInfo.Initialize();
filterInfo.value.SetPropertyID(PID_AUTO_BASEPRICE);
filterInfo.SetComparisonOperator(GREATER_THAN);
filterInfo.value.SetShort(20000);
propFilter.push_back(filterInfo);
// Sort By Name
propID.SetObjectID(PID_CMN_NAME);
sortList.push_back(propID);
// Call GetDestCollectionByProperty
result = obj.GetDestCollectionByProperty(objectList,
propFilter, sortList, RELGUID_DivMakesAutos);
if (SUCCEEDED(result))
{
cout << endl;
cout << ">>> Objects in collection with a " << endl;
cout << ">>> BasePrice < 12000 or a BasePrice > 20000" << endl;
PrintObjectList(objectList);
}
DisplayResult(result);
}
}
return result;
}
//////////////////////////////////////////////////////////////////
// PrintObjectList function
// Print a vector of CMetaObject objects.
//////////////////////////////////////////////////////////////////
void PrintObjectList(LLCMetaObject &objectList)
{
LICMetaObject pMetaObject;
if (objectList.size() > 0)
{
cout << "\nCOLLECTION BEGIN ";
pMetaObject = objectList.begin();
while(pMetaObject != objectList.end())
{
cout << endl;
PrintMetaObject(*pMetaObject);
pMetaObject++;
}
cout << "COLLECTION END " << endl;
}
else
{
cout << "EMPTY COLLECTION " << endl;
}
}