00001 #ifndef s11n_cl_S11N_CLASSLOAD_HPP_INCLUDED
00002 #define s11n_cl_S11N_CLASSLOAD_HPP_INCLUDED 1
00003
00004 #include <s11n.net/s11n/s11n_debuggering_macros.hpp>
00005 #include <s11n.net/s11n/factory.hpp>
00006 #include <s11n.net/s11n/exception.hpp>
00007 #include <s11n.net/s11n/type_traits.hpp>
00008
00009 #include <s11n.net/s11n/s11n_config.hpp>
00010 #if s11n_CONFIG_ENABLE_PLUGINS
00011 # include <s11n.net/s11n/plugin/plugin.hpp>
00012 #endif
00013
00014 namespace s11n {
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 namespace cl {
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 template <typename InterfaceBase>
00058 struct S11N_EXPORT_API object_factory
00059 {
00060
00061 typedef InterfaceBase base_type;
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091 base_type * operator()( const std::string & key ) const
00092 {
00093 try
00094 {
00095 typedef ::s11n::fac::factory_mgr<base_type> FacMgr;
00096 FacMgr & fm = FacMgr::instance();
00097 #if s11n_CONFIG_ENABLE_PLUGINS
00098
00099 if( ! fm.provides( key ) )
00100 {
00101 using namespace ::s11n::debug;
00102 S11N_TRACE(TRACE_FACTORY_PLUGINS) << "Trying to find plugin for " << key << "...\n";
00103 std::string where = ::s11n::plugin::open( key );
00104 if( where.empty() )
00105 {
00106 S11N_TRACE(TRACE_FACTORY_PLUGINS) << "Plugin load failed for '"<<key<<"': " << ::s11n::plugin::dll_error() << "\n";
00107 return 0;
00108 }
00109 }
00110 #endif // s11n_CONFIG_ENABLE_PLUGINS
00111 return fm.create( key );
00112 }
00113 catch( const ::s11n::s11n_exception & )
00114 {
00115 throw;
00116 }
00117 catch( const std::exception & ex )
00118 {
00119 throw ::s11n::factory_exception( ex.what() );
00120 }
00121 catch(...)
00122 {
00123 throw ::s11n::factory_exception( "Unknown exception: Factory load failed for class '"+key+"'.", __FILE__, __LINE__ );
00124 }
00125 return 0;
00126 }
00127 };
00128
00129
00130
00131
00132
00133 template <typename InterfaceBase>
00134 InterfaceBase * classload( const std::string key );
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 template <typename InterfaceBase>
00145 void classloader_register( const std::string & classname, InterfaceBase * (*factory_func)() );
00146
00147
00148
00149
00150
00151
00152
00153 template <typename InterfaceBase, typename SubType>
00154 inline void classloader_register_subtype( const std::string & classname )
00155 {
00156 classloader_register<InterfaceBase>( classname,
00157 ::s11n::fac::create_hook<InterfaceBase,SubType>::create
00158 );
00159 }
00160
00161
00162
00163
00164
00165
00166
00167 template <typename InterfaceBase>
00168 inline void classloader_register_base( const std::string & classname )
00169 {
00170 classloader_register_subtype<InterfaceBase,InterfaceBase>( classname );
00171 }
00172
00173
00174 namespace Detail
00175 {
00176
00177 template <typename T>
00178 inline T * noop_factory() { return 0; }
00179 }
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 template <typename InterfaceBase>
00196 inline void classloader_register_abstract( const std::string & classname )
00197 {
00198 classloader_register<InterfaceBase>( classname, Detail::noop_factory<InterfaceBase> );
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 template <typename InterfaceBase>
00216 inline void classloader_alias( const std::string & _alias,
00217 const std::string & classname)
00218 {
00219 ::s11n::fac::factory<InterfaceBase>().aliases().alias( _alias, classname );
00220 }
00221
00222
00223 }
00224 }
00225
00226 #include <s11n.net/s11n/classload.tpp>
00227 #endif // s11n_cl_S11N_CLASSLOAD_HPP_INCLUDED