Boost logo

Boost Users :

From: Philipp Henkel (threadpool_at_[hidden])
Date: 2007-01-04 12:33:43


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<T**>(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<T**>(addressof(ptr)));
  return reinterpret_cast<T**>(addressof(ptr));
}

template<typename T>
inline typename enable_if < is_base_of<IUnknown, T> >::type
intrusive_ptr_add_ref(T* pObject)
{
  pObject->AddRef();
}

template<typename T>
inline typename enable_if < is_base_of<IUnknown, T> >::type
intrusive_ptr_release(T* pObject)
{
  pObject->Release();
}

Best regards,
Philipp



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net