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.
Security Note |
---|
This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties. |
To get samples and instructions for installing them:
To access samples from Visual Studio
On the Help menu, click Samples.
By default, these samples are installed in drive:\Program Files\Microsoft Visual Studio 10.0\Samples\.
- For the most recent version of this sample and a list of other samples, see Visual Studio Samples on the MSDN Web site.
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.
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.