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 dart::common::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,
92 return static_cast<std::size_t
>(t);
97 template <
typename KeyT,
102 const KeyT& key,
Creator creator)
104 mCreatorMap[key] = std::move(creator);
108 template <
typename KeyT,
112 template <
typename Derived>
116 return registerCreator(
118 &Factory::defaultCreator<Derived>
123 template <
typename KeyT,
130 mCreatorMap.erase(key);
134 template <
typename KeyT,
144 template <
typename KeyT,
150 const auto it = mCreatorMap.find(key);
152 return (it != mCreatorMap.end());
156 template <
typename KeyT,
161 const KeyT& key, Args&&... args)
163 const auto it = mCreatorMap.find(key);
165 const auto found = (it != mCreatorMap.end());
168 dtwarn <<
"[Factory] Failed to create an object of '"
169 <<
typeid(BaseT).
name() <<
"' class with the key (type: '"
170 <<
typeid(KeyT).
name() <<
"'). Returning nullptr instead.\n";
176 return it->second(std::forward<Args>(args)...);
180 template <
typename KeyT,
186 std::unordered_set<KeyT> keys;
187 for(
const auto& entry : mCreatorMap)
188 keys.insert(entry.first);
194 template <
typename KeyT,
198 template <
typename Derived>
202 std::forward<Args>(args)...);
206 template <
typename KeyT,
214 SingletonFactory::getSingleton().registerCreator(key, creator);
218 template <
typename KeyT,
226 SingletonFactory::getSingleton().template registerCreator<DerivedT>(key);
#define dtwarn
Output a warning message.
Definition: Console.hpp:46
std::string * name
Definition: SkelParser.cpp:1642
FactoryRegistrar(const KeyT &key, Creator creator)
Constructor.
Definition: Factory-impl.hpp:212
typename FactoryType::Creator Creator
Definition: Factory.hpp:126
Implementation of the Abstract Factory Pattern.
Definition: Factory.hpp:62
std::unordered_set< KeyT > getKeys() const
Get a set of the keys that are available for this Creator.
Definition: Factory-impl.hpp:184
bool canCreate(const KeyT &key)
Returns true if an object creator function is registered with the key.
Definition: Factory-impl.hpp:148
std::function< HeldT(Args...)> Creator
Definition: Factory.hpp:67
HeldT create(const KeyT &key, Args &&... args)
Creates an object of the class that is registered with a key.
Definition: Factory-impl.hpp:160
void registerCreator(const KeyT &key, Creator creator)
Registers a object creator function with a key.
Definition: Factory-impl.hpp:101
void unregisterCreator(const KeyT &key)
Unregisters the object creator function that is registered with a key.
Definition: Factory-impl.hpp:127
static HeldT defaultCreator(Args &&... args)
Definition: Factory-impl.hpp:199
void unregisterAllCreators()
Unregisters all the object creator functions.
Definition: Factory-impl.hpp:138
Definition: BulletCollisionDetector.cpp:63
Definition: SharedLibraryManager.hpp:43
Definition: Factory-impl.hpp:88
std::size_t operator()(T t) const
Definition: Factory-impl.hpp:90
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