22 #ifndef SINGA_UTILS_FACTORY_H_
23 #define SINGA_UTILS_FACTORY_H_
25 #include <glog/logging.h>
34 #define CreateInstance(SubClass, BaseClass) \
35 [](void)->BaseClass* {return new SubClass();}
55 const std::function<T*(
void)>& func) {
56 CHECK(str2func_.find(
id) == str2func_.end())
57 <<
"The id has been registered by another function";
68 const std::function<T*(
void)>& func) {
69 CHECK(id2func_.find(
id) == id2func_.end())
70 <<
"The id has been registered by another function";
78 inline T*
Create(
const std::string&
id) {
79 CHECK(str2func_.find(
id) != str2func_.end())
80 <<
"The creation function for " <<
id <<
" has not been registered";
81 return str2func_[id]();
89 CHECK(id2func_.find(
id) != id2func_.end())
90 <<
"The creation function for " <<
id <<
" has not been registered";
91 return id2func_[id]();
96 std::map<std::string, std::function<T*(void)>> str2func_;
97 std::map<int, std::function<T*(void)>> id2func_;
100 #endif // SINGA_UTILS_FACTORY_H_
Factory template to generate class (or a sub-class) object based on id.
Definition: factory.h:45
T * Create(const std::string &id)
create an instance by providing its id
Definition: factory.h:78
T * Create(int id)
create an instance by providing its id
Definition: factory.h:88
void Register(const std::string &id, const std::function< T *(void)> &func)
Register functions to create user defined classes.
Definition: factory.h:54
void Register(int id, const std::function< T *(void)> &func)
Register functions to create user defined classes.
Definition: factory.h:67