The ATLCollections sample demonstrates the use of ICollectionOnSTLImpl and CComEnumOnSTL, and the implementation of custom copy policy classes.
| Security Note |
|---|
This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. |
Building and Running the Sample
To build and run this sample
Open the solution file ATLCollections.sln.
From the Build menu, click Build Solution.
From the Debug menu, click Start Without Debugging. This will test the collections using the supplied C++ client. This client outputs the content of the collections to a command window by looping through using the
CountandItemproperties and by enumerating the items using the_NewEnumproperty. TheAdd,Remove, andClearmethods of theIItemsinterface are also tested.
About the Sample
This project implements two COM collections based on data stored in STL containers:
The simpler of the two classes,
CWords, is a read-only BSTR collection based on data stored as std::strings in a std::vector. This class uses the custom copy policy classVCUE::GenericCopy, defined in VCUE_Copy.h and VCUE_CopyString.h, to convert the data from std::strings to the type appropriate for the collection or enumerator interface.The second class,
CItems, is a read-write VARIANT collection based on data stored in a std::map. The CComVariant data in the map uses CComBSTRs (actually CAdapt<CComBSTR>s) as the keys. This class uses the custom copy policy classVCUE::MapCopy, defined in VCUE_Copy.h, to convert the stored data to VARIANTs that can be passed back to COM clients.CItemsderives from classVCUE::ICollectionOnSTLCopyImpl, defined in VCUE_Collections.h. The latter class derives from ICollectionOnSTLImpl and overrides get__NewEnum so that each enumerator has its own copy of the collection data.CItemsimplements theAdd,Remove, andClearmethods in the collection interface.
Keywords
This sample uses the following keywords:
_Copy, CComEnumOnSTL, ICollectionOnSTLImpl, CAdapt, IEnumVARIANT, std::map, std::string, std::vector
| Note |
|---|
Some of the samples, such as this one, have not been modified to reflect the changes in the Visual C++ wizards, libraries, and compiler, but still demonstrate how to complete your desired task. |