Useless use of smart pointers
Hi!
Some old code is getting an overhaul at the moment and using smart
pointers is an obvious part of this. Let's look at a simplified
example.
Consider having a type
class Obj;
that is expensively constructed from a file. Once constructed, they
are never modified and used (shared) a zillion times by a few thousand
other objects (think of data or parameter tables). Therefore, these
objects are stored in a std::map by some key (e.g. filename)
encapsulated by some type like this:
class ObjStore1 {
public:
const Obj* get( const std::string& filename );
~ObjStore1();
private:
typedef std::map< std::string, const Obj* > store_type;
store_type store;
};
The get method retrieves some Obj from the map, constructing it on
demand:
const Obj* ObjStore1::get( const std::string& filename ) {
store_type::const_iterator it = store.find( filename );
if ( it != store.end() ) {
return it->second;
} else {
std::auto_ptr< Obj > pObj( new Obj( filename ) );
store[ filename ] = pObj.get();
return pObj.release();
}
}
In this implementation, ~ObjStore1 needs to delete the Objs:
ObjStore1::~ObjStore1() {
store_type::iterator it;
for ( it = store.begin(); it != store.end(); ++it ) {
delete it->second;
}
};
A "simple" modification could be using std::tr1::shared_ptrs in the
map:
class ObjStore2 {
public:
const Obj* get( const std::string& filename );
private:
typedef std::map< std::string,
std::tr1::shared_ptr< const Obj > > store_type;
store_type store;
};
What have we won? There's no destructor needed anymore, and the get
method gets rid of std::auto_ptr:
const Obj* ObjStore2::get( const std::string& filename ) {
store_type::const_iterator it = store.find( filename );
if ( it != store.end() ) {
return it->second.get();
} else {
std::tr1::shared_ptr< Obj > pObj( new Obj( filename ) );
store[ filename ] = pObj;
return pObj.get();
}
}
Is this already misuse of std::tr1::shared_ptr? And there are still
many naked pointers thrown around in the code (but clearly documented
as being "weak").
Am I right not to consider to return a std::tr1::weak_ptr from the get
method?
I hope this is a common building block pattern and would appreciate to
hear of your thoughts.
Regards,
Matthias
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]