SurgSim::Framework::ReuseFactory< T > Class Template Reference

Factory for acquiring new or unused existing instances of class T to reduce repeated deallocation and reallocation of objects with short lifespans. More...

`#include <SurgSim/Framework/ReuseFactory.h>`

## Classes

class | Deleter
| Custom deleter to keep unused objects for reuse, rather than actually deleting them. More...

## Public Member Functions

| ReuseFactory ()
| Constructor. Initially no unused objects are available, so returned instances are new allocations. More...
| ~ReuseFactory ()
| Destructor. Any remaining unused objects will be deleted. More...
std::shared_ptr< T > | getInstance ()
| Get a new or previously deleted object of class T. More...

## Private Member Functions

void | addUnused (T *unusedObject)
| Adds an object to the stack of unused objects. More...

## Private Attributes

std::stack< std::unique_ptr< T > > | m_unusedObjects
| Stack of objects that are available for reuse. More...

## Friends

class | Deleter
| Custom Deleter is friended to manage unused objects rather than actually deleting them. More...

## Detailed Description

### template<class T>
class SurgSim::Framework::ReuseFactory< T >

Factory for acquiring new or unused existing instances of class T to reduce repeated deallocation and reallocation of objects with short lifespans.

Example Usage:

{
// Create an instance of the factory to provide instances of MyObject.
ReuseFactory<MyObject> factory;
// Get an instance of MyObject. This instance will be allocated as no unused objects are available yet.
std::shared_ptr<MyObject> myObject = factory.getInstance();
// Setup the provided object, as the state is not guaranteed to be initialized.
myObject.set(...);
}
// When myObject goes out of scope, it will automatically be added back to the factory for reuse later.
// This will return the previous, now unused, instance rather than allocating anew.
std::shared_ptr<MyObject> myObject2 = factory.getInstance();
// Setup the provided object, as the state is not guaranteed to be initialized.
myObject2.set(...);

Limitations:

- The class T must have a default constructor.
- The state of returned objects are in no way reset, so the state will need to be setup after retrieving the instance from the factory.
- Custom deleters for an instance of T cannot be specified, as a custom deleter is used to manage the unused objects.

Template Parameters

  T | Instances of this class are provided by this factory

## Constructor & Destructor Documentation

## ReuseFactory()

template<class T >

SurgSim::Framework::ReuseFactory< T >::ReuseFactory | ( |  | ) |

inline

Constructor. Initially no unused objects are available, so returned instances are new allocations.

## ~ReuseFactory()

template<class T >

SurgSim::Framework::ReuseFactory< T >::~ReuseFactory | ( |  | ) |

inline

Destructor. Any remaining unused objects will be deleted.

## Member Function Documentation

## addUnused()

template<class T >

void SurgSim::Framework::ReuseFactory< T >::addUnused | ( | T * | unusedObject | ) |

inline private

Adds an object to the stack of unused objects.

This should only be called from Deleter.

Parameters

  unusedObject | Object that is no longer referenced by any shared pointers

## getInstance()

template<class T >

std::shared_ptr<T> SurgSim::Framework::ReuseFactory< T >::getInstance | ( |  | ) |

inline

Get a new or previously deleted object of class T.

Returns
  Valid shared pointer to an object of class T

## Friends And Related Function Documentation

## Deleter

template<class T >

friend class Deleter

friend

Custom Deleter is friended to manage unused objects rather than actually deleting them.

## Member Data Documentation

## m_unusedObjects

template<class T >

std::stack<std::unique_ptr<T> > SurgSim::Framework::ReuseFactory< T >::m_unusedObjects

private

Stack of objects that are available for reuse.

The documentation for this class was generated from the following file:

- SurgSim/Framework/ReuseFactory.h