Collection and enumeration in ATL
Cool - Reserved (2004-11-22 11:55:00) Category: The relationship between the collection and enumerator in VC / MFC COM is very similar to the relationship between the container and iterator in STL. Enumerator If a COM object can be referred to as a "collection", it is clear that inside the object is definitely a collection of some types of data. When we want to expose internal data to the client, if Direct STL mode, provide an Iterator type, then cause the server-side data package to be too weak, and the data is easily destroyed by the client. In view of this, COM provides an enumerator for customers to use - the client adopts the interface of the set, acquires the interface of the enumerator, using the enumerator to access the data; except for the envelope interface, the general set object also provides GET__ITEM method Exposure of your own data. Suppose the ICollection interface supports the enumerator IEnumsth, then get an enumerator interface by the following code: HRESULT HRES; IEENUMSTH PSTH; hRES = ICOLLECTION-> GET__NEWENUM ((iunknown **) & psth); our program No declarations for IEnumsth, COM specified, all of the Ienumxxx types need to have the following four methods: Next, Skip, RESET, Clone. Obviously, the role of the GET__NEWENUM () method is to construct an object (hereinafter referred to as an enumerator object), which implements the four methods listed above, and then returns the interface pointer of the object to the client. . ATL provides a general implementation template for an enumerator object, which greatly facilitates our work. The design idea of the envelope object template is "Based on strategy", let's take a look at what strategy to implement, what strategies need: 1, the name of the enumerator interface - iEnumsth2 in the above example, The IID of the enumerator interface is also __UUIDOF (IEnumsth) 3, the type of data enumerated - is also STH in the above example (can be Variant, BSTR, etc.) 4, replication strategy - the most Complex, its completion is the function 5, data storage form-"Collection" (array, STL container, etc.) stored in the data store in the "Collection" object Copy to the customer. ATL threading model template class which provides the following statement: template
However, in many other places in ATL, there are uses for the __UUIDOF operator - in the world of COM, the thrush of MS is impossible ............ The steps to use CCOMENUMONSTL are as follows: 1. Determine which STL container is required for enumeration 2. When the client needs an enumerator interface, select the appropriate template parameters, TypeDef out a specific enumerator object 3, CComObject <>: : CREATEINSTANCE () Generate Enumerator Instance 4 ATL Development Team provides a project called: ATLCOLLECTIONS with a folder called Reuse, which has a folder called Reuse, which has strong reuse. In the vcue_collection.h file, the ATL development team provides us with the createstLenumerator () function, encapsulating the creation process of the entire enumerator object, which is like this: Template
The actual steps of this example are as follows (the programming environment is VS2002, that is, VC7 ATL7): 1. Create a new type of ATLENUM1, add a new type of project called AtleNum1 under it, In the project properties setting, the "property" selection is canceled "Allows the check box to merge the Proxy / Stub code, click the establishment item. 2, switch to" Class View ", follow the following to add the project Add A template for the ATL simple object name atlenum: Figure Atlenum-01 Figure Atlenum-02vc The default property setting for the added class is supported by the two interface, we will change it to a custom interface, exempt from the iDispatch interface. Trouble .3, we assume that the vector is global and is initialized in the constructor of the AtleNum class. Open the atlenum.h file, add the header file containing: #include