Apache SINGA
A distributed deep learning platform .
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Macros
factory.h
1 #ifndef INCLUDE_UTILS_FACTORY_H_
2 #define INCLUDE_UTILS_FACTORY_H_
3 #include <glog/logging.h>
4 
5 #include <functional>
6 #include <utility>
7 #include <map>
12 #define CreateInstance(SubClass, BaseClass) \
13  [](void)->BaseClass* {return new SubClass();}
14 
23 template<typename T>
24 class Factory{
25  //template<Factory<T>> friend class Singleton;
26  public:
33  void Register(const std::string id, std::function<T*(void)> func);
38  T *Create(const std::string id);
39 
40  private:
41  //<! Map that stores the registered creation functions
42  std::map<std::string, std::function<T*(void)>> str2func_;
43 };
44 
45 template<typename T>
46 void Factory<T>::Register(const std::string id,
47  std::function<T*(void)> func) {
48  str2func_[id] = func;
49 }
50 
51 template<typename T>
52 T *Factory<T>::Create(const std::string id) {
53  CHECK(str2func_.find(id) != str2func_.end())
54  << "The creation function for " << id << " has not been registered";
55  return str2func_[id]();
56 }
57 #endif // INCLUDE_UTILS_FACTORY_H_
T * Create(const std::string id)
create a layer instance by providing its type
Definition: factory.h:52
factory template to generate class (or a sub-class) object based on id.
Definition: factory.h:24
void Register(const std::string id, std::function< T *(void)> func)
Register functions to create user defined classes.
Definition: factory.h:46