00001
00002
00003
00004
00005
00006
00007 #include "LibFactory.h"
00008
00009 using namespace libfactory;
00010 using namespace std;
00011
00012 bool LibFactory::Register(string const & LibID, string const & Library,
00013 string const & Constructor, string const & Destructor){
00014
00015 IdPairToLib pairAux(LibID, libtriple::make_triple(Library,Constructor,Destructor));
00016
00017 return libassociations_.insert(pairAux).second;
00018 }
00019
00020 bool LibFactory::Unregister(string const & LibID){
00021 return libassociations_.erase(LibID) == 1;
00022 }
00023
00024 vector<string> LibFactory::List() const {
00025 vector<string> result;
00026
00027 for (IdToLib::const_iterator it = libassociations_.begin();
00028 it != libassociations_.end(); ++it){
00029 result.push_back(it->first);
00030 }
00031
00032 return result;
00033 }
00034
00035 void* LibFactory::LibCreate(string const & LibID, LcgInfo::create_Querier** Creator,
00036 LcgInfo::destroy_Querier** Destructor){
00037 void *Lib_handle;
00038 IdToLib::iterator it=libassociations_.find(LibID);
00039 if (it == libassociations_.end()) {
00040 cerr << "Library with given ID: " << LibID << "not found."<<endl;
00041 return NULL;
00042 }
00043 Lib_handle = dlopen (it->second.first.c_str(), RTLD_NOW|RTLD_GLOBAL);
00044 if (Lib_handle == NULL) {
00045 cerr << "Cannot load dynamically loadable library" << it->second.first << endl;
00046 cerr << "dlerror returns: " << dlerror() << endl;
00047 return NULL;
00048 }
00049 LcgInfo::create_Querier* create_dm =
00050 (LcgInfo::create_Querier*) dlsym(Lib_handle,it->second.second.c_str());
00051 LcgInfo::destroy_Querier* destroy_dm =
00052 (LcgInfo::destroy_Querier*) dlsym(Lib_handle,it->second.third.c_str());
00053
00054 if (!create_dm || !destroy_dm) {
00055 cerr << "Cannot load library constructor/distructor symbols" << endl;
00056 cerr << "dlerror returns: " << dlerror() << endl;
00057 return NULL;
00058 }
00059
00060 *Creator = create_dm;
00061 *Destructor = destroy_dm;
00062
00063 return Lib_handle;
00064 }