Re: [Boost-users] [shared_ptr] Is there an interface to set the ptr a
Sorry, this version is incorrect: it should be #define DECLARE_VAR_AUTO_CLEANER_BY_MEM_FN(CLASS, VAR, MEM_FN_CLEANER)\ boost::shared_ptr<CLASS> auto__cleaner__for__ ## VAR\ (&VAR, boost::bind(&CLASS::MEM_FN_CLEANER, _1)); #define DECLARE_PTR_AUTO_CLEANER_BY_MEM_FN(CLASS, VAR_PTR, MEM_FN_CLEANER)\ boost::shared_ptr<CLASS> auto__cleaner__for ## VAR_PTR\ (VAR_PTR,\ boost::lambda::if_then(boost::lambda::_1!=static_cast<CLASS*>(0),\ boost::lambda::bind(&CLASS::MEM_FN_CLEANER, boost::lambda::_1))) // use case: struct A { void close() { delete this; } }; { A *pa = new A(); DECLARE_PTR_AUTO_CLEANER_BY_MEM_FN(A, pa, close); } // pa is cleaned by its close() function automatically Max ============================================================= 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 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 auto__cleaner__for__ ## VAR\ (&VAR, boost::bind(&CLASS::MEM_FN_CLEANER, _1)); Usage: DECLARE_VAR_PTR_WITH_AUTO_MEM_CLEANER(AcDbBlockTable, pBlockTable, close); Max ------------------------------------------------------------------- 新浪空间——与朋友开心分享网络新生活!(http://space.sina.com.cn/ )
participants (1)
-
Max