library.cpp
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
00035 #include "gpucalc/library.h"
00036 #include "gpucalc/log_manager.h"
00037
00038
00039 #if (PLATFORM == PLATFORM_LINUX)
00040 #include <dlfcn.h>
00041 #endif
00042
00043 #if (PLATFORM == PLATFORM_WIN32)
00044 #define WIN32_LEAN_AND_MEAN
00045 #include <windows.h>
00046 #endif
00047
00048 #if (PLATFORM == PLATFORM_APPLE)
00049 #include "macPlugins.h"
00050 #endif
00051
00052
00053
00054
00055 namespace gpucalc
00056 {
00057 void Library::load()
00058 {
00059 std::string name = this->getObjectName();
00060 # if PLATFORM == PLATFORM_LINUX
00061 if (name.substr(name.length() - 3, 3) != ".so")
00062 name += ".so";
00063 # endif
00064
00065 mhInst = (LIBRARY_HANDLE)LIBRARY_LOAD(name.c_str());
00066 if (!mhInst)
00067 {
00068 Except<ERR_INTERNAL_ERROR>(this, "Could not load dynamic library \"" + name + "\". System Error: " + libraryError(),
00069 "DynLib::load", __FILE__, __LINE__);
00070 }
00071
00072 LogManager::getSingleton().logMessage(this, "Library loaded.", LML_Trivial);
00073 mIsLoaded = true;
00074 }
00075
00076
00077 void Library::unload()
00078 {
00079 if (LIBRARY_UNLOAD(mhInst))
00080 {
00081 Except<ERR_INTERNAL_ERROR>(this, "Could not unload dynamic library \"" + this->getObjectName() + "\". System Error: " + libraryError(),
00082 "DynLib::unload", __FILE__, __LINE__);
00083 }
00084 LogManager::getSingleton().logMessage(this, "Library unloaded.", LML_Trivial);
00085 mIsLoaded = false;
00086 }
00087
00088 LIBRARY_HANDLE Library::getSymbol(const std::string & SymbolName) const
00089 {
00090 LogManager::getSingleton().logMessage(this, "Exporting symbol \"" + SymbolName + "\"...", LML_Trivial);
00091 return (LIBRARY_HANDLE)LIBRARY_GETSYM(mhInst, SymbolName.c_str());
00092 }
00093
00094
00095 std::string Library::libraryError() const
00096 {
00097 #if PLATFORM == PLATFORM_WIN32
00098 LPVOID lpMsgBuf;
00099 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
00100 0, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, 0);
00101 std::string ret = (char*)lpMsgBuf;
00102
00103 LocalFree( lpMsgBuf );
00104 return ret;
00105 #elif OGRE_PLATFORM == OGRE_PLATFORM_LINUX
00106 return std::string(dlerror());
00107 #elif OGRE_PLATFORM == OGRE_PLATFORM_APPLE
00108 return std::string(mac_errorBundle());
00109 #else
00110 return std::string("");
00111 #endif
00112 }
00113 }