Skip to content

Commit

Permalink
Convert bare pointer to smart pointer in core/util/Factory (#1950)
Browse files Browse the repository at this point in the history
  • Loading branch information
mschicker authored and bmarchant committed Nov 10, 2017
1 parent cd0abed commit 1fb42e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
14 changes: 4 additions & 10 deletions hoot-core/src/main/cpp/hoot/core/util/Factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ Factory::Factory()

Factory::~Factory()
{
QMutexLocker locker(&_mutex);
for (std::map<std::string, ObjectCreator*>::const_iterator it = _creators.begin();
it != _creators.end(); ++it)
{
delete it->second;
}
}

any Factory::constructObject(const std::string& name)
Expand All @@ -65,7 +59,7 @@ any Factory::constructObject(const std::string& name)
{
throw HootException("Could not find object to construct. (" + name + ")");
}
ObjectCreator* c = _creators[name];
boost::shared_ptr<ObjectCreator> c = _creators[name];
locker.unlock();

return c->create();
Expand All @@ -85,10 +79,10 @@ vector<std::string> Factory::getObjectNamesByBase(const std::string& baseName)
QMutexLocker locker(&_mutex);
vector<std::string> result;

for (std::map<std::string, ObjectCreator*>::const_iterator it = _creators.begin();
for (std::map<std::string, boost::shared_ptr<ObjectCreator> >::const_iterator it = _creators.begin();
it != _creators.end(); ++it)
{
ObjectCreator* c = it->second;
boost::shared_ptr<ObjectCreator> c = it->second;
if (c->getBaseName() == baseName)
{
result.push_back(c->getName());
Expand All @@ -102,7 +96,7 @@ bool Factory::hasClass(const std::string& name)
return _creators.find(name) != _creators.end();
}

void Factory::registerCreator(ObjectCreator* oc, bool baseClass)
void Factory::registerCreator(boost::shared_ptr<ObjectCreator> oc, bool baseClass)
{
QMutexLocker locker(&_mutex);
if (baseClass == false && oc->getBaseName() == oc->getName())
Expand Down
8 changes: 4 additions & 4 deletions hoot-core/src/main/cpp/hoot/core/util/Factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class Factory
* @param baseClass If set to true, then the object creator may implement the base class. This is
* very unusual, but accidentally forgetting to implement "string className()" is quite common.
*/
void registerCreator(ObjectCreator* oc, bool baseClass = false);
void registerCreator(boost::shared_ptr<ObjectCreator> oc, bool baseClass = false);

private:
Factory();
Expand All @@ -173,7 +173,7 @@ class Factory

QMutex _mutex;

std::map<std::string, ObjectCreator*> _creators;
std::map<std::string, boost::shared_ptr<ObjectCreator> > _creators;
};

template<class Base, class T>
Expand All @@ -184,8 +184,8 @@ class AutoRegister
*/
AutoRegister(std::string baseName, std::string name, bool baseClass = false)
{
Factory::getInstance().registerCreator(
new ObjectCreatorTemplate<Base, T>(baseName, name), baseClass);
Factory::getInstance().registerCreator(boost::shared_ptr<ObjectCreatorTemplate<Base, T> >(
new ObjectCreatorTemplate<Base, T>(baseName, name)), baseClass);
}
};

Expand Down

0 comments on commit 1fb42e7

Please sign in to comment.