OBTPrototypeFactory.h

Go to the documentation of this file.
00001 #ifndef OBT_PROTOTYPE_FACTORY_H
00002 #define OBT_PROTOTYPE_FACTORY_H
00003 
00004 #include <utility>
00005 #include <map>
00006 
00007 #include "OBTSingleton.h"
00008 
00009 namespace OBT
00010 {
00068 template< class TIdentifier, class TCreator > 
00069 class PrototypeFactory
00070 {
00071 public:
00072 
00073         friend class Singleton< PrototypeFactory< TIdentifier, TCreator > > ;
00074 
00076         virtual ~PrototypeFactory() ;
00077 
00082         const TCreator& create( const TIdentifier& classId ) const ;
00083 
00085         template < class TDerivedCreator > bool registerCreator( const TIdentifier& classId ) ;
00086 
00087 private:
00088 
00090         PrototypeFactory() ;
00091 
00093         PrototypeFactory( const PrototypeFactory& ) ;
00094 
00096         PrototypeFactory& operator=( const PrototypeFactory& ) ;
00097 
00099         std::map< TIdentifier, TCreator* > _creators ;
00100 } ;
00101 
00102 //-------------------------------------------------------------------------
00103 // constructor
00104 //-------------------------------------------------------------------------
00105 template< class TIdentifier, class TCreator >
00106 PrototypeFactory< TIdentifier, TCreator >::PrototypeFactory()
00107 {
00108 }
00109 
00110 //-------------------------------------------------------------------------
00111 // destructor
00112 //-------------------------------------------------------------------------
00113 template< class TIdentifier, class TCreator >
00114 PrototypeFactory< TIdentifier, TCreator >::~PrototypeFactory()
00115 {
00116 }
00117 
00118 //-------------------------------------------------------------------------
00119 // create
00120 //-------------------------------------------------------------------------
00121 template< class TIdentifier, class TCreator >
00122 const TCreator&
00123         PrototypeFactory< TIdentifier, TCreator >::create( const TIdentifier& classId ) const
00124 {
00125         typename std::map< TIdentifier, TCreator* >::const_iterator ite( _creators.find( classId ) ) ;
00126         if( ite != _creators.end() )
00127         {
00128                 return *ite->second ;
00129         }
00130 
00131         static TCreator creator ;
00132         return creator ;
00133 }
00134 
00135 //-------------------------------------------------------------------------
00136 // registerCreator
00137 //-------------------------------------------------------------------------
00138 template< class TIdentifier, class TCreator > 
00139 template< class TDerivedCreator >
00140 bool
00141         PrototypeFactory< TIdentifier, TCreator >::registerCreator( const TIdentifier& classId )
00142 {
00143         static TDerivedCreator creator ;
00144         _creators[classId] = &creator ;
00145         return true ;
00146 }
00147 } // namespace OBT
00148 
00149 #endif //OBT_PROTOTYPE_FACTORY_H

Generated on Wed Oct 1 11:34:05 2008 for OBT by  doxygen 1.5.3