dl_wrapper.cpp
Go to the documentation of this file.
00001 // g2o - General Graph Optimization
00002 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
00003 //
00004 // g2o is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU Lesser General Public License as published
00006 // by the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // g2o is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include <sys/types.h>
00018 
00019 #include <cstdio>
00020 #include <iostream>
00021 #include <algorithm>
00022 
00023 #include "dl_wrapper.h"
00024 #include "g2o/stuff/filesys_tools.h"
00025 
00026 #if defined (UNIX) || defined(CYGWIN)
00027 #include <dlfcn.h>
00028 #endif
00029 
00030 #ifdef __APPLE__
00031 #define SO_EXT "dylib"
00032 #define SO_EXT_LEN 5
00033 #elif defined (WINDOWS) || defined (CYGWIN)
00034 #define SO_EXT "dll"
00035 #define SO_EXT_LEN 3
00036 #else // Linux
00037 #define SO_EXT "so"
00038 #define SO_EXT_LEN 2
00039 #endif
00040 
00041 using namespace std;
00042 
00043 namespace g2o {
00044 
00045 DlWrapper::DlWrapper()
00046 {
00047 }
00048 
00049 DlWrapper::~DlWrapper()
00050 {
00051   clear();
00052 }
00053 
00054 int DlWrapper::openLibraries(const std::string& directory, const std::string& pattern)
00055 {
00056   cerr << "# loading libraries from " << directory << "\t pattern: " << pattern << endl;
00057   string searchPattern = directory + "/" + pattern;
00058   if (pattern == "")
00059     searchPattern = directory + "/*";
00060   vector<string> matchingFiles = getFilesByPattern(searchPattern.c_str());
00061 
00062   int numLibs = 0;
00063   for (size_t i = 0; i < matchingFiles.size(); ++i) {
00064     const string& filename = matchingFiles[i];
00065     if (find(_filenames.begin(), _filenames.end(), filename) != _filenames.end())
00066       continue;
00067 
00068     // If we are doing a release build, the wildcards will pick up the
00069     // suffixes; unfortunately the "_rd" extension means that we
00070     // don't seem to be able to filter out the incompatible files using a
00071     // wildcard expansion to wordexp.
00072 
00073 #ifndef G2O_LIBRARY_POSTFIX
00074     if ((filename.rfind(string("_d.") + SO_EXT) == filename.length() - 3 - SO_EXT_LEN)
00075         || (filename.rfind(string("_rd.") + SO_EXT) == filename.length() - 4 - SO_EXT_LEN)
00076         || (filename.rfind(string("_s.") + SO_EXT) == filename.length() - 3 - SO_EXT_LEN))
00077         continue;
00078 #endif
00079 
00080     // open the lib
00081     //cerr << "loading " << filename << endl;
00082     if (openLibrary(filename))
00083       numLibs++;
00084   }
00085 
00086   return numLibs;
00087 }
00088 
00089 void DlWrapper::clear()
00090 {
00091 # if defined (UNIX) || defined(CYGWIN)
00092   for (size_t i = 0; i < _handles.size(); ++i) {
00093     dlclose(_handles[i]);
00094   }
00095 #elif defined(WINDOWS)
00096   for (size_t i = 0; i < _handles.size(); ++i) {
00097     FreeLibrary(_handles[i]);
00098   }
00099 #endif
00100   _filenames.clear();
00101   _handles.clear();
00102 }
00103 
00104 bool DlWrapper::openLibrary(const std::string& filename)
00105 {
00106 # if defined (UNIX) || defined(CYGWIN)
00107   void* handle = dlopen(filename.c_str(), RTLD_LAZY);
00108   if (! handle) {
00109     cerr << "Cannot open library: " << dlerror() << '\n';
00110     return false;
00111   }
00112 # elif defined (WINDOWS)
00113   HMODULE handle = LoadLibrary(filename.c_str());
00114   if (! handle) {
00115     cerr << "Cannot open library." << endl;
00116     return false;
00117   }
00118 # endif
00119 
00120   //cerr << "loaded " << filename << endl;
00121 
00122   _filenames.push_back(filename);
00123   _handles.push_back(handle);
00124   return true;
00125 }
00126 
00127 } // end namespace g2o
00128 


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:02