1. // To register a component, place on global scope
  2. #define REGISTER_CREATOR( NAME ) \
  3.   Component *NAME##Create( void )\
  4.   {                              \
  5.     return new NAME( );          \
  6.   }                              \
  7.   NAME##Creator( #NAME, NAME##Create )
  8.  
  9. struct Creator
  10. {
  11.   Creator( const char *name, Component *(*fn)( void ) )
  12.     : Create( fn )
  13.   {
  14.     // Assume FACTORY is a global singleton or pointer
  15.     FACTORY->Register( name, this );
  16.   }
  17.  
  18.   Component *(*Create)( void );
  19. }
  20.  
  21. class Factory
  22. {
  23. public:
  24.   Component *CreateComponent( const std::string& name );
  25.   void Register( const char *name, Creator creator );
  26.  
  27. private:
  28.   std::map<std::string, Creator *> m_map;
  29. };
  30.  
  31. Component *Factory::CreateComponent( const std::string& name )
  32. {
  33.   return m_map[name]->Create( );
  34. }
  35.  
  36. void Factory::Register( const char *name, Creator creator )
  37. {
  38.   m_map[std::string( name )] = creator;
  39. }