Re: smart_ptr and handles

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 ) ); } 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? 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? Regards, -Dirk

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 binder: 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) jon -- "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

Jonathan Wakely wrote:
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) ); }
Yep. Note also that shared_ptr will call the deleter with an HDC, not a void*, so the cast in operator() is not necessary: void operator()(HDC p) const { ReleaseDC( hwnd, p ); }
I think bind(ReleaseDC, hWnd, _1) should work.
That's what I'd use, too. :-)

On Wed, Dec 29, 2004 at 04:44:22PM +0200, Peter Dimov wrote:
Jonathan Wakely wrote:
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) ); }
Yep. Note also that shared_ptr will call the deleter with an HDC, not a void*, so the cast in operator() is not necessary:
void operator()(HDC p) const { ReleaseDC( hwnd, p ); }
Yes, of course it will. Silly me. Thanks, Peter.
I think bind(ReleaseDC, hWnd, _1) should work.
That's what I'd use, too. :-)
Finally, Dirk, if you don't want to use shared_ptr<void> (and casts) everywhere you can include boost/type_traits.hpp and use remove_pointer: typedef shared_ptr<remove_pointer<HDC>::type> dc_handle; typedef shared_ptr<remove_pointer<HWND>::type> wnd_handle; jon -- "It is unbecoming for young men to utter maxims." - Aristotle
participants (3)
-
Dirk Gregorius
-
Jonathan Wakely
-
Peter Dimov