There's the demand of passing a name and doing the class construction of this name. I don't want to make the switch case on the construct. Let's do the hack!
I found the base implementation in stackoverflow.
template <class T> void* constructor() { return (void*)new T(); }
struct factory
{
typedef void*(*constructor_t)();
typedef std::map<std::string, constructor_t> map_type;
map_type m_classes;
template <class T>
void register_class(std::string const& n)
{ m_classes.insert(std::make_pair(n, &constructor<T>)); }
void* construct(std::string const& n)
{
map_type::iterator i = m_classes.find(n);
if (i == m_classes.end()) return 0; // or throw or whatever you want
return i->second();
}
};
factory g_factory;
#define REGISTER_CLASS(n) g_factory.register_class<n>(#n)
The problem is it does not allow the arg passing in construction. My class accepts the arguments module
.
template <class T, typename M_> void *constructor(M_ *module_) {
return (void *)new T{reinterpret_cast<M_ *>(module_)};
}
template <typename M_> struct arg_to_pass {
typedef void *(*constructor_t)(M_ *);
typedef std::map<std::string, constructor_t> map_type;
map_type m_classes;
M_ *module;
template <class T> void register_class(std::string const &n, M_ *&module_) {
module = module_;
m_classes.insert(std::make_pair(n, &constructor<T, M_>));
}
void *construct(std::string const &n) {
auto i = m_classes.find(n);
if (i == m_classes.end())
return nullptr; // or throw or whatever you want
return i->second(module);
}
};
arg_to_pass<Module> pass_factory;
#define REGISTER_CLASS(n, m_) pass_factory.register_class<n>(#n, m_)
This will resolve all the problem.