$search
00001 /* -*- C++ -*- 00002 * $Id: factory.hh 957 2005-03-07 16:01:20Z sjoyeux $ 00003 */ 00004 #ifndef UTILMM_TYPES_FACTORY_HEADER 00005 # define UTILMM_TYPES_FACTORY_HEADER 00006 #include "utilmm/config/config.h" 00007 00008 #include "boost/utility.hpp" 00009 00010 #include "utilmm/singleton/wrapper_fwd.hh" 00011 #include "utilmm/functional/arg_traits.hh" 00012 00013 #include "utilmm/types/factory_fwd.hh" 00014 #include "utilmm/types/bits/factory_error.hh" 00015 00016 namespace utilmm { 00017 00042 template< class AbstractProduct, typename IdentifierType, typename Result, 00043 typename ProductCreator, 00044 template<typename, class, typename> class FactoryErrorPolicy, 00045 class OrderId > 00046 class factory 00047 :public FactoryErrorPolicy<IdentifierType, AbstractProduct, Result>, 00048 public boost::noncopyable { 00049 private: 00050 typedef typename arg_traits<IdentifierType>::type id_param; 00051 typedef typename arg_traits<ProductCreator>::type creator_param; 00052 00053 typedef std::map<IdentifierType, ProductCreator, OrderId> factory_db; 00054 00055 factory_db associations; 00056 00057 public: 00070 bool add(id_param id, creator_param creator) { 00071 typename factory_db::value_type to_add(id, creator); 00072 00073 return associations.insert(to_add).second; 00074 } 00084 bool remove(id_param id) { 00085 return associations.erase(id)==1; 00086 } 00087 00100 bool make_alias(id_param from, id_param to) { 00101 typename factory_db::iterator i = associations.find(to); 00102 00103 return associations.end()==i || add(from, i->second); 00104 } 00105 00116 Result create(id_param id) { 00117 typename factory_db::iterator i = associations.find(id); 00118 00119 if( associations.end()==i ) 00120 return this->on_unknown_id(id); 00121 else 00122 return (i->second)(); 00123 } 00124 00133 bool check_entry(id_param id) const { 00134 return associations.find(id)!=associations.end(); 00135 } 00136 00148 ProductCreator const &get_creator(id_param id) const { 00149 typename factory_db::const_iterator i = associations.find(id); 00150 00151 if( associations.end()==i ) 00152 return this->on_unknown_type(id); 00153 else 00154 return i->second; 00155 } 00156 00157 private: 00158 factory() {} 00159 ~factory() {} 00160 00161 template<class Ty> 00162 friend class singleton::wrapper; 00163 }; // class utilmm::factory<> 00164 00165 } // namespace utilmm 00166 00167 # define IN_UTILMM_TYPES_FACTORY_HEADER 00168 #include "utilmm/types/bits/factory.tcc" 00169 # undef IN_UTILMM_TYPES_FACTORY_HEADER 00170 #endif // UTILMM_TYPES_FACTORY_HEADER 00171