Are there any good COM smart pointer implementations. I am using C++, and VS8 Express.
I am trying to use CComPtr in <Atlbase.h> to no avail. These are the compiler errors.
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlcomcli.h(300) : error C2299: 'ATL::CComPtr<T>::operator =' : behavior change: an explicit specialization cannot be a copy constructor or copy assignment operator
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlcomcli.h(304) : see reference to class template instantiation 'ATL::CComPtr<T>' being compiled
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlcomcli.h(300) : error C2299: 'ATL::CComPtr<T>::operator =' : behavior change: an explicit specialization cannot be a copy constructor or copy assignment operator
with
[
T=IUnknown
]
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlcomcli.h(508) : see reference to class template instantiation 'ATL::CComPtr<T>' being compiled
with
[
T=IUnknown
]
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlcomcli.h(300) : error C2299: 'ATL::CComPtr<T>::operator =' : behavior change: an explicit specialization cannot be a copy constructor or copy assignment operator
with
[
T=IPersistStream
]
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlcomcli.h(2215) : see reference to class template instantiation 'ATL::CComPtr<T>' being compiled
with
[
T=IPersistStream
]
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlbase.h(861) : error C2299: 'ATL::CAutoPtr<T>::CAutoPtr' : behavior change: an explicit specialization cannot be a copy constructor or copy assignment operator
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlbase.h(927) : see reference to class template instantiation 'ATL::CAutoPtr<T>' being compiled
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlbase.h(884) : error C2299: 'ATL::CAutoPtr<T>::operator =' : behavior change: an explicit specialization cannot be a copy constructor or copy assignment operator
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlbase.h(4522) : error C3203: 'CComAutoThreadModule' : unspecialized class template can't be used as a template argument for template parameter 'T', expected a real type
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlbase.h(4534) : see reference to class template instantiation 'ATL::CComAutoThreadModule<ThreadAllocator,dwWait>' being compiled
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlcomcli.h(300) : error C2299: 'ATL::CComPtr<T>::operator =' : behavior change: an explicit specialization cannot be a copy constructor or copy assignment operator
with
[
T=ITypeLib
]
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlbase.h(5982) : see reference to class template instantiation 'ATL::CComPtr<T>' being compiled
with
[
T=ITypeLib
]
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlcomcli.h(300) : error C2299: 'ATL::CComPtr<T>::operator =' : behavior change: an explicit specialization cannot be a copy constructor or copy assignment operator
with
[
T=ICatRegister
]
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlbase.h(6100) : see reference to class template instantiation 'ATL::CComPtr<T>' being compiled
with
[
T=ICatRegister
]
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlcomcli.h(300) : error C2299: 'ATL::CComPtr<T>::operator =' : behavior change: an explicit specialization cannot be a copy constructor or copy assignment operator
with
[
T=LPD3DXMESH
]
c:\documents and settings\running\my documents\visual studio projects\yardville\source\../include/Renderdata.h(17) : see reference to class template instantiation 'ATL::CComPtr<T>' being compiled
with
[
T=LPD3DXMESH
]
any ideas??!??
Not me...
------------------------------------------------------
So I started working on my own, but I would rather work with something more tested. Also, I am running into issues with the implementation; I cannot AddRef () to a COM object that I haven't yet acquired. So do I add an Acquire() method that will set a flag, such that while this flag is set, I need to call AddRef() next time I access the COM object before I reset the flag, however if the Com_ptr is destroyed before the item is released I will not call Release()?
template<class T>
class com_ptr
{
public:
com_ptr(T* raw):ptr(raw),bRefNeeded(false)
{
if (ptr)
ptr->AddRef();
}
com_ptr(const com_ptr<T> &rhs):ptr(rhs.ptr)
{
if (ptr)
ptr->AddRef;
}
com_ptr& operator=(const com_ptr& rhs)
{
if ( this->ptr == rhs.ptr)
return *this;
if (ptr)
ptr->Release();
ptr = rhs.ptr;
if (ptr)
ptr->AddRef();
}
~com_ptr()
{
if (ptr && !bRefNeeded)
ptr->Release();
}
T* Acquire()
{
bRefNeeded = true; // or something of the sort !!!!!
return ptr;
}
T* Get()
{
if (ptr && bRefNeeded)
{
ptr->AddRef();
bRefNeeded = false;
}
return ptr;
}
T& operator*() const
{
if (ptr && bRefNeeded)
{
ptr->AddRef();
bRefNeeded = false;
}
return *ptr;
}
T* operator->() const
{
if (ptr && bRefNeeded)
{
ptr->AddRef();
bRefNeeded = false;
}
return ptr;
}
private:
T* ptr;
bool bRefNeeded;
};
[Edited by - DeafManNoEars on August 13, 2007 9:24:13 PM]