Apollo 10.0
自动驾驶开放平台
apollo::common::util::Factory< IdentifierType, AbstractProduct, ProductCreator, MapContainer > 模板类 参考

Implements a Factory design pattern with Register and Create methods 更多...

#include <factory.h>

apollo::common::util::Factory< IdentifierType, AbstractProduct, ProductCreator, MapContainer > 的协作图:

Public 成员函数

bool Register (const IdentifierType &id, ProductCreator creator)
 Registers the class given by the creator function, linking it to id.
 
bool Contains (const IdentifierType &id)
 
bool Unregister (const IdentifierType &id)
 Unregisters the class with the given identifier
 
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.
 
template<typename... Args>
std::unique_ptr< AbstractProduct > CreateObject (const IdentifierType &id, Args &&... args)
 Creates and transfers membership of an object of type matching id.
 

详细描述

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.

参数
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

在文件 factory.h60 行定义.

成员函数说明

◆ 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

在文件 factory.h86 行定义.

86{ producers_.clear(); }

◆ 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

在文件 factory.h74 行定义.

74 {
75 return producers_.find(id) != producers_.end();
76 }

◆ 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.

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

在文件 factory.h115 行定义.

116 {
117 auto result = CreateObjectOrNull(id, std::forward<Args>(args)...);
118 AERROR_IF(!result) << "Factory could not create Object of type : " << id;
119 return result;
120 }
std::unique_ptr< AbstractProduct > CreateObjectOrNull(const IdentifierType &id, Args &&... args)
Creates and transfers membership of an object of type matching id.
Definition factory.h:98
#define AERROR_IF(cond)
Definition log.h:74

◆ 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.

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

在文件 factory.h98 行定义.

99 {
100 auto id_iter = producers_.find(id);
101 if (id_iter != producers_.end()) {
102 return std::unique_ptr<AbstractProduct>(
103 (id_iter->second)(std::forward<Args>(args)...));
104 }
105 return nullptr;
106 }

◆ 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

在文件 factory.h88 行定义.

88{ return producers_.empty(); }

◆ 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.

参数
idIdentifier of the class being registered
creatorFunction returning a pointer to an instance of the registered class
返回
True if the key id is still available

在文件 factory.h70 行定义.

70 {
71 return producers_.insert(std::make_pair(id, creator)).second;
72 }

◆ 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

参数
idThe identifier of the class to be unregistered

在文件 factory.h82 行定义.

82 {
83 return producers_.erase(id) == 1;
84 }

该类的文档由以下文件生成: