Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hey Ninjas!! Welcome to another article on Advanced Template Library. Today we will learn about Implementing a C++ Standard Library Based Collection. The Advanced Template Library comprises all template-based C++ classes. The Advanced Template library had a predefined ICollectionOnSTLImpl interface. The ICollectionOnSTLImp allows to implement C++ Standard Library based collection interface efficiently.
Without further ado, let's look into the details of Implementing a C++ Standard Library Based Collection.
Generating a New Simple Object
The first step is to generate a new simple object. Follow the following steps to generate a new object:
Uncheck the Attributes box under the Application Settings.
Create a new Project.
Create a simple object named Words using ATL Add Simple Object Wizard and ATL Add Class dialog box.
The dual interface named IWords will be automatically generated.
Create the objects of the class as required. They are used to represent a collection of words.
Editing the IDL File
The next step is to edit the IDL file. The IDL file is used to create header and proxy files.
We have to add three properties to the IDL file. These properties will convert the interface into a read-only collection interface. You can have a look over the IDL file below:
Here, we have used the DISPID_VALUE for representing the id. You can give any value to the DISPID_VALUE. This will be used as the default property for the collections. This value is used to access the different methods of the class.
Creating Typedefs for Storage and Exposure
The next step is to decide the storage structure of the data. This is totally based on the user's requirement. You can store the data in integer, string, arrays, vectors, etc. This data will be exposed to the enumerator.
Here, we have created typedef for the storage of data. You can use other data types for naming the collections. The collection interface uses BSTR for exposing data. We have used IEnumVARIANT class as the enumerator. This class comprises all required methods for enumerating objects. It is a a user-defined data type containing integral constants.
Creating Typedefs for Copy Policy Classes
The typedef will provide all the required information in other classes also. These typedefs will be used by the enumerator and collection.
We had created a Generic copy of Enumerator and Collection. These are used to copy objects without duplicates from one class to another.
Creating Typedefs for Enumeration and Collection
The code template requires CComEnumOnSTL and ICollectionOnSTLImpl classes. These classes provide methods for collection class. These classes can be created using as shown:
In the above code snippet, we had created Typedefs for Enumeration and Collection class. Now we simply need to use Collection_Type for implementing ICollectionOnSTLImpl class and Enumerator_Type for implementing CComEnumOnSTL class.
Editing the Wizard-Generated Code
The Wizard generated code contains the IWords interface. Implementing a Standard Library Based Collection requires CWords Interface. The ICollectionOnSTLImpl class contains the CWords interface. We had earlier created the alias of ICollectionOnSTLImpl class as Collection_Type. Edit the Wizard-Generated Code as:
Code:
class ATL_NO_VTABLE CWords :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CWords, &CLSID_Words>,
public IDispatchImpl<Collection_Type, &IID_IWords, &LIBID_NVC_ATL_COMLib, 1, 0>
{
public:
DECLARE_REGISTRY_RESOURCEID(IDR_WORDS)
BEGIN_COM_MAP(CWords)
COM_INTERFACE_ENTRY(IWords)
COM_INTERFACE_ENTRY(IDispatch)
END_COM_MAP()
//Your code
}
You can also try this code with Online C++ Compiler
The Wizard Generated code comes with the predefined IWords Interface. For implementing a Standard Library Based Collection requires CWords Interface we require the CWords interface. We have used the Collection_Type typedef for using the ICollectionOnSTLImpl class.
Adding Code to Populate the Collection
The final step is to populate the collection. You can add a few words to the constructor of the class. These words can be any word of your choice. The collection is populated using the m_coll member of the class. You can have a look at the following code snippet: