Boost logo

Boost Users :

Subject: Re: [Boost-users] [shared_ptr] Is there an interface to set the ptr a
From: Max (loadcom_at_[hidden])
Date: 2008-12-29 06:17:53


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/ )


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net