Boost logo

Boost Users :

From: Jeff Flinn (TriumphSprint2000_at_[hidden])
Date: 2005-04-11 13:35:19


"Daniel Lidström" <daniel.lidstrom_at_[hidden]> wrote in message
news:E1F34343C3A0804BB3E29ABBFCBBAF0C01BCE3_at_ADA...
> Hello,
>
> I want to use the shared_ptr with objects that are allocated using
> a factory. The problem is the deallocation. It has to be done with
> a ``manager'' class. For example, here is how the allocation and
> deallocation has to be done:
>
> PxyFileData& geo_file = dynamic_cast<PxyFileData&>(fileMan.New("geo"));
> fileMan.Close(geo_file);
>
> I would like to be able to put shared_ptr<PxyFileData>'s into a
> std::vector
> and then forget about the deallocation. I.e. fileMan.Close(...) should be
> done automatically when the objects aren't needed anymore. But since the
> deallocation isn't simply delete, how can I do this?

I believe this should do it:

    typedef shared_ptr<void> tPxyFileDataPtr;
    typedef ??? tFileMan;

    tPxyFileDataPtr PxyFileDataPtr( const char* aName, tFileMan& fileMan )
    {
        return tPxyFileDataPtr(
dynamic_cast<PxyFileData&>(fileMan.New(aName))
                              , boost::bind( &tFileMan::New, fileMan )
                              );
    }

Alternate usages of shared_ptr are described in the docs.

Jeff Flinn


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