BESPlugin.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef T_BESPlugin_h
00036 #define T_BESPlugin_h
00037
00038 #include <dlfcn.h>
00039 #include <string>
00040 #include <iostream>
00041
00042 #include "BESObj.h"
00043 #include "BESInternalFatalError.h"
00044 #include "BESInternalError.h"
00045
00046 using std::string;
00047 using std::cerr;
00048 using std::endl;
00049
00053 class NoSuchLibrary : public BESInternalFatalError
00054 {
00055 public:
00056 NoSuchLibrary( const string &msg, const string &file, int line )
00057 : BESInternalFatalError( msg, file, line ) {}
00058 };
00059
00063 class NoSuchObject : public BESInternalFatalError
00064 {
00065 public:
00066 NoSuchObject( const string &msg, const string &file, int line )
00067 : BESInternalFatalError( msg, file, line ) {}
00068 };
00069
00090 template<typename M>
00091 class BESPlugin : public BESObj
00092 {
00093 private:
00094 string d_filename;
00095 void *d_lib;
00096
00099 BESPlugin() throw(BESInternalError)
00100 {
00101 throw BESInternalError( "Unimplemented method", __FILE__, __LINE__ );
00102 }
00103
00108 BESPlugin(const BESPlugin &p) throw(BESInternalError)
00109 {
00110 throw BESInternalError( "Unimplemented method.", __FILE__, __LINE__ );
00111 }
00112
00116 BESPlugin &operator=(const BESPlugin &p) throw(BESInternalError)
00117 {
00118 throw BESInternalError( "Unimplemented method.", __FILE__, __LINE__ );
00119 }
00120
00121 void *get_lib() throw(NoSuchLibrary) {
00122 if (!d_lib) {
00123 d_lib = dlopen(d_filename.c_str(), RTLD_NOW|RTLD_LOCAL);
00124 if (d_lib == NULL) {
00125 throw NoSuchLibrary( string( dlerror() ), __FILE__, __LINE__ ) ;
00126 }
00127 }
00128
00129 return d_lib;
00130 }
00131
00132 public:
00137 BESPlugin(const string &filename) : d_filename(filename), d_lib(0) {}
00138
00141 virtual ~BESPlugin() {
00142 if (d_lib)
00143 dlclose(d_lib);
00144 }
00145
00152 M* instantiate() throw(NoSuchLibrary, NoSuchObject) {
00153 void *maker = dlsym(get_lib(), "maker");
00154 if (!maker) {
00155 throw NoSuchObject( string( dlerror() ), __FILE__, __LINE__ ) ;
00156 }
00157
00158 typedef M *(*maker_func_ptr)();
00159 maker_func_ptr my_maker = *reinterpret_cast<maker_func_ptr*>(&maker);
00160 M *my_M = (my_maker)();
00161
00162 return my_M;
00163 }
00164
00165 virtual void dump( ostream &strm ) const
00166 {
00167 strm << "BESPlugin::dump - (" << (void *)this << ")" << endl ;
00168 strm << " plugin name: " << d_filename << endl ;
00169 strm << " library handle: " << (void *)d_lib << endl ;
00170 }
00171 };
00172
00173 #endif // T_BESPlugin_h
00174