這是上星期我改的某個程式片段:
template< typename ProductCreator >
class CreatorLoader {
public:
CreatorLoader( const std::string & plugin ) : plugin_( plugin ) {}
ProductCreator * operator()() {
plugin::PluginContext pc;
ProductCreator * c = qobject_cast< ProductCreator * >( pc.load( this->plugin_.c_str() ) );
if( !c ) {
throw Error< RunTime >( "Invalid plugin!" );
}
return c;
}
private:
std::string plugin_;
};
template< typename Product, typename CreatorFactory >
bool registerProduct( const std::string & key, const std::string & plugin ) {
return CreatorFactory::Instance().Register( key, CreatorLoader< Creator< Product > >( plugin ) );
}
template< typename Product, typename CreatorFactory >
std::tr1::shared_ptr< Product > createProduct( const std::string & key ) {
Creator< Product > * tmp = NULL;
try {
tmp = CreatorFactory::Instance().CreateObject( key );
} catch( std::exception & e ) {
tmp = CreatorFactory::Instance().CreateObject( "*" );
}
return tmp->create();
}
現在看就需要點思考時間 ... = =,還是補點註解好了。
沒有留言:
張貼留言