All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ReuseFactory.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_FRAMEWORK_REUSEFACTORY_H
17 #define SURGSIM_FRAMEWORK_REUSEFACTORY_H
18 
19 #include <memory>
20 #include <stack>
21 
22 namespace SurgSim
23 {
24 
25 namespace Framework
26 {
27 
56 template <class T>
58 {
60  friend class Deleter;
61 public:
66 
69  std::shared_ptr<T> getInstance()
70  {
71  std::shared_ptr<T> object;
72 
73  if (m_unusedObjects.empty())
74  {
75  object = std::shared_ptr<T>(new T(), Deleter(this));
76  }
77  else
78  {
79  object = std::shared_ptr<T>(m_unusedObjects.top().release(), Deleter(this));
80  m_unusedObjects.pop();
81  }
82 
83  return object;
84  }
85 
86 private:
88  class Deleter
89  {
90  public:
93  explicit Deleter(ReuseFactory* factory) : m_factory(factory)
94  {
95  }
98  void operator()(T* unusedObject) const
99  {
100  m_factory->addUnused(unusedObject);
101  }
102  private:
105  };
106 
110  void addUnused(T* unusedObject)
111  {
112  m_unusedObjects.push(std::unique_ptr<T>(unusedObject));
113  }
114 
116  std::stack<std::unique_ptr<T>> m_unusedObjects;
117 };
118 
119 }; // namespace Framework
120 
121 }; // namespace SurgSim
122 
123 #endif // SURGSIM_FRAMEWORK_REUSEFACTORY_H
ReuseFactory()
Constructor. Initially no unused objects are available, so returned instances are new allocations...
Definition: ReuseFactory.h:63
~ReuseFactory()
Destructor. Any remaining unused objects will be deleted.
Definition: ReuseFactory.h:65
std::stack< std::unique_ptr< T > > m_unusedObjects
Stack of objects that are available for reuse.
Definition: ReuseFactory.h:116
std::shared_ptr< T > getInstance()
Get a new or previously deleted object of class T.
Definition: ReuseFactory.h:69
Deleter(ReuseFactory *factory)
Constructor.
Definition: ReuseFactory.h:93
void addUnused(T *unusedObject)
Adds an object to the stack of unused objects.
Definition: ReuseFactory.h:110
ReuseFactory * m_factory
ReuseFactory with the collection of unused objects for reuse.
Definition: ReuseFactory.h:104
friend class Deleter
Custom Deleter is friended to manage unused objects rather than actually deleting them...
Definition: ReuseFactory.h:60
Custom deleter to keep unused objects for reuse, rather than actually deleting them.
Definition: ReuseFactory.h:88
Factory for acquiring new or unused existing instances of class T to reduce repeated deallocation and...
Definition: ReuseFactory.h:57
void operator()(T *unusedObject) const
Deletion method, adds the object to the ReuseFactory's collection.
Definition: ReuseFactory.h:98