![]() |
TerraLib 4.1
|
00001 /************************************************************************************ 00002 TerraLib - a library for developing GIS applications. 00003 Copyright © 2001-2007 INPE and Tecgraf/PUC-Rio. 00004 00005 This code is part of the TerraLib library. 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2.1 of the License, or (at your option) any later version. 00010 00011 You should have received a copy of the GNU Lesser General Public 00012 License along with this library. 00013 00014 The authors reassure the license terms regarding the warranties. 00015 They specifically disclaim any warranties, including, but not limited to, 00016 the implied warranties of merchantability and fitness for a particular purpose. 00017 The library provided hereunder is on an "as is" basis, and the authors have no 00018 obligation to provide maintenance, support, updates, enhancements, or modifications. 00019 In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct, 00020 indirect, special, incidental, or consequential damages arising out of the use 00021 of this library and its documentation. 00022 *************************************************************************************/ 00026 #ifndef __TERRALIB_INTERNAL_FACTORY_H 00027 #define __TERRALIB_INTERNAL_FACTORY_H 00028 00029 #include <map> 00030 #include <string> 00031 00032 using namespace std; 00033 00035 /* 00036 The "factory" pattern is a technique for creating new 00037 instances of objects which defines an abstract interface, 00038 (represented by the "Make" module). The subclasses of 00039 factory decide which class to instantiate. 00040 00041 00042 Each subclass of factory "registers" itself at compile time; 00043 therefore, the addition of a new factory requires no change 00044 the parent class. 00045 00046 \author Gilberto Camara 00047 */ 00048 template <class T, class Arg> 00049 class TeFactory 00050 { 00051 public: 00052 00054 typedef map<string, TeFactory<T,Arg>* > TeFactoryMap; 00055 00057 static TeFactoryMap& instance () 00058 { 00059 static TeFactoryMap Fmap_; 00060 return Fmap_; 00061 00062 } 00063 00065 TeFactory (const string& factoryName); 00066 00067 virtual ~TeFactory() {} 00068 00070 static T* make ( string name, const Arg& arg ); 00071 00073 static T* make ( const Arg& arg ); 00074 00075 00076 protected: 00077 00079 virtual T* build ( const Arg& arg ) = 0; 00080 00081 private: 00082 string Fname_; 00083 }; 00084 00085 // Initialisation of static variable 00086 //template <class T, class Arg> 00087 //TeFactory<T,Arg>::TeFactoryMap TeFactory<T,Arg>::Fmap_; 00088 00089 00090 // Constructor 00091 template <class T, class Arg> 00092 TeFactory<T,Arg>::TeFactory(const string& name): 00093 Fname_(name) 00094 { 00095 TeFactory<T,Arg>::instance()[name] = this; 00096 00097 } 00098 00099 // Destructor 00100 //template <class T, class Arg> 00101 //TeFactory<T,Arg>::~TeFactory<T,Arg> () 00102 //{ 00103 // Remove the object from the factory dictionary 00104 // Fmap_.erase ( Fname_ ); 00105 //} 00106 00108 template <class T, class Arg> 00109 T* 00110 TeFactory<T,Arg>::make ( string name, const Arg& arg ) 00111 { 00112 // try to find the name on the factory dictionary 00113 typename TeFactoryMap::iterator i = TeFactory<T,Arg>::instance().find ( name ); 00114 00115 // Not found ? return the Default Object 00116 if ( i == TeFactory<T,Arg>::instance().end() ) 00117 return T::DefaultObject( arg ); 00118 00119 // Create an object, based on the input parameters 00120 return (*i).second->build ( arg ); 00121 return 0; 00122 00123 } 00124 00126 template <class T, class Arg> 00127 T* 00128 TeFactory<T,Arg>::make (const Arg& arg ) 00129 { 00130 string name = arg.decName(); 00131 00132 // try to find the name on the factory dictionary 00133 typename TeFactoryMap::iterator i = TeFactory<T,Arg>::instance().find ( name ); 00134 00135 // Not found ? return the Default Object 00136 if ( i == TeFactory<T,Arg>::instance().end() ) 00137 return T::DefaultObject( arg ); 00138 00139 // Create an object, based on the input parameters 00140 return (*i).second->build ( arg ); 00141 return 0; 00142 00143 } 00144 #endif 00145