Apache SINGA
A distributed deep learning platform .
 All Classes Namespaces Files Functions Variables Typedefs Macros
factory.h
1 /************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21 
22 #ifndef SINGA_UTILS_FACTORY_H_
23 #define SINGA_UTILS_FACTORY_H_
24 
25 #include <glog/logging.h>
26 #include <functional>
27 #include <map>
28 #include <string>
29 
34 #define CreateInstance(SubClass, BaseClass) \
35  [](void)->BaseClass* {return new SubClass();}
36 
44 template<typename T>
45 class Factory {
46  public:
54  inline void Register(const std::string& id,
55  const std::function<T*(void)>& func) {
56  CHECK(str2func_.find(id) == str2func_.end())
57  << "The id has been registered by another function";
58  str2func_[id] = func;
59  }
67  inline void Register(int id,
68  const std::function<T*(void)>& func) {
69  CHECK(id2func_.find(id) == id2func_.end())
70  << "The id has been registered by another function";
71  id2func_[id] = func;
72  }
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]();
82  }
88  inline T* Create(int id) {
89  CHECK(id2func_.find(id) != id2func_.end())
90  << "The creation function for " << id << " has not been registered";
91  return id2func_[id]();
92  }
93 
94  private:
95  // Map that stores the registered creation functions
96  std::map<std::string, std::function<T*(void)>> str2func_;
97  std::map<int, std::function<T*(void)>> id2func_;
98 };
99 
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