library.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Kutumov Alexey                                  *
00003  *   ru.pixel@gmail.com                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
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         // Free the buffer.
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 }

Generated on Thu Mar 5 22:36:42 2009 for gpucalc by  doxygen 1.5.6