CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
LogicFactory.h
Go to the documentation of this file.
1 #ifndef LOGICFACTORY_H
2 #define LOGICFACTORY_H 1
3 
4 // Include files
5 #include <cstdlib>
6 #include <string>
7 #include <map>
8 #include <memory>
19 template <class Ilogic, typename Identifier, typename LogicCreator = Ilogic* (*)()>
20 class LogicFactory {
21 public:
22  bool Register(const Identifier& id, LogicCreator creator) {
23  return m_associations.insert(typename std::map<Identifier, LogicCreator>::value_type(id, creator)).second;
24  }
25 
26  bool Unregister(const Identifier& id) { return m_associations.erase(id) == 1; }
27 
28  std::unique_ptr<Ilogic> CreateObject(const Identifier& id) const {
29  auto itr = m_associations.find(id);
30 
31  if (itr != m_associations.end()) {
32  return std::unique_ptr<Ilogic>{(itr->second)()};
33  } else
34  return nullptr; // handle error
35  }
36 
37 protected:
38 private:
39  typename std::map<Identifier, LogicCreator> m_associations;
40 };
41 #endif // LOGICFACTORY_H
std::unique_ptr< Ilogic > CreateObject(const Identifier &id) const
Definition: LogicFactory.h:28
std::map< Identifier, LogicCreator > m_associations
Definition: LogicFactory.h:39
bool Register(const Identifier &id, LogicCreator creator)
Definition: LogicFactory.h:22
bool Unregister(const Identifier &id)
Definition: LogicFactory.h:26