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-28 23:21:26


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_at_[hidden]
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_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users


-------------------------------------------------------------------
ÐÂÀ˿ռ䡪¡ªÓëÅóÓÑ¿ªÐÄ·ÖÏíÍøÂçÐÂÉú»î£¡(http://space.sina.com.cn/ )

-------------------------------------------------------------------
ÐÂÀ˿ռ䡪¡ªÓëÅóÓÑ¿ªÐÄ·ÖÏíÍøÂçÐÂÉú»î£¡(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