Boost logo

Boost :

From: Jonathan Wakely (cow_at_[hidden])
Date: 2004-12-29 09:20:11

On Wed, Dec 29, 2004 at 02:51:27PM +0100, Dirk Gregorius wrote:

> Thanks for your help and sorry for not looking carefully at the
> documentation:
> I tried the following for a Windows device context:
> The creation function is: HDC GetDC( HWND)
> The deletion function is: int ReleaseDC( HWND, HDC );
> // Code like in the documentation...
> typedef shared_ptr<void> handle;
> handle make_dc( HWND hWnd )
> {
> // The release function takes two arguments, so I tried an adapter
> functor...unfortunately it does not work
> handle( GetDC( hWnd ), bind1( ReleaseDC, hWnd ) );
> }

Should that be bind, not bind1 ?

It's not hard to write your own deleter if you can't do it with a

    struct DCReleaser
        DCReleaser(HWND hwnd) : hwnd(hwnd) {}
        void operator()(void* p) { ReleaseDC( hwnd, (HDC)p ); }
        HWND hwnd;

    handle make_dc( HWND hWnd )
       return handle( GetDC( hWnd ), DCReleaser(hWnd) );

but ...

> My problem is to bind the window handle to the custom delete function.
> My compilter spits aout a lot of error messages here.
> Can I use boost::bind here or boost::function?

I think bind(ReleaseDC, hWnd, _1) should work.

> BTW: How does shared_ptr call the custom delete function? For a COM
> object it should call in the D'tor:
> raw_ptr->Release();
> While in my case it should call ( what is the usual way in the C++
> standard IIRC ):
> Release( raw_ptr );
> Does the shard_ptr interface figures this out in some clever way and
> manage this internally?

The custom deleter's function operator is called with the managed
pointer as its argument, e.g. for a deleter d and pointer p: d(p)


"Ye have locked yerselves up in cages of fear;
 and behold, do ye now complain that ye lack freedom."
	- Lord Omar Khayyam Ravenhurst
	  "Epistle to the Paranoids," HBT

Boost list run by bdawes at, gregod at, cpdaniel at, john at