Click to open or copy the ATLCollections project files.
This sample demonstrates the use of ICollectionOnSTLImpl and CComEnumOnSTL, and the implementation of custom copy policy classes.
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.
The collections can be tested using the supplied C++ client. This client outputs the content of the collections 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.
This sample uses the following keywords:
_Copy, CComEnumOnSTL, ICollectionOnSTLImpl, CAdapt, IEnumVARIANT, std::map, std::string, std::vector