43 template <
typename Tbaseclass,
typename... Targs>
59 template <
typename Tderived
class>
61 registry[key].reset(
new Creator<Tderivedclass>);
70 std::shared_ptr<Tbaseclass>
create(
const std::string& key, Targs&&... args) {
71 if (!this->
check(key))
72 throw std::runtime_error(
"Invalid key: " + key +
73 ", not found in the factory register!");
74 return registry.at(key)->create(std::forward<Targs>(args)...);
82 bool check(
const std::string& key)
const {
84 for (
const auto& keyvalue : registry)
85 if (keyvalue.first == key) status =
true;
93 std::vector<std::string>
list()
const {
94 std::vector<std::string> factory_items;
95 for (
const auto& keyvalue : registry)
96 factory_items.push_back(keyvalue.first);
111 virtual std::shared_ptr<Tbaseclass>
create(Targs&&...) = 0;
118 template <
typename Tderived
class>
119 struct Creator :
public CreatorBase {
123 std::shared_ptr<Tbaseclass>
create(Targs&&... args)
override {
124 return std::make_shared<Tderivedclass>(std::forward<Targs>(args)...);
128 std::map<std::string, std::shared_ptr<CreatorBase>> registry;
139 template <
typename Tbaseclass,
typename Tderivedclass,
typename... Targs>
149 ->template register_factory<Tderivedclass>(key);
153 #endif // _FACTORY_H_ void register_factory(const std::string &key)
std::shared_ptr< Tbaseclass > create(const std::string &key, Targs &&...args)
Register(const std::string &key)
static Factory * instance()
std::vector< std::string > list() const
bool check(const std::string &key) const