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 <stdlib.h>
6 #include <string>
7 #include <map>
8 
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  typename std::map<Identifier, LogicCreator>::const_iterator itr;
31  itr = m_associations.find(id);
32  if( itr != m_associations.end() ) {
33  delete ( itr->second )() ;
34  }
35  return m_associations.erase(id) == 1;
36  }
37 
38  Ilogic* CreateObject( const Identifier & id )
39  {
40  typename std::map<Identifier, LogicCreator>::const_iterator itr;
41  itr = m_associations.find( id );
42 
43  if ( itr != m_associations.end() ) {
44  return ( itr->second )();
45  } else return NULL; // handle error
46  }
47 
48 protected:
49 
50 private:
51 
52  typename std::map<Identifier, LogicCreator> m_associations;
53 
54 };
55 #endif // LOGICFACTORY_H
Ilogic * CreateObject(const Identifier &id)
Definition: LogicFactory.h:38
std::map< Identifier, LogicCreator > m_associations
Definition: LogicFactory.h:52
#define NULL
Definition: scimark2.h:8
bool Register(const Identifier &id, LogicCreator creator)
Definition: LogicFactory.h:23
bool Unregister(const Identifier &id)
Definition: LogicFactory.h:28