57template <
typename IdentifierType,
class AbstractProduct,
58 class ProductCreator = AbstractProduct *(*)(),
59 class MapContainer = std::map<IdentifierType, ProductCreator>>
70 bool Register(
const IdentifierType &
id, ProductCreator creator) {
71 return producers_.insert(std::make_pair(
id, creator)).second;
75 return producers_.find(
id) != producers_.end();
83 return producers_.erase(
id) == 1;
86 void Clear() { producers_.clear(); }
88 bool Empty()
const {
return producers_.empty(); }
97 template <
typename... Args>
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)...));
114 template <
typename... Args>
115 std::unique_ptr<AbstractProduct>
CreateObject(
const IdentifierType &
id,
118 AERROR_IF(!result) <<
"Factory could not create Object of type : " << id;
123 MapContainer producers_;
Implements a Factory design pattern with Register and Create methods
std::unique_ptr< AbstractProduct > CreateObjectOrNull(const IdentifierType &id, Args &&... args)
Creates and transfers membership of an object of type matching id.
std::unique_ptr< AbstractProduct > CreateObject(const IdentifierType &id, Args &&... args)
Creates and transfers membership of an object of type matching id.
bool Contains(const IdentifierType &id)
bool Register(const IdentifierType &id, ProductCreator creator)
Registers the class given by the creator function, linking it to id.
bool Unregister(const IdentifierType &id)
Unregisters the class with the given identifier