00001 #ifndef Factory_H 00002 #define Factory_H 00003 00004 #include <map> 00005 #include <string> 00006 00007 using std::string; 00008 using std::map; 00009 00011 /* 00012 The "factory" pattern is a technique for creating new 00013 instances of objects which defines an abstract interface, 00014 (represented by the "Make" module). The subclasses of 00015 factory decide which class to instantiate. 00016 00017 00018 Each subclass of factory "registers" itself at compile time; 00019 therefore, the addition of a new factory requires no change 00020 the parent class. 00021 00022 \author Gilberto Camara 00023 */ 00024 template <class T, class Arg> 00025 class Factory 00026 { 00027 public: 00028 00030 typedef map<string, Factory<T,Arg>* > FactoryMap; 00031 00033 static FactoryMap& instance () 00034 { 00035 static FactoryMap Fmap_; 00036 return Fmap_; 00037 00038 } 00039 00041 Factory (const string& name):Fname_(name) 00042 { 00043 Factory<T,Arg>::instance()[name] = this; 00044 }; 00045 00046 00047 virtual ~Factory() 00048 { 00049 // Remove the object from the factory dictionary 00050 // Fmap_.erase ( Fname_ ); 00051 }; 00052 00053 00055 static T* make ( string name, const Arg& arg ) 00056 { 00057 // try to find the name on the factory dictionary 00058 typename FactoryMap::iterator i = Factory<T,Arg>::instance().find ( name ); 00059 00060 // Not found ? return the Default Object 00061 //if ( i == Factory<T,Arg>::instance().end() ) 00062 // return T::DefaultObject( arg ); 00063 00064 // Create an object, based on the input parameters 00065 return (*i).second->build ( arg ); 00066 return 0; 00067 }; 00068 00070 static T* make ( Arg arg ) 00071 { 00072 string name = arg.decName(); 00073 00074 // try to find the name on the factory dictionary 00075 typename FactoryMap::iterator i = Factory<T,Arg>::instance().find ( name ); 00076 00077 // Not found ? return the Default Object 00078 if ( i == Factory<T,Arg>::instance().end() ) 00079 return T::DefaultObject( arg ); 00080 00081 // Create an object, based on the input parameters 00082 return (*i).second->build ( arg ); 00083 return 0; 00084 }; 00085 00086 00087 protected: 00088 00090 virtual T* build ( const Arg& arg ) = 0; 00091 00092 private: 00093 string Fname_; 00094 }; 00095 00096 // Initialisation of static variable 00097 //template <class T, class Arg> 00098 //Factory<T,Arg>::FactoryMap Factory<T,Arg>::Fmap_; 00099 00100 00101 /* 00102 // Constructor 00103 template <class T, class Arg> 00104 Factory<T,Arg>::Factory<T,Arg>(const string& name): 00105 Fname_(name) 00106 { 00107 Factory<T,Arg>::instance()[name] = this; 00108 00109 }; 00110 00111 // Destructor 00112 template <class T, class Arg> 00113 Factory<T,Arg>::~Factory<T,Arg> () 00114 { 00115 // Remove the object from the factory dictionary 00116 // Fmap_.erase ( Fname_ ); 00117 }; 00118 00119 // Virtual Constructor 00120 /* 00121 template <class T, class Arg> 00122 T* 00123 Factory<T,Arg>::make ( string name, const Arg& arg ) 00124 { 00125 // try to find the name on the factory dictionary 00126 typename FactoryMap::iterator i = Factory<T,Arg>::instance().find ( name ); 00127 00128 // Not found ? return the Default Object 00129 if ( i == Factory<T,Arg>::instance().end() ) 00130 return T::DefaultObject( arg ); 00131 00132 // Create an object, based on the input parameters 00133 return (*i).second->build ( arg ); 00134 return 0; 00135 00136 }; 00137 00138 00139 template <class T, class Arg> 00140 T* 00141 Factory<T,Arg>::make ( Arg arg ) 00142 { 00143 string name = arg.decName(); 00144 00145 // try to find the name on the factory dictionary 00146 typename FactoryMap::iterator i = Factory<T,Arg>::instance().find ( name ); 00147 00148 // Not found ? return the Default Object 00149 if ( i == Factory<T,Arg>::instance().end() ) 00150 return T::DefaultObject( arg ); 00151 00152 // Create an object, based on the input parameters 00153 return (*i).second->build ( arg ); 00154 return 0; 00155 00156 }; 00157 */ 00158 00159 #endif