37 #include <boost/function.hpp>
38 #include <boost/bind.hpp>
40 #include <feel/feelcore/feel.hpp>
41 #include <feel/feelcore/typeinfo.hpp>
49 typename IdentifierType,
59 Exception( IdentifierType
id )
64 M_ex = this->getEx(
id );
69 const char* what()
const throw ()
73 std::string getEx( std::string
const&
id )
75 std::ostringstream __ex_str;
76 __ex_str <<
"[Factory] Unknown Type : " << id;
77 return __ex_str.str();
80 std::string getEx( T
const&
id )
82 std::ostringstream __ex_str;
83 __ex_str <<
"[Factory] Unknown Type : ";
84 return __ex_str.str();
90 static AbstractProduct* onUnknownType( IdentifierType
id )
92 throw Exception(
id );
107 class AbstractProduct,
108 typename IdentifierType,
109 typename ProductCreator = boost::function<AbstractProduct*()>,
114 public FactoryErrorPolicy<IdentifierType,AbstractProduct>
122 typedef IdentifierType identifier_type;
123 typedef AbstractProduct product_type;
124 typedef ProductCreator creator_type;
126 typedef FactoryErrorPolicy<identifier_type,product_type> super;
150 DVLOG(2) <<
"Registered type with id : " <<
id <<
"\n";
151 return M_associations.insert(
typename id_to_product_type::value_type(
id, creator ) ).second;
163 DVLOG(2) <<
"Unregistered type with id : " <<
id <<
"\n";
164 return M_associations.erase(
id ) == 1;
177 typename id_to_product_type::const_iterator i = M_associations.find(
id );
179 if ( i != M_associations.end() )
181 DVLOG(2) <<
"Creating type with id : " <<
id <<
"\n";
182 return ( i->second )();
185 DVLOG(2) <<
"Unknown type with id : " <<
id <<
"\n";
186 return super::onUnknownType(
id );
193 typedef std::map<identifier_type, creator_type> id_to_product_type;
194 id_to_product_type M_associations;
208 class AbstractProduct,
209 class ProductCreator = boost::function<AbstractProduct* ( const AbstractProduct* )>,
210 template<
typename,
class>
class FactoryErrorPolicy = FactoryDefaultError
214 public FactoryErrorPolicy<TypeInfo, AbstractProduct>
223 typedef FactoryErrorPolicy<TypeInfo,AbstractProduct> super;
258 bool registerProduct(
const TypeInfo&
id, ProductCreator creator )
260 return M_associations.insert(
typename id_to_product_type::value_type(
id, creator ) ).second;
263 bool unregisterProduct(
const TypeInfo&
id )
265 return M_associations.erase(
id ) == 1;
268 AbstractProduct* createObject(
const AbstractProduct* model )
270 if ( model == 0 )
return 0;
272 typename id_to_product_type::const_iterator i = M_associations.find(
typeid( *model ) );
274 if ( i != M_associations.end() )
276 return ( i->second )( model );
279 return super::onUnknownType(
typeid( *model ) );
285 typedef std::map<TypeInfo, ProductCreator> id_to_product_type;
286 id_to_product_type M_associations;