CComObjectRoot Classes

[Previous] [Next]

ATL objects inherit from CComObjectRootEx or CComObjectRoot. CComObjectRoot is just a typedef for CComObjectRootEx<CComObjectThreadModel >. CComObjectRootEx has the advantage of allowing each object to declare its own threading model. Using CComObjectRoot applies the global CComObjectThreadModel to your object. When we examine the following class definition, we can see how the threading model ties in to the reference-counting scheme:

 template <class ThreadModel> class CComObjectRootEx : public CComObjectRootBase { public:     typedef ThreadModel _ThreadModel;     typedef _ThreadModel::AutoCriticalSection _CritSec;     typedef CComObjectLockT<_ThreadModel> ObjectLock;     ULONG InternalAddRef()     {         ATLASSERT(m_dwRef != -1L);         return _ThreadModel::Increment(&m_dwRef);     }     ULONG InternalRelease()     {         ATLASSERT(m_dwRef > 0);         return _ThreadModel::Decrement(&m_dwRef);     }     void Lock() {m_critsec.Lock();}     void Unlock() {m_critsec.Unlock();} private:     _CritSec m_critsec; }; 

The threading model is maintained as a typedef ThreadModel whose static Increment and Decrement members are called on to handle safe reference counting. An instance of the critical section determined by the threading-model class is instantiated as the m_critsec data member, which is delegated in the Lock and Unlock implementations. Other ATL base classes can use the ObjectLock typedef as a mechanism for calling Lock/Unlock by creating an instance of the ObjectLock type. CComObjectLockT simply calls Lock on the object in the constructor and Unlock in the destructor.

The base class CComObjectRootBase keeps the actual reference count for the object as a data member. It also handles QueryInterface method calls, which are forwarded from the CComObject classes through the COM map and eventually to InternalQueryInterface in CComObjectRootBase. We'll look at the routing shortly, when we discuss CComObject. InternalQueryInterface delegates to AtlInternalQueryInterface, which loops through the COM map to find the requested interface. CComObjectRootBase also handles reference counting and QueryInterface for aggregated and tear-off objects by maintaining a pointer to the outer IUnknown and implementing OuterAddRef, OuterRelease, and OuterQueryInterface.



Inside Atl
Inside ATL (Programming Languages/C)
ISBN: 1572318589
EAN: 2147483647
Year: 1998
Pages: 127

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net