Mini-proposal, simple boost/handle.hpp lib

Does anyone besides me find this code useful: http://codepad.org/zK0iOLp2 It could go in boost/handle.hpp and could be used to manage any type of handle in a shared_ptr, for example one could do: shared_ptr<int> s; { int sockfd = socket(PF_INET,SOCK_STREAM,0); if( sockfd!=-1 ) make_handle(sockfd,close).swap(s); } It would also work with HWND, so you get a shared_ptr<HWND> (even without make_handle you could still use shared_ptr<HWND__> but strictly speaking you'd be exploiting implementation details with that.) Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Wednesday 06 August 2008 18:25 pm, Emil Dotchevski wrote:
Does anyone besides me find this code useful:
It could go in boost/handle.hpp and could be used to manage any type of handle in a shared_ptr, for example one could do:
shared_ptr<int> s; { int sockfd = socket(PF_INET,SOCK_STREAM,0); if( sockfd!=-1 ) make_handle(sockfd,close).swap(s); }
Couldn't you use lambda for this? boost::shared_ptr<int> s; { int sockfd = socket(PF_INET,SOCK_STREAM,0); if( sockfd!=-1 ) s.reset(&sockfd, boost::lambda::bind(close, * boost::lambda::_1)); } I've never really used lambda, but it compiles at least. By the way, is there any documentation on the lambda headers (which ones to include to use what)? I didn't see anything in the lambda docs. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFImvp55vihyNWuA4URAjJrAJ9pWAw6YESdzuAcbZSbJF672tQqsQCgwlBc SMEGxtEXucTsieb0g0uDK/M= =ijrr -----END PGP SIGNATURE-----

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thursday 07 August 2008 09:36 am, Frank Mori Hess wrote:
Couldn't you use lambda for this?
boost::shared_ptr<int> s; { int sockfd = socket(PF_INET,SOCK_STREAM,0); if( sockfd!=-1 ) s.reset(&sockfd, boost::lambda::bind(close, * boost::lambda::_1)); }
I've never really used lambda, but it compiles at least. By the way, is there any documentation on the lambda headers (which ones to include to use what)? I didn't see anything in the lambda docs.
Err, nevermind. This will fail when sockfd goes out of scope. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFImvwp5vihyNWuA4URAh67AJ9WC+46DseGcxkQrGHZ1due9qRwngCeLE9e MzyY8PLrawS+fsHXDidka7o= =QFKh -----END PGP SIGNATURE-----

Frank Mori Hess wrote:
boost::shared_ptr<int> s; { int sockfd = socket(PF_INET,SOCK_STREAM,0); if( sockfd!=-1 ) s.reset(&sockfd, boost::lambda::bind(close, * boost::lambda::_1)); }
I've never really used lambda, but it compiles at least. By the way, is there any documentation on the lambda headers (which ones to include to use what)? I didn't see anything in the lambda docs.
Err, nevermind. This will fail when sockfd goes out of scope.
boost::shared_ptr<void> s; { int sockfd = socket(PF_INET, SOCK_STREAM, 0); if( sockfd != -1) s.reset(static_cast<void*>(sockfd), bind(close, static_cast_<int>(_1))); }

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Friday 08 August 2008 09:02 am, Mathias Gaunard wrote:
Frank Mori Hess wrote:
boost::shared_ptr<int> s; { int sockfd = socket(PF_INET,SOCK_STREAM,0); if( sockfd!=-1 ) s.reset(&sockfd, boost::lambda::bind(close, * boost::lambda::_1)); }
I've never really used lambda, but it compiles at least. By the way, is there any documentation on the lambda headers (which ones to include to use what)? I didn't see anything in the lambda docs.
Err, nevermind. This will fail when sockfd goes out of scope.
boost::shared_ptr<void> s; { int sockfd = socket(PF_INET, SOCK_STREAM, 0); if( sockfd != -1) s.reset(static_cast<void*>(sockfd), bind(close, static_cast_<int>(_1))); }
make_handle() Emil's handle.hpp creates a shared_ptr which points at a copy of the handle, whose lifetime is also controlled by the shared_ptr. So, you can access the handle through the shared_ptr as well as having the shared_ptr call the handle's cleanup function in its deleter. But my attempt at using lambda stores a pointer to the original handle in the shared_ptr, which becomes invalid as soon as the original handle goes out of scope. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFInElT5vihyNWuA4URAkSFAJ9QwJZhyJ9dtP6yYWPJIsa0bgSKggCfahQG NflCK2b+CjM+x07AqlaifL0= =lvD6 -----END PGP SIGNATURE-----

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Friday 08 August 2008 09:25 am, Frank Mori Hess wrote:
boost::shared_ptr<void> s; { int sockfd = socket(PF_INET, SOCK_STREAM, 0); if( sockfd != -1) s.reset(static_cast<void*>(sockfd), bind(close, static_cast_<int>(_1))); }
make_handle() Emil's handle.hpp creates a shared_ptr which points at a copy of the handle, whose lifetime is also controlled by the shared_ptr. So, you can access the handle through the shared_ptr as well as having the shared_ptr call the handle's cleanup function in its deleter. But my attempt at using lambda stores a pointer to the original handle in the shared_ptr, which becomes invalid as soon as the original handle goes out of scope.
Oh, and now I realize you are solving the problem by copying the handle into a void*. handle.hpp is still more useable though, since it could be used with any handle-like class, not just ints or similar that can copied into a void*. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFInEqJ5vihyNWuA4URAn+2AJ950uziyl/L8oWXKsj7pw4YzkN9VgCgxlSV 5ew6UUNQREACoYhF0kHLvRI= =L0TC -----END PGP SIGNATURE-----

Frank Mori Hess wrote:
Oh, and now I realize you are solving the problem by copying the handle into a void*. handle.hpp is still more useable though, since it could be used with any handle-like class, not just ints or similar that can copied into a void*.
I do not know of handles that are not pointers or ints smaller than pointers.
participants (4)
-
Emil Dotchevski
-
Frank Mori Hess
-
Mathias Gaunard
-
Steven Watanabe