CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
LogicFactory.h
Go to the documentation of this file.
1 // $Id: LogicFactory.h,v 1.1 2009/05/16 19:43:30 aosorio Exp $
2 #ifndef LOGICFACTORY_H
3 #define LOGICFACTORY_H 1
4 
5 // Include files
6 #include <stdlib.h>
7 #include <string>
8 #include <map>
9 
20 template <class Ilogic, typename Identifier, typename LogicCreator = Ilogic * (*)()>
21 class LogicFactory {
22 public:
23 
24  bool Register( const Identifier & id, LogicCreator creator)
25  {
26  return m_associations.insert(typename std::map<Identifier, LogicCreator>::value_type(id,creator)).second;
27  }
28 
29  bool Unregister( const Identifier & id )
30  {
31  typename std::map<Identifier, LogicCreator>::const_iterator itr;
32  itr = m_associations.find(id);
33  if( itr != m_associations.end() ) {
34  delete ( itr->second )() ;
35  }
36  return m_associations.erase(id) == 1;
37  }
38 
39  Ilogic* CreateObject( const Identifier & id )
40  {
41  typename std::map<Identifier, LogicCreator>::const_iterator itr;
42  itr = m_associations.find( id );
43 
44  if ( itr != m_associations.end() ) {
45  return ( itr->second )();
46  } else return NULL; // handle error
47  }
48 
49 protected:
50 
51 private:
52 
53  typename std::map<Identifier, LogicCreator> m_associations;
54 
55 };
56 #endif // LOGICFACTORY_H
Ilogic * CreateObject(const Identifier &id)
Definition: LogicFactory.h:39
std::map< Identifier, LogicCreator > m_associations
Definition: LogicFactory.h:53
#define NULL
Definition: scimark2.h:8
bool Register(const Identifier &id, LogicCreator creator)
Definition: LogicFactory.h:24
bool Unregister(const Identifier &id)
Definition: LogicFactory.h:29
Container::value_type value_type