57 template <
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,
117 auto result = CreateObjectOrNull(
id, std::forward<Args>(args)...);
118 AERROR_IF(!result) <<
"Factory could not create Object of type : " << id;
123 MapContainer producers_;
void Clear()
Definition: factory.h:86
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Implements a Factory design pattern with Register and Create methods.
Definition: factory.h:60
bool Empty() const
Definition: factory.h:88
#define AERROR_IF(cond)
Definition: log.h:74
bool Contains(const IdentifierType &id)
Definition: factory.h:74
bool Unregister(const IdentifierType &id)
Unregisters the class with the given identifier.
Definition: factory.h:82
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 CreateO...
Definition: factory.h:98
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 CreateO...
Definition: factory.h:115
bool Register(const IdentifierType &id, ProductCreator creator)
Registers the class given by the creator function, linking it to id. Registration must happen prior t...
Definition: factory.h:70