12 #include <unordered_map> 23 template<
typename T,
class ... Args>
29 using Creator = std::function<T(Args...)>;
35 static T
create(
const std::string& name, Args&& ... args)
38 return registry().at(name)(std::forward<Args>(args)...);
46 registry()[name] = std::move(c);
52 return registry().find(name) != registry().cend();
56 static const std::vector<std::string>
getNames()
58 std::vector<std::string> result;
59 for (
const auto& item : registry())
61 result.push_back(item.first);
67 using Registry = std::unordered_map<std::string, Creator>;
70 static Registry& registry()
72 static Registry registry = {};
86 template<
typename T,
typename U,
typename ... Args>
95 static_assert(std::is_base_of<T, U>::value,
96 "U must be a subclass of T");
Generic Factory class that can take objects with constructor parameters.
static const std::vector< std::string > getNames()
std::function< std::shared_ptr< DeviceManager >(Args...)> Creator
Type of the function to generate a new object.
static bool contains(const std::string &name)
static void add(const std::string &name, Creator c)
adds a new creation function to the factory
SharedObjectRegistrar(std::string name)
The constructor can automatically register the given class in the Factory For example it can be used ...
static T create(const std::string &name, Args &&... args)
tries to construct the object give name, it will forward the given paramters
Templated class that can add to the object factory with objects that will be generated via std::make_...