33#ifndef DART_COMMON_DETAIL_FACTORY_IMPL_HPP_
34#define DART_COMMON_DETAIL_FACTORY_IMPL_HPP_
48template <
typename T,
typename HeldT,
typename... Args>
51 static HeldT
run(Args&&... args)
53 return HeldT(
new T(std::forward<Args>(args)...));
58template <
typename T,
typename... Args>
61 static std::unique_ptr<T>
run(Args&&... args)
63 return std::make_unique<T>(std::forward<Args>(args)...);
68template <
typename T,
typename... Args>
71 static std::shared_ptr<T>
run(Args&&... args)
73 return std::make_shared<T>(std::forward<Args>(args)...);
82template <
typename KeyT,
typename BaseT,
typename HeldT,
typename... Args>
88 return static_cast<std::size_t
>(t);
93template <
typename KeyT,
typename BaseT,
typename HeldT,
typename... Args>
95 const KeyT& key,
Creator creator)
97 mCreatorMap[key] = std::move(creator);
101template <
typename KeyT,
typename BaseT,
typename HeldT,
typename... Args>
102template <
typename Derived>
105 return registerCreator(key, &Factory::defaultCreator<Derived>);
109template <
typename KeyT,
typename BaseT,
typename HeldT,
typename... Args>
112 mCreatorMap.erase(key);
116template <
typename KeyT,
typename BaseT,
typename HeldT,
typename... Args>
123template <
typename KeyT,
typename BaseT,
typename HeldT,
typename... Args>
126 const auto it = mCreatorMap.find(key);
128 return (it != mCreatorMap.end());
132template <
typename KeyT,
typename BaseT,
typename HeldT,
typename... Args>
134 const KeyT& key, Args&&... args)
136 const auto it = mCreatorMap.find(key);
138 const auto found = (it != mCreatorMap.end());
141 dtwarn <<
"[Factory] Failed to create an object of '"
142 <<
typeid(BaseT).
name() <<
"' class with the key (type: '"
143 <<
typeid(KeyT).
name() <<
"'). Returning nullptr instead.\n";
149 return it->second(std::forward<Args>(args)...);
153template <
typename KeyT,
typename BaseT,
typename HeldT,
typename... Args>
156 std::unordered_set<KeyT> keys;
157 for (
const auto& entry : mCreatorMap)
158 keys.insert(entry.first);
164template <
typename KeyT,
typename BaseT,
typename HeldT,
typename... Args>
165template <
typename Derived>
169 std::forward<Args>(args)...);
180 const KeyT& key,
Creator creator)
182 SingletonFactory::getSingleton().registerCreator(key, creator);
195 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:179
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:154
bool canCreate(const KeyT &key)
Returns true if an object creator function is registered with the key.
Definition Factory-impl.hpp:124
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:133
void registerCreator(const KeyT &key, Creator creator)
Registers a object creator function with a key.
Definition Factory-impl.hpp:94
void unregisterCreator(const KeyT &key)
Unregisters the object creator function that is registered with a key.
Definition Factory-impl.hpp:110
static HeldT defaultCreator(Args &&... args)
Definition Factory-impl.hpp:166
void unregisterAllCreators()
Unregisters all the object creator functions.
Definition Factory-impl.hpp:117
Definition BulletCollisionDetector.cpp:60
Definition SharedLibraryManager.hpp:46
Definition Factory-impl.hpp:84
std::size_t operator()(T t) const
Definition Factory-impl.hpp:86
static std::shared_ptr< T > run(Args &&... args)
Definition Factory-impl.hpp:71
static std::unique_ptr< T > run(Args &&... args)
Definition Factory-impl.hpp:61
The default creator.
Definition Factory-impl.hpp:50
static HeldT run(Args &&... args)
Definition Factory-impl.hpp:51