00001
00002
00003 #ifndef DLMANAGER_H
00004 #define DLMANAGER_H
00005
00006
00007
00008 #ifdef HAVE_CONFIG_H
00009 #include "config.h"
00010 #endif
00011
00012 #include "path.h"
00013 #include <map>
00014 #include <vector>
00015 #include <string>
00016 #include <stdio.h>
00017
00018 #include <errno.h>
00019 #include "Exception.h"
00020
00021
00022
00023
00024
00025 #ifdef HAVE_DLFCN_H
00026 #include <dlfcn.h>
00027
00028 namespace FD {
00029
00031 typedef void *DL_HANDLE_TYPE;
00032
00034 inline DL_HANDLE_TYPE _DL_OPEN(std::string path, int debug = 1)
00035 {
00036
00037 DL_HANDLE_TYPE library = dlopen (path.c_str(), RTLD_LAZY|RTLD_GLOBAL);
00038 if (!library && debug)
00039 std::cerr << "Toolbox load error: " << dlerror() << std::endl;
00040
00041 return library;
00042 }
00043
00045 inline void * _DL_GET_SYM(DL_HANDLE_TYPE lib, const std::string &symbol)
00046 {
00047 return dlsym (lib, symbol.c_str());
00048 }
00049
00051 inline void _DL_CLOSE(DL_HANDLE_TYPE lib)
00052 {
00053 dlclose(lib);
00054 }
00055
00056 #elif defined (HAVE_DL_H)
00057
00058 #include <dl.h>
00059
00060 typedef shl_t DL_HANDLE_TYPE;
00061 inline DL_HANDLE_TYPE _DL_OPEN(string path, int debug = 0)
00062 {
00063
00064 DL_HANDLE_TYPE library = shl_load (path.c_str(), BIND_IMMEDIATE, 0);
00065
00066 if (!library && debug)
00067 perror ("Load error");
00068 return library;
00069 }
00070 inline void * _DL_GET_SYM(DL_HANDLE_TYPE lib, std::string symbol)
00071 {
00072 void *tmp;
00073 shl_findsym (&lib, symbol.c_str(), TYPE_PROCEDURE, &tmp);
00074 return tmp;
00075 }
00076 inline void _DL_CLOSE(DL_HANDLE_TYPE lib)
00077 {
00078 shl_unload (lib);
00079 }
00080
00081
00082 #endif
00083
00084
00086 class LoadedLibrary {
00087
00089 DL_HANDLE_TYPE lib;
00090
00092 int count;
00093
00094 public:
00096 LoadedLibrary(const std::string &path)
00097 : lib(_DL_OPEN(path))
00098 , count(1)
00099 {if (!lib) throw new GeneralException(std::string("couldn't load library ")+path,__FILE__,__LINE__);}
00100
00102 void *get_proc (std::string symbol)
00103 {return _DL_GET_SYM(lib,symbol);}
00104
00106 ~LoadedLibrary()
00107 {
00108
00109
00110 }
00111 };
00112
00113
00114
00116 class DLManager {
00117
00119 static std::map<std::string,LoadedLibrary* > loaded;
00120
00121 public:
00122
00125 static LoadedLibrary *getLib(const std::string &name);
00126
00127
00128 };
00129
00130 class ToolboxData {
00131 std::string fullname;
00132 DL_HANDLE_TYPE handle;
00133 public:
00134 ToolboxData() {}
00135 ToolboxData(std::string _fullname, DL_HANDLE_TYPE _handle)
00136 : fullname(_fullname)
00137 , handle(_handle)
00138 {}
00139 };
00140
00141 class ToolboxList {
00142 static std::map<std::string, ToolboxData> loadedToolboxes;
00143 public:
00144 static std::vector<std::string> load(const std::vector<std::string> &list, int debug);
00145 };
00146
00147 }
00148
00149 #endif