Using shared_ptr slightly different

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? Thanks! -- Daniel SBG AB

"Daniel Lidström" <daniel.lidstrom@sbg.se> wrote in message news:E1F34343C3A0804BB3E29ABBFCBBAF0C01BCE3@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

Daniel Lidström wrote:
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 assume that FileMan::Close is void FileMan::Close( FileData& data ); If it took a pointer as an argument, things would be simple: shared_ptr<PxyFileData> p( &geo_file, boost::bind( &FileMan::Close, &fileMan, _1 ) ); But since you need a reference, you'd need to use boost::lambda::bind: namespace ll = boost::lambda; shared_ptr<PxyFileData> p( &geo_file, ll::bind( &FileMan::Close, &fileMan, *ll::_1 ) ); or write your own function object: struct fm_close { typedef void result_type; void operator()( FileMan * pfm, FileData * pfd ) const { pfm->Close( *pfd ); } }; shared_ptr<PxyFileData> p( &geo_file, boost::bind( fm_close(), &fileMan, _1 ) ); or maybe struct fm_close { FileMan * pfm_; explicit fm_close( FileMan * pfm ): pfm_( pfm ) {} void operator()( FileData * pfd ) const { pfm_->Close( *pfd ); } }; shared_ptr<PxyFileData> p( &geo_file, fm_close( &fileMan ) );

"Daniel Lidström" <daniel.lidstrom@sbg.se> wrote in message news:E1F34343C3A0804BB3E29ABBFCBBAF0C01BCE3@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?
Thanks!
-- Daniel SBG AB
If fileMan.Close received pointers, you could do the following: shared_ptr<PxyFileData> ptr( &geo_file , boost::bind( boost::mem_fn(&<fileMan's type here>::Close) , &fileMan , _1 ) ); If Close doesn't take pointers, I guess you could use Boost.Lambda's bind instead (to dereference _1, so you'd write *_1), but since I haven't used it, I can't say for sure, the syntax should be about the same I think... HTH Pablo Aguilar
participants (4)
-
Daniel Lidström
-
Jeff Flinn
-
Pablo Aguilar
-
Peter Dimov