Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

/home/pauldt/projects/IDEA/IDEA-ALL/IDEA-DEV/Core/Utilities/Includes/Factory.hh

Go to the documentation of this file.
00001 // -*- Mode: c++ -*-
00002 //     File: $Source: /home/cvs/ISG-Repository/IDEA-DEV/Core/Utilities/Includes/Factory.hh,v $
00003 //      Tag: $Name: DR_01_11_06 $
00004 //      CVS: $Id: Factory.hh,v 1.3 2005/12/30 22:06:24 rijsman Exp $
00005 //     Info: $CVSROOT/IDEA-DEV/COPYRIGHT
00006 
00007 #ifndef FACTORY_HEADER_DEFINITION
00008 #define FACTORY_HEADER_DEFINITION
00009 
00010 #include "Debugging.hh"
00011 #include "Trace.hh"
00012 #include "Utilities.hh"
00013 
00014 namespace IDEA
00015 {
00025   template< class Type >
00026   class Factory
00027   {
00028   private:
00029     typedef std::map< std::string, const Factory* > String2Type;
00030   public:
00035     Factory( const std::string& key ):
00036       m_Key( key )
00037     {
00038       registerFactory( this );
00039     }
00043     virtual ~Factory() 
00044     {
00045       unRegisterFactory( this );
00046     }
00050     const std::string& getKey() const
00051     {
00052       return m_Key;
00053     }
00057     static void useKey( const std::string& key )
00058     {
00059       // this will work, looks a little weird but see comments at getUseKey
00060       getUseKey() = key;
00061     }
00066     static const Type* instance() 
00067     {
00068       typename String2Type::const_iterator ite = getContainer().find( getUseKey() );
00069 
00070       if( ite != getContainer().end() )
00071         {
00072           ASSERT( 0 != dynamic_cast< const Type* >(ite->second) );
00073 
00074           // HERE we make a strong ASSUMPTION Type is a specialization of Factory
00075           return static_cast<const Type*>(ite->second);
00076         }
00077       else
00078         {
00079           ERROR_MSG("Requested to use factory instance '"
00080                     << getUseKey() << "' but no such instance has been registered for type '"
00081                     << typeid( Type ).name() << "'. Did you load the library which registers this factory?");
00082         }
00083 
00084       return 0;
00085     }
00086   private:
00090     Factory();
00094     Factory( const Factory& factory );
00101     static std::string& getUseKey()
00102     {
00103       static std::string useKey = "";
00104 
00105       return useKey;
00106     }
00113     static String2Type& getContainer() 
00114     {
00115       static String2Type* container = new String2Type();
00116 
00117       return *container;
00118     }
00122     static void registerFactory( const Factory* factory )
00123     {
00124       getContainer().insert( std::make_pair( factory->getKey(), factory ) );
00125     }
00129     static void unRegisterFactory( const Factory* factory )
00130     {
00131       typename String2Type::iterator ite = getContainer().find( factory->getKey() );
00132 
00133       if( ite != getContainer().end() )
00134         getContainer().erase( ite );
00135     }
00139     std::string m_Key;
00140   };
00141 }
00142 
00143 #endif // FACTORY_HEADER_DEFINITION

Contact information
© IDEA
Generated on Fri Feb 3 17:09:41 2006 for IDEA.