33 #ifndef DART_COMMON_DETAIL_FACTORY_IMPL_HPP_
34 #define DART_COMMON_DETAIL_FACTORY_IMPL_HPP_
49 template <
typename T,
typename HeldT,
typename... Args>
52 static HeldT
run(Args&&... args)
54 return HeldT(
new T(std::forward<Args>(args)...));
59 template <
typename T,
typename... Args>
62 static std::unique_ptr<T>
run(Args&&... args)
64 return std::make_unique<T>(std::forward<Args>(args)...);
69 template <
typename T,
typename... Args>
72 static std::shared_ptr<T>
run(Args&&... args)
74 return std::make_shared<T>(std::forward<Args>(args)...);
83 template <
typename KeyT,
typename BaseT,
typename HeldT,
typename... Args>
89 return static_cast<std::size_t
>(t);
94 template <
typename KeyT,
typename BaseT,
typename HeldT,
typename... Args>
96 const KeyT& key,
Creator creator)
98 mCreatorMap[key] = std::move(creator);
102 template <
typename KeyT,
typename BaseT,
typename HeldT,
typename... Args>
103 template <
typename Derived>
106 return registerCreator(key, &Factory::defaultCreator<Derived>);
110 template <
typename KeyT,
typename BaseT,
typename HeldT,
typename... Args>
113 mCreatorMap.erase(key);
117 template <
typename KeyT,
typename BaseT,
typename HeldT,
typename... Args>
124 template <
typename KeyT,
typename BaseT,
typename HeldT,
typename... Args>
127 const auto it = mCreatorMap.find(key);
129 return (it != mCreatorMap.end());
133 template <
typename KeyT,
typename BaseT,
typename HeldT,
typename... Args>
135 const KeyT& key, Args&&... args)
137 const auto it = mCreatorMap.find(key);
139 const auto found = (it != mCreatorMap.end());
142 dtwarn <<
"[Factory] Failed to create an object of '"
143 <<
typeid(BaseT).
name() <<
"' class with the key (type: '"
144 <<
typeid(KeyT).
name() <<
"'). Returning nullptr instead.\n";
150 return it->second(std::forward<Args>(args)...);
154 template <
typename KeyT,
typename BaseT,
typename HeldT,
typename... Args>
157 std::unordered_set<KeyT> keys;
158 for (
const auto& entry : mCreatorMap)
159 keys.insert(entry.first);
165 template <
typename KeyT,
typename BaseT,
typename HeldT,
typename... Args>
166 template <
typename Derived>
170 std::forward<Args>(args)...);
181 const KeyT& key,
Creator creator)
183 SingletonFactory::getSingleton().registerCreator(key, creator);
196 SingletonFactory::getSingleton().template registerCreator<DerivedT>(key);
#define dtwarn
Output a warning message.
Definition: Console.hpp:46
std::string * name
Definition: SkelParser.cpp:1697
FactoryRegistrar(const KeyT &key, Creator creator)
Constructor.
Definition: Factory-impl.hpp:180
typename FactoryType::Creator Creator
Definition: Factory.hpp:130
Implementation of the Abstract Factory Pattern.
Definition: Factory.hpp:63
std::unordered_set< KeyT > getKeys() const
Get a set of the keys that are available for this Creator.
Definition: Factory-impl.hpp:155
bool canCreate(const KeyT &key)
Returns true if an object creator function is registered with the key.
Definition: Factory-impl.hpp:125
std::function< HeldT(Args...)> Creator
Definition: Factory.hpp:68
HeldT create(const KeyT &key, Args &&... args)
Creates an object of the class that is registered with a key.
Definition: Factory-impl.hpp:134
void registerCreator(const KeyT &key, Creator creator)
Registers a object creator function with a key.
Definition: Factory-impl.hpp:95
void unregisterCreator(const KeyT &key)
Unregisters the object creator function that is registered with a key.
Definition: Factory-impl.hpp:111
static HeldT defaultCreator(Args &&... args)
Definition: Factory-impl.hpp:167
void unregisterAllCreators()
Unregisters all the object creator functions.
Definition: Factory-impl.hpp:118
Definition: BulletCollisionDetector.cpp:65
Definition: SharedLibraryManager.hpp:46
Definition: Factory-impl.hpp:85
std::size_t operator()(T t) const
Definition: Factory-impl.hpp:87
static std::shared_ptr< T > run(Args &&... args)
Definition: Factory-impl.hpp:72
static std::unique_ptr< T > run(Args &&... args)
Definition: Factory-impl.hpp:62
The default creator.
Definition: Factory-impl.hpp:51
static HeldT run(Args &&... args)
Definition: Factory-impl.hpp:52