ReuseFactory.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2013, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SURGSIM_FRAMEWORK_REUSEFACTORY_H
#define SURGSIM_FRAMEWORK_REUSEFACTORY_H
#include <memory>
#include <stack>
namespace SurgSim
{
namespace Framework
{
template <class T>
class ReuseFactory
{
friend class Deleter;
public:
ReuseFactory() {}
~ReuseFactory() {}
std::shared_ptr<T> getInstance()
{
std::shared_ptr<T> object;
if (m_unusedObjects.empty())
{
object = std::shared_ptr<T>(new T(), Deleter(this));
}
else
{
object = std::shared_ptr<T>(m_unusedObjects.top().release(), Deleter(this));
m_unusedObjects.pop();
}
return object;
}
private:
class Deleter
{
public:
explicit Deleter(ReuseFactory* factory) : m_factory(factory)
{
}
void operator()(T* unusedObject) const
{
m_factory->addUnused(unusedObject);
}
private:
ReuseFactory* m_factory;
};
void addUnused(T* unusedObject)
{
m_unusedObjects.push(std::unique_ptr<T>(unusedObject));
}
std::stack<std::unique_ptr<T>> m_unusedObjects;
};
}; // namespace Framework
}; // namespace SurgSim
#endif // SURGSIM_FRAMEWORK_REUSEFACTORY_H
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Framework::ReuseFactory::ReuseFactory
ReuseFactory()
Constructor. Initially no unused objects are available, so returned instances are new allocations...
Definition: ReuseFactory.h:63
SurgSim::Framework::ReuseFactory::~ReuseFactory
~ReuseFactory()
Destructor. Any remaining unused objects will be deleted.
Definition: ReuseFactory.h:65
SurgSim::Framework::ReuseFactory::m_unusedObjects
std::stack< std::unique_ptr< T > > m_unusedObjects
Stack of objects that are available for reuse.
Definition: ReuseFactory.h:116
SurgSim::Framework::ReuseFactory::getInstance
std::shared_ptr< T > getInstance()
Get a new or previously deleted object of class T.
Definition: ReuseFactory.h:69
SurgSim::Framework::ReuseFactory::Deleter::operator()
void operator()(T *unusedObject) const
Deletion method, adds the object to the ReuseFactory&#39;s collection.
Definition: ReuseFactory.h:98
SurgSim::Framework::ReuseFactory::Deleter::Deleter
Deleter(ReuseFactory *factory)
Constructor.
Definition: ReuseFactory.h:93
SurgSim::Framework::ReuseFactory::addUnused
void addUnused(T *unusedObject)
Adds an object to the stack of unused objects.
Definition: ReuseFactory.h:110
SurgSim::Framework::ReuseFactory::Deleter::m_factory
ReuseFactory * m_factory
ReuseFactory with the collection of unused objects for reuse.
Definition: ReuseFactory.h:104
SurgSim::Framework::ReuseFactory::Deleter
friend class Deleter
Custom Deleter is friended to manage unused objects rather than actually deleting them...
Definition: ReuseFactory.h:60
SurgSim::Framework::ReuseFactory::Deleter
Custom deleter to keep unused objects for reuse, rather than actually deleting them.
Definition: ReuseFactory.h:88
SurgSim::Framework::ReuseFactory
Factory for acquiring new or unused existing instances of class T to reduce repeated deallocation and...
Definition: ReuseFactory.h:57