Apollo  6.0
Open source self driving car software
Public Member Functions | List of all members
apollo::common::util::Factory< IdentifierType, AbstractProduct, ProductCreator, MapContainer > Class Template Reference

Implements a Factory design pattern with Register and Create methods. More...

#include <factory.h>

Collaboration diagram for apollo::common::util::Factory< IdentifierType, AbstractProduct, ProductCreator, MapContainer >:
Collaboration graph

Public Member Functions

bool Register (const IdentifierType &id, ProductCreator creator)
 Registers the class given by the creator function, linking it to id. Registration must happen prior to calling CreateObject. More...
 
bool Contains (const IdentifierType &id)
 
bool Unregister (const IdentifierType &id)
 Unregisters the class with the given identifier. More...
 
void Clear ()
 
bool Empty () const
 
template<typename... Args>
std::unique_ptr< AbstractProduct > CreateObjectOrNull (const IdentifierType &id, Args &&... args)
 Creates and transfers membership of an object of type matching id. Need to register id before CreateObject is called. May return nullptr silently. More...
 
template<typename... Args>
std::unique_ptr< AbstractProduct > CreateObject (const IdentifierType &id, Args &&... args)
 Creates and transfers membership of an object of type matching id. Need to register id before CreateObject is called. More...
 

Detailed Description

template<typename IdentifierType, class AbstractProduct, class ProductCreator = AbstractProduct *(*)(), class MapContainer = std::map<IdentifierType, ProductCreator>>
class apollo::common::util::Factory< IdentifierType, AbstractProduct, ProductCreator, MapContainer >

Implements a Factory design pattern with Register and Create methods.

The objects created by this factory all implement the same interface (namely, AbstractProduct). This design pattern is useful in settings where multiple implementations of an interface are available, and one wishes to defer the choice of the implementation in use.

Parameters
IdentifierTypeType used for identifying the registered classes, typically std::string.
AbstractProductThe interface implemented by the registered classes
ProductCreatorFunction returning a pointer to an instance of the registered class
MapContainerInternal implementation of the function mapping IdentifierType to ProductCreator, by default std::unordered_map

Member Function Documentation

◆ Clear()

template<typename IdentifierType, class AbstractProduct, class ProductCreator = AbstractProduct *(*)(), class MapContainer = std::map<IdentifierType, ProductCreator>>
void apollo::common::util::Factory< IdentifierType, AbstractProduct, ProductCreator, MapContainer >::Clear ( )
inline

◆ Contains()

template<typename IdentifierType, class AbstractProduct, class ProductCreator = AbstractProduct *(*)(), class MapContainer = std::map<IdentifierType, ProductCreator>>
bool apollo::common::util::Factory< IdentifierType, AbstractProduct, ProductCreator, MapContainer >::Contains ( const IdentifierType &  id)
inline

◆ CreateObject()

template<typename IdentifierType, class AbstractProduct, class ProductCreator = AbstractProduct *(*)(), class MapContainer = std::map<IdentifierType, ProductCreator>>
template<typename... Args>
std::unique_ptr<AbstractProduct> apollo::common::util::Factory< IdentifierType, AbstractProduct, ProductCreator, MapContainer >::CreateObject ( const IdentifierType &  id,
Args &&...  args 
)
inline

Creates and transfers membership of an object of type matching id. Need to register id before CreateObject is called.

Parameters
idThe identifier of the class we which to instantiate
argsthe object construction arguments

◆ CreateObjectOrNull()

template<typename IdentifierType, class AbstractProduct, class ProductCreator = AbstractProduct *(*)(), class MapContainer = std::map<IdentifierType, ProductCreator>>
template<typename... Args>
std::unique_ptr<AbstractProduct> apollo::common::util::Factory< IdentifierType, AbstractProduct, ProductCreator, MapContainer >::CreateObjectOrNull ( const IdentifierType &  id,
Args &&...  args 
)
inline

Creates and transfers membership of an object of type matching id. Need to register id before CreateObject is called. May return nullptr silently.

Parameters
idThe identifier of the class we which to instantiate
argsthe object construction arguments

◆ Empty()

template<typename IdentifierType, class AbstractProduct, class ProductCreator = AbstractProduct *(*)(), class MapContainer = std::map<IdentifierType, ProductCreator>>
bool apollo::common::util::Factory< IdentifierType, AbstractProduct, ProductCreator, MapContainer >::Empty ( ) const
inline

◆ Register()

template<typename IdentifierType, class AbstractProduct, class ProductCreator = AbstractProduct *(*)(), class MapContainer = std::map<IdentifierType, ProductCreator>>
bool apollo::common::util::Factory< IdentifierType, AbstractProduct, ProductCreator, MapContainer >::Register ( const IdentifierType &  id,
ProductCreator  creator 
)
inline

Registers the class given by the creator function, linking it to id. Registration must happen prior to calling CreateObject.

Parameters
idIdentifier of the class being registered
creatorFunction returning a pointer to an instance of the registered class
Returns
True if the key id is still available

◆ Unregister()

template<typename IdentifierType, class AbstractProduct, class ProductCreator = AbstractProduct *(*)(), class MapContainer = std::map<IdentifierType, ProductCreator>>
bool apollo::common::util::Factory< IdentifierType, AbstractProduct, ProductCreator, MapContainer >::Unregister ( const IdentifierType &  id)
inline

Unregisters the class with the given identifier.

Parameters
idThe identifier of the class to be unregistered

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