Hi all,
I would like to discuss some code I'm using to manage COM objects with
intrusive_ptr.
1. I need to access the address of the raw pointer (to use QueryInterface).
Therefore I
implemented operator& for intrusive_ptr. Like in COM this pointer-pointer
will only
be used for intialization purposes. My implementation relies on the
intrusive_ptr's
memory layout and returns reinterpret_cast(addressof(ptr)). I know,
this is evil...
CComPtr-like usage:
boost::intrusive_ptr<IVideo> video;
someObject->QueryInterface(IID_IVideo,(void**)&video);
Is there a better way? How do you bring intrusive_ptr and QueryInterface
together?
2. My functions intrusive_ptr_add_ref and intrusive_ptr_release are
specialized to handle
IUnknown instances only. This way I avoid any conflicts with
implementations of other
reference counted objects. Maybe you can make use of them :-)
Here's my code:
template<typename T>
T** operator&(intrusive_ptr<T>& ptr)
{
ASSERT(0 == *reinterpret_cast(addressof(ptr)));
return reinterpret_cast(addressof(ptr));
}
template<typename T>
inline typename enable_if < is_base_of >::type
intrusive_ptr_add_ref(T* pObject)
{
pObject->AddRef();
}
template<typename T>
inline typename enable_if < is_base_of >::type
intrusive_ptr_release(T* pObject)
{
pObject->Release();
}
Best regards,
Philipp