g2o
dl_wrapper.cpp
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #include <sys/types.h>
28 
29 #include <cstdio>
30 #include <iostream>
31 #include <algorithm>
32 
33 #include "dl_wrapper.h"
34 #include "g2o/stuff/macros.h"
36 
37 #if defined (UNIX) || defined(CYGWIN)
38 #include <dlfcn.h>
39 #endif
40 
41 #ifdef __APPLE__
42 #define SO_EXT "dylib"
43 #define SO_EXT_LEN 5
44 #elif defined (WINDOWS) || defined (CYGWIN)
45 #define SO_EXT "dll"
46 #define SO_EXT_LEN 3
47 #else // Linux
48 #define SO_EXT "so"
49 #define SO_EXT_LEN 2
50 #endif
51 
52 using namespace std;
53 
54 namespace g2o {
55 
56 DlWrapper::DlWrapper()
57 {
58 }
59 
60 DlWrapper::~DlWrapper()
61 {
62  //clear();
63 }
64 
65 int DlWrapper::openLibraries(const std::string& directory, const std::string& pattern)
66 {
67  //cerr << "# loading libraries from " << directory << "\t pattern: " << pattern << endl;
68  string searchPattern = directory + "/" + pattern;
69  if (pattern == "")
70  searchPattern = directory + "/*";
71  vector<string> matchingFiles = getFilesByPattern(searchPattern.c_str());
72 
73  int numLibs = 0;
74  for (size_t i = 0; i < matchingFiles.size(); ++i) {
75  const string& filename = matchingFiles[i];
76  if (find(_filenames.begin(), _filenames.end(), filename) != _filenames.end())
77  continue;
78 
79  // If we are doing a release build, the wildcards will pick up the
80  // suffixes; unfortunately the "_rd" extension means that we
81  // don't seem to be able to filter out the incompatible files using a
82  // wildcard expansion to wordexp.
83 
84 #ifndef G2O_LIBRARY_POSTFIX
85  if ((filename.rfind(string("_d.") + SO_EXT) == filename.length() - 3 - SO_EXT_LEN)
86  || (filename.rfind(string("_rd.") + SO_EXT) == filename.length() - 4 - SO_EXT_LEN)
87  || (filename.rfind(string("_s.") + SO_EXT) == filename.length() - 3 - SO_EXT_LEN))
88  continue;
89 #endif
90 
91  // open the lib
92  //cerr << "loading " << filename << endl;
93  if (openLibrary(filename))
94  numLibs++;
95  }
96 
97  return numLibs;
98 }
99 
100 void DlWrapper::clear()
101 {
102 # if defined (UNIX) || defined(CYGWIN)
103  for (size_t i = 0; i < _handles.size(); ++i) {
104  dlclose(_handles[i]);
105  }
106 #elif defined(WINDOWS)
107  for (size_t i = 0; i < _handles.size(); ++i) {
108  FreeLibrary(_handles[i]);
109  }
110 #endif
111  _filenames.clear();
112  _handles.clear();
113 }
114 
115 bool DlWrapper::openLibrary(const std::string& filename)
116 {
117 # if defined (UNIX) || defined(CYGWIN)
118  void* handle = dlopen(filename.c_str(), RTLD_LAZY);
119  if (! handle) {
120  cerr << __PRETTY_FUNCTION__ << " Cannot open library: " << dlerror() << '\n';
121  return false;
122  }
123 # elif defined (WINDOWS)
124  HMODULE handle = LoadLibrary(filename.c_str());
125  if (! handle) {
126  cerr << __PRETTY_FUNCTION__ << " Cannot open library." << endl;
127  return false;
128  }
129 # endif
130 
131  //cerr << "loaded " << filename << endl;
132 
133  _filenames.push_back(filename);
134  _handles.push_back(handle);
135  return true;
136 }
137 
138 } // end namespace g2o
139 
#define __PRETTY_FUNCTION__
Definition: macros.h:89
#define SO_EXT_LEN
Definition: dl_wrapper.cpp:49
std::vector< std::string > getFilesByPattern(const char *pattern)
#define SO_EXT
Definition: dl_wrapper.cpp:48