Table of contents
1.
Introduction
2.
What is ATL?
3.
What is COM?
4.
Implementing CComObjectRootEx
5.
Implementing CComObject, CComPolyObject, and CComAggObject
6.
Frequently Asked Questions
6.1.
What is object and class in oops?
6.2.
What is ATL in programming?
6.3.
What is a template class in C++?
6.4.
Why are objects used in C++?
7.
Conclusion
Last Updated: Mar 27, 2024

How to implement ATL objects?

Author Aditi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Have you worked with C++ programming language? Do you want to learn something new in C++?

Then you have come to the right place. This article is focused on one of the template libraries of the C++ programming language, i.e., Active Template Library. This library allows an easy way to create small and fast COM objects. It is a template-based set of C++ classes. It has various features and can be used to create different objects. Let's dive into the article to know more.

How to implement ATL objects

What is ATL?

ATL stands for Active Template Library. It is a template-based set of C++ classes. You can easily create small and fast COM objects. Here COM stands for Component Object Model. ATL supports key features of COM. Those features include stock implementations of IUnknown, IDispatch, IClassFactory, and IClassFactory2. The features also include the following:-

  • dual interfaces, 
  • connection points, 
  • tear-off interfaces, 
  • standard COM enumerator interfaces and 
  • ActiveX controls.

ATL code is used to create different objects. They are as follows:-

  • single-threaded objects
  • free-threaded model objects
  • apartment-model objects
  • both apartment-model and free-threaded objects

What is COM?

COM stands for Component Object Model. It is the famous TLA (three-letter acronym) that is present in Windows everywhere now. Various technologies are coming based on COM. COM documentation contains many terms like COM object, server, interface, etc. But what is COM exactly?

COM is a technique for exchanging binary code between programs and languages. It is unlike the C++ method, which encourages source code reuse. ATL is the ideal example of this. Reusing code at the source level is effective, but only in C++. Along with the possibility of name collisions, it increases bloat in your project. This bloat is caused because of having several copies of the same code. This is important to know before you move forward in the article.

Implementing CComObjectRootEx

CComObjectRootEx is very important in ATL. All ATL objects must have at least one instance of CComObjectRoot or CComObjectRootEx in their inheritance. The default QueryInterface is based on COM map entries. It is provided by CComObjectRootEx.

When a client requests an interface, the object's interfaces are made available to the client through its COM map. The query is executed through the CComObjectRootEx::InternalQueryInterface. InternalQueryInterface only handles interfaces in the COM map table.

The COM_INTERFACE_ENTRY macro or one of its variations can be used to add interfaces to the COM map table. For example, the code below adds the COM map table entries for the interfaces ISupportErrorInfo, IDispatch, and IBeeper:

BEGIN_COM_MAP(CBeeper)
   COM_INTERFACE_ENTRY(IBeeper)
   COM_INTERFACE_ENTRY(IDispatch)
   COM_INTERFACE_ENTRY_TEAR_OFF(IID_ISupportErrorInfo, CBeeper2)
END_COM_MAP()

Implementing CComObject, CComPolyObject, and CComAggObject

The most derived classes are the template classes CComObject, CComPolyObject, and CComAggObject in the inheritance chain. They are in charge of managing the QueryInterface, AddRef, and Release methods in IUnknown. In addition, the QueryInterface semantics and reference counting necessary for the inner unknown are provided by CComAggObject and CComPolyObject (when used for aggregated objects).

Whether you define one (or none) of the following macros will determine whether CComObject, CComPolyObject, or CComAggObject is used:

Implementing CComObject, CComPolyObject, and CComAggObject

Using CComAggObject and CComObject have a lot of advantages. One of them is the implementation of optimized IUnknown for the type of object being generated. For instance, an aggregated object requires a pointer to the outer unknown and a reference count for the inner unknown, whereas a nonaggregated object requires a reference count.

By utilizing CComPolyObject, you can handle aggregated and nonaggregated scenarios without including CComAggObject and CComObject in your module. For both situations, a single CComPolyObject object is used. This means that your module only contains one instance of the vtable and one instance of the functions. If your vtable is large, this can significantly reduce the size of your module. CComPolyObject, unlike CComAggObject and CComObject, is not optimized for an aggregated or nonaggregated object. Hence using it with a tiny vtable may result in a slightly larger module size.

Frequently Asked Questions

What is object and class in oops?

Class is a template and is a group of similar objects. Objects are created from the class. In comparison, the object is an instance of a class. It is a real-world entity like a person, car, laptop, mobile, mouse, book, chair, etc. 

What is ATL in programming?

The Active Template Library (ATL) is a set of template-based C++ classes. It allows you to create small and fast Component Object Model (COM) objects.

What is a template class in C++?

Templates in c++ are defined as a blueprint or formula for creating a generic class or a function. Generic Programming is an approach where parameters are generic types in algorithms, and it works for various data types. A template in C++ is a straightforward yet effective tool.

Why are objects used in C++?

When a class is defined, then no memory or storage is allocated. It is because the class only defines the specification for the object. We must create objects to use the data and access member functions defined in the class.

Conclusion

This complete article has helped us to learn about all the concepts of Active Template Library. We have learned about ATL, COM, and the implementation of ATL objects. 

We hope this blog has helped you enhance your  ATL of C++ programming language knowledge. Check out our articles to know more about the classes & objectsintroduction to templates, and STL in C++. Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. Happy Coding!

thank you
Live masterclass