Re: [Boost-users] [shared_ptr] Is there an interface to set the ptr a

Further, I've written the following macros for such use case: #define DECLARE_VAR_PTR_WITH_AUTO_MEM_CLEANER(CLASS, VAR_PTR, MEM_FN_CLEANER)\ CLASS *VAR_PTR;\ boost::shared_ptr<CLASS> auto__cleaner__for__ ## VAR_PTR\ (VAR_PTR, boost::bind(&CLASS::MEM_FN_CLEANER, _1)); #define DECLARE_VAR_WITH_AUTO_MEM_CLEANER(CLASS, VAR, MEM_FN_CLEANER)\ CLASS VAR;\ boost::shared_ptr<CLASS> auto__cleaner__for__ ## VAR\ (&VAR, boost::bind(&CLASS::MEM_FN_CLEANER, _1)); Usage: DECLARE_VAR_PTR_WITH_AUTO_MEM_CLEANER(AcDbBlockTable, pBlockTable, close); Max Hello Scott, Thanks. It seems to be one of the simplest way to get to my needs to declare another companion auto_free_helper. It's better than none, if not the best. I'm using this technique for stream: std::ofstream outstream(strPath); boost::shared_ptr os_auto_closer(&outstream, boost::bind(&std::ofstream::close, _1)); B/Rgds Max ----- Original Message ----- From: Scott McMurray To: boost-users@lists.boost.org Subject: Re: [Boost-users] [shared_ptr] Is there an interface to set the ptr a shared_ptr holds Date: 2008-12-29 11:35:53 On Sun, Dec 28, 2008 at 22:29, Max wrote:
I have the following code scenario:
A *pa = NULL;
// pa will be allocated here (by 3rd-party function) // by setA(), // whose behavior could not be changed setA(pa); // prototype: void setA(A*& pa)
//...
pa->close();
I think the following is fine: A *pa_ = 0; setA(pa_); boost::shared_ptr pa(pa_, boost::bind(&A::close, _1)); Since the shared_ptr constructor will call the deleter if its internal allocation fails. ~ Scott _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users ------------------------------------------------------------------- 新浪空间——与朋友开心分享网络新生活!(http://space.sina.com.cn/ ) ------------------------------------------------------------------- 新浪空间——与朋友开心分享网络新生活!(http://space.sina.com.cn/ )
participants (1)
-
Max