Advantages and Uses
There are several advantages of using Active Template Library.
We can create small, fast Component Object Model objects. It supports stock implementations, dual interfaces, connection points, and tear-off interfaces.
There are performance and program architecture reasons to prefer tear-off interfaces. The critical thing is that when asked for a service, the object hands you back a different object that implements that service rather than implementing the service itself. This makes the implementation smoother and quicker.
CComTearOffObject
This class implements the Tear-Off Classes Interfaces. The syntax of this class is as follows-
template<class Base>
class CComTearOffObject: public Base
CComTearOffObject implements a tear-off interface as a separate object that is instantiated only when that interface is queried for. When the reference count becomes zero, it gets deleted.
CComTearOffObject::AddRef
It increments the reference count of the CComTearOffObject object by one.
The syntax is as follows-
STDMETHOD_(ULONG, AddRef)();
CComTearOffObject::CComTearOffObject
It is the Constructor for this class. We use Constructor to create an instance of the class. The syntax is as follows-
CComTearOffObject(void* pv);
CComTearOffObject::~CComTearOffObject
It is the destructor and destroys the object. The syntax is as follows-
~CComTearOffObject();
CComTearOffObject::QueryInterface
It queries the pointer when there is a request. The syntax for retrieving the query to a pointer is-
STDMETHOD(QueryInterface)(REFIID iid, void** ppvObject);
CComTearOffObject::Release
The job of this method is to decrement the count by one. If the reference count becomes zero, it deletes the object. The syntax is as follows-
STDMETHOD_ULONG Release();
CComCachedTearOffObject Class
This class has to implement the IUnknown for a tear-off interface. IUnknown interface allows us to get pointers to other interfaces on a given object.
template
<class contained>
class CComCachedTearOffObject: public
IUnknown,
public CComObjectRootEx<contained
::_ThreadModel::ThreadModelNoCS>
Let us study the methods-
CComCachedTearOffObject::AddRef
This method increments the count of the object by 1. The syntax of the same is -
STDMETHOD_(ULONG, AddRef)();
CComCachedTearOffObject::CComCachedTearOffObject
It is the Constructor for this class. Constructor is used to creating an instance of the class. The syntax is as follows-
CComCachedTearOffObject(void* pv);
CComCachedTearOffObject::~CComCachedTearOffObject
It is the destructor and destroys the object. The syntax is as follows-
~CComCachedTearOffObject();
CComCachedTearOffObject::FinalConstruct
This method calls m_contained::FinalConstruct to create m_contained. The CComContainedObject< contained> is used to access the interface implemented by the tear-off class. The syntax is as follows-
HRESULT FinalConstruct();
CComCachedTearOffObject::FinalRelease
This method calls m_contained::FinalRelease and frees m_contained. m_contained is the CComContainedObject< contained> object. The syntax is written below-
void FinalRelease();
CComCachedTearOffObject::m_contained
This is a CComContainedObject object derived from your tear-off class. The syntax is as follows-
CcomContainedObject <contained> m_contained;
CComCachedTearOffObject::QueryInterface
It retrieves a pointer to the requested interface. The syntax is-
STDMETHOD(QueryInterface)(REFIID iid, void** ppvObject);
iid
The GUID of the interface is requested.
ppvObject
It is a pointer to the interface. It is identified by iid or NULL if the interface is not found.
CComCachedTearOffObject::Release
It decrements the reference count by one and, if the reference count is 0, deletes the CComCachedTearOffObject object. The syntax is-
STDMETHOD_(ULONG, Release)();
Frequently Asked Questions
What are Tear Off Interfaces Classes?
A tear-off interface is an interface we want to expose on demand but not inherit from the main class.
Name two Tear Off Interfaces Classes.
Two Tear-Off Interfaces Classes are CComTearOffObject and CComCachedTearOffObject.
Differentiate between a Constructor and a Destructor.
We use a Constructor to create an instance of the class. The destructor destroys the object.
What is an IUnknown Interface?
IUnknown interface allows us to get pointers to other interfaces on a given object.
What is the need for Active Template Library?
The Active Template Library (ATL) is a set of template-based C++ classes developed by Microsoft, intended to simplify the programming of Component Object Model (COM) objects.
Conclusion
This blog has provided you with the concept of Tear Off Interfaces Classes. We hope that you understand this concept clearly. For more about Active Template Library, Refer to our other blogs-
- Object Safety Classes
- Site Information Classes
-
Service Provider Support Classes
Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.
Happy Coding!