|
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.
- 参数
-
IdentifierType | Type used for identifying the registered classes, typically std::string. |
AbstractProduct | The interface implemented by the registered classes |
ProductCreator | Function returning a pointer to an instance of the registered class |
MapContainer | Internal implementation of the function mapping IdentifierType to ProductCreator, by default std::unordered_map |
在文件 factory.h 第 60 行定义.
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.
- 参数
-
id | The identifier of the class we which to instantiate |
args | the object construction arguments |
在文件 factory.h 第 115 行定义.
116 {
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.
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.
- 参数
-
id | The identifier of the class we which to instantiate |
args | the object construction arguments |
在文件 factory.h 第 98 行定义.
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 }
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.
- 参数
-
id | Identifier of the class being registered |
creator | Function returning a pointer to an instance of the registered class |
- 返回
- True if the key id is still available
在文件 factory.h 第 70 行定义.
70 {
71 return producers_.insert(std::make_pair(id, creator)).second;
72 }