ATLCollections Sample: Demonstrates ICollectionOnSTLImpl, CComEnumOnSTL, and Custom Copy Policy Classes
The ATLCollections sample demonstrates the use of ICollectionOnSTLImpl and CComEnumOnSTL, and the implementation of custom copy policy classes.
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. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.
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 Count and Item properties and by enumerating the items using the _NewEnum property. The Add, Remove, and Clear methods of the IItems interface 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 class VCUE::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 class VCUE::MapCopy, defined in VCUE_Copy.h, to convert the stored data to VARIANTs that can be passed back to COM clients. CItems derives from class VCUE::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. CItems implements the Add, Remove, and Clear methods in the collection interface.
This sample uses the following keywords:
_Copy, CComEnumOnSTL, ICollectionOnSTLImpl, CAdapt, IEnumVARIANT, std::map, std::string, std::vector
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.