g2o
Loading...
Searching...
No Matches
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 "dl_wrapper.h"
28
29#include <sys/types.h>
30
31#include <algorithm>
32#include <cstdio>
33#include <iostream>
34
36#include "g2o/stuff/macros.h"
37
38#if defined(UNIX) || defined(CYGWIN)
39#include <dlfcn.h>
40#endif
41
42#ifdef __APPLE__
43#define SO_EXT "dylib"
44#define SO_EXT_LEN 5
45#elif defined(WINDOWS) || defined(CYGWIN)
46#define SO_EXT "dll"
47#define SO_EXT_LEN 3
48#else // Linux
49#define SO_EXT "so"
50#define SO_EXT_LEN 2
51#endif
52
53using namespace std;
54
55namespace g2o {
56
58
60 // clear();
61}
62
63int DlWrapper::openLibraries(const std::string& directory,
64 const std::string& pattern) {
65 // cerr << "# loading libraries from " << directory << "\t pattern: " <<
66 // pattern << endl;
67 string searchPattern = directory + "/" + pattern;
68 if (pattern == "") searchPattern = directory + "/*";
69 vector<string> matchingFiles = getFilesByPattern(searchPattern.c_str());
70
71 int numLibs = 0;
72 for (size_t i = 0; i < matchingFiles.size(); ++i) {
73 const string& filename = matchingFiles[i];
74 if (find(_filenames.begin(), _filenames.end(), filename) !=
75 _filenames.end())
76 continue;
77
78 // If we are doing a release build, the wildcards will pick up the
79 // suffixes; unfortunately the "_rd" extension means that we
80 // don't seem to be able to filter out the incompatible files using a
81 // wildcard expansion to wordexp.
82
83#ifndef G2O_LIBRARY_POSTFIX
84 if ((filename.rfind(string("_d.") + SO_EXT) ==
85 filename.length() - 3 - SO_EXT_LEN) ||
86 (filename.rfind(string("_rd.") + SO_EXT) ==
87 filename.length() - 4 - SO_EXT_LEN) ||
88 (filename.rfind(string("_s.") + SO_EXT) ==
89 filename.length() - 3 - SO_EXT_LEN))
90 continue;
91#endif
92
93 // open the lib
94 // cerr << "loading " << filename << endl;
95 if (openLibrary(filename)) numLibs++;
96 }
97
98 return numLibs;
99}
100
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
115bool DlWrapper::openLibrary(const std::string& filename) {
116#if defined(UNIX) || defined(CYGWIN)
117 void* handle = dlopen(filename.c_str(), RTLD_LAZY);
118 if (!handle) {
119 cerr << __PRETTY_FUNCTION__ << " Cannot open library: " << dlerror()
120 << '\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
virtual ~DlWrapper()
bool openLibrary(const std::string &filename)
std::vector< std::string > _filenames
Definition dl_wrapper.h:74
int openLibraries(const std::string &directory, const std::string &pattern="")
#define SO_EXT
#define SO_EXT_LEN
#define __PRETTY_FUNCTION__
Definition macros.h:90
std::vector< std::string > getFilesByPattern(const char *pattern)
Definition jet.h:876