22 #ifndef SINGA_UTILS_FACTORY_H_ 23 #define SINGA_UTILS_FACTORY_H_ 29 #include "singa/utils/logging.h" 34 #define CreateInstance(SubClass, BaseClass) \ 35 [](void)->BaseClass* {return new SubClass();} 44 template<
typename T,
typename ID = std::
string>
55 const std::function<T*(
void)>& creator) {
56 Registry* reg = GetRegistry();
68 Registry* reg = GetRegistry();
69 CHECK(reg->find(
id) != reg->end())
70 <<
"The creation function for " <<
id <<
" has not been registered";
74 static const std::vector<ID> GetIDs() {
76 for (
const auto entry : *GetRegistry())
77 keys.push_back(entry.first);
83 typedef std::map<ID, std::function<T*(void)>> Registry;
84 static Registry* GetRegistry() {
90 template<
typename Base,
typename Sub,
typename ID = std::
string>
97 #endif // SINGA_UTILS_FACTORY_H_
Factory template to generate class (or a sub-class) object based on id.
Definition: factory.h:45
static void Register(const ID &id, const std::function< T *(void)> &creator)
Register functions to create user defined classes.
Definition: factory.h:54
static T * Create(const ID &id)
create an instance by providing its id
Definition: factory.h:67