CMS 3D CMS Logo

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 
23  bool Register( const Identifier & id, LogicCreator creator)
24  {
25  return m_associations.insert(typename std::map<Identifier, LogicCreator>::value_type(id,creator)).second;
26  }
27 
28  bool Unregister( const Identifier & id )
29  {
30  return m_associations.erase(id) == 1;
31  }
32 
33  std::unique_ptr<Ilogic> CreateObject( const Identifier & id ) const
34  {
35  auto itr = m_associations.find( id );
36 
37  if ( itr != m_associations.end() ) {
38  return std::unique_ptr<Ilogic>{( itr->second )()};
39  } else return nullptr; // handle error
40  }
41 
42 protected:
43 
44 private:
45 
46  typename std::map<Identifier, LogicCreator> m_associations;
47 
48 };
49 #endif // LOGICFACTORY_H
std::unique_ptr< Ilogic > CreateObject(const Identifier &id) const
Definition: LogicFactory.h:33
std::map< Identifier, LogicCreator > m_associations
Definition: LogicFactory.h:46
bool Register(const Identifier &id, LogicCreator creator)
Definition: LogicFactory.h:23
bool Unregister(const Identifier &id)
Definition: LogicFactory.h:28