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
00026 #ifdef HAVE_DLFCN_H
00027 #include <dlfcn.h>
00028
00030 typedef void *DL_HANDLE_TYPE;
00031
00033 inline DL_HANDLE_TYPE _DL_OPEN(std::string path, int debug = 1)
00034 {
00035
00036 DL_HANDLE_TYPE library = dlopen (path.c_str(), RTLD_LAZY|RTLD_GLOBAL);
00037 if (!library && debug)
00038 std::cerr << "Toolbox load error: " << dlerror() << std::endl;
00039
00040 return library;
00041 }
00042
00044 inline void * _DL_GET_SYM(DL_HANDLE_TYPE lib, const std::string &symbol)
00045 {
00046 return dlsym (lib, symbol.c_str());
00047 }
00048
00050 inline void _DL_CLOSE(DL_HANDLE_TYPE lib)
00051 {
00052 dlclose(lib);
00053 }
00054
00055 #elif defined (HAVE_DL_H)
00056
00057 #include <dl.h>
00058
00059 typedef shl_t DL_HANDLE_TYPE;
00060 inline DL_HANDLE_TYPE _DL_OPEN(string path, int debug = 0)
00061 {
00062
00063 DL_HANDLE_TYPE library = shl_load (path.c_str(), BIND_IMMEDIATE, 0);
00064
00065 if (!library && debug)
00066 perror ("Load error");
00067 return library;
00068 }
00069 inline void * _DL_GET_SYM(DL_HANDLE_TYPE lib, std::string symbol)
00070 {
00071 void *tmp;
00072 shl_findsym (&lib, symbol.c_str(), TYPE_PROCEDURE, &tmp);
00073 return tmp;
00074 }
00075 inline void _DL_CLOSE(DL_HANDLE_TYPE lib)
00076 {
00077 shl_unload (lib);
00078 }
00079
00080
00081 #endif
00082
00083
00085 class LoadedLibrary {
00086
00088 DL_HANDLE_TYPE lib;
00089
00091 int count;
00092
00093 public:
00095 LoadedLibrary(const std::string &path)
00096 : lib(_DL_OPEN(path))
00097 , count(1)
00098 {if (!lib) throw new GeneralException(std::string("couldn't load library ")+path,__FILE__,__LINE__);}
00099
00101 void *get_proc (std::string symbol)
00102 {return _DL_GET_SYM(lib,symbol);}
00103
00105 ~LoadedLibrary()
00106 {
00107
00108
00109 }
00110 };
00111
00112
00113
00115 class DLManager {
00116
00118 static std::map<std::string,LoadedLibrary* > loaded;
00119
00120 public:
00121
00124 static LoadedLibrary *getLib(const std::string &name);
00125
00126
00127 };
00128
00129 class ToolboxData {
00130 std::string fullname;
00131 DL_HANDLE_TYPE handle;
00132 public:
00133 ToolboxData() {}
00134 ToolboxData(std::string _fullname, DL_HANDLE_TYPE _handle)
00135 : fullname(_fullname)
00136 , handle(_handle)
00137 {}
00138 };
00139
00140 class ToolboxList {
00141 static std::map<std::string, ToolboxData> loadedToolboxes;
00142 public:
00143 static std::vector<std::string> load(const std::vector<std::string> &list, int debug);
00144 };
00145
00146
00147
00148 #endif
00149