BESPlugin.h

Go to the documentation of this file.
00001 
00002 // -*- C++ -*-
00003 
00004 // (c) COPYRIGHT DAS, llc. 2001
00005 //
00006 // Author: James Gallagher <jgallagher@gso.uri.edu>
00007 
00008 #ifndef T_BESPlugin_h
00009 #define T_BESPlugin_h
00010 
00011 #include <dlfcn.h>
00012 #include <string>
00013 #include <iostream>
00014 
00015 #include "BESObj.h"
00016 #include "BESPluginException.h"
00017 
00018 using std::string;
00019 using std::cerr;
00020 using std::endl;
00021 
00025 class NoSuchLibrary : public BESPluginException
00026 {
00027 public:
00028     NoSuchLibrary( const string &msg, const string &file, int line )
00029         : BESPluginException( msg, file, line ) {}
00030 };
00031 
00035 class NoSuchObject : public BESPluginException
00036 {
00037 public:
00038     NoSuchObject( const string &msg, const string &file, int line )
00039         : BESPluginException( msg, file, line ) {}
00040 };
00041 
00062 template<typename M>
00063 class BESPlugin : public BESObj
00064 {
00065 private:
00066     string d_filename;          // Library filename
00067     void *d_lib;                // Open library handle
00068 
00071     BESPlugin()  throw(BESPluginException)
00072     {   
00073         throw BESPluginException( "Unimplemented method", __FILE__, __LINE__ );
00074     }
00075 
00080     BESPlugin(const BESPlugin &p) throw(BESPluginException)
00081     {
00082         throw BESPluginException( "Unimplemented method.", __FILE__, __LINE__ );
00083     }
00084 
00088     BESPlugin &operator=(const BESPlugin &p) throw(BESPluginException)
00089     {
00090         throw BESPluginException( "Unimplemented method.", __FILE__, __LINE__ );
00091     }
00092 
00093     void *get_lib() throw(NoSuchLibrary) {
00094         if (!d_lib) {
00095             d_lib = dlopen(d_filename.c_str(), RTLD_NOW|RTLD_LOCAL);
00096             if (d_lib == NULL) {
00097                 throw NoSuchLibrary( string( dlerror() ), __FILE__, __LINE__ ) ;
00098             }
00099         }
00100 
00101         return d_lib;
00102     }
00103 
00104 public:
00109     BESPlugin(const string &filename) : d_filename(filename), d_lib(0) {}
00110 
00113     virtual ~BESPlugin() {
00114         if (d_lib)
00115             dlclose(d_lib);
00116     }
00117 
00124     M* instantiate() throw(NoSuchLibrary, NoSuchObject) {
00125         void *maker = dlsym(get_lib(), "maker");
00126         if (!maker) {
00127             throw NoSuchObject( string( dlerror() ), __FILE__, __LINE__ ) ;
00128         }
00129     
00130         typedef M *(*maker_func_ptr)();
00131         maker_func_ptr my_maker = *reinterpret_cast<maker_func_ptr*>(&maker);
00132         M *my_M = (my_maker)();
00133 
00134         return my_M;
00135     }
00136 
00137     virtual void dump( ostream &strm ) const
00138     {
00139         strm << "BESPlugin::dump - (" << (void *)this << ")" << endl ;
00140         strm << "    plugin name: " << d_filename << endl ;
00141         strm << "    library handle: " << (void *)d_lib << endl ;
00142     }
00143 };
00144 
00145 #endif // T_BESPlugin_h
00146 

Generated on Wed Jan 2 06:01:18 2008 for OPeNDAP Back End Server (BES) by  doxygen 1.5.4