g2o
Loading...
Searching...
No Matches
Public Member Functions | Protected Attributes | Private Member Functions | List of all members
g2o::DlWrapper Class Reference

Loading libraries during run-time. More...

#include <dl_wrapper.h>

Public Member Functions

 DlWrapper ()
 
virtual ~DlWrapper ()
 
int openLibraries (const std::string &directory, const std::string &pattern="")
 
bool openLibrary (const std::string &filename)
 
void clear ()
 

Protected Attributes

std::vector< std::string > _filenames
 

Private Member Functions

 DlWrapper (const DlWrapper &)
 
DlWrapperoperator= (const DlWrapper &)
 

Detailed Description

Loading libraries during run-time.

Definition at line 46 of file dl_wrapper.h.

Constructor & Destructor Documentation

◆ DlWrapper() [1/2]

g2o::DlWrapper::DlWrapper ( )

Definition at line 57 of file dl_wrapper.cpp.

57{}

◆ ~DlWrapper()

g2o::DlWrapper::~DlWrapper ( )
virtual

Definition at line 59 of file dl_wrapper.cpp.

59 {
60 // clear();
61}

◆ DlWrapper() [2/2]

g2o::DlWrapper::DlWrapper ( const DlWrapper )
private

it's not allowed to draw a copy of the wrapper

Member Function Documentation

◆ clear()

void g2o::DlWrapper::clear ( )

free all loaded libs, i.e., call dlclose()

Definition at line 101 of file dl_wrapper.cpp.

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}
std::vector< std::string > _filenames
Definition dl_wrapper.h:74

References _filenames.

◆ openLibraries()

int g2o::DlWrapper::openLibraries ( const std::string &  directory,
const std::string &  pattern = "" 
)

open all libs from a directory matching a specific pattern.

Returns
number of loaded libs

Definition at line 63 of file dl_wrapper.cpp.

64 {
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}
bool openLibrary(const std::string &filename)
#define SO_EXT
#define SO_EXT_LEN
std::vector< std::string > getFilesByPattern(const char *pattern)

References _filenames, g2o::getFilesByPattern(), openLibrary(), SO_EXT, and SO_EXT_LEN.

Referenced by g2o::loadStandardSolver(), and g2o::loadStandardTypes().

◆ openLibrary()

bool g2o::DlWrapper::openLibrary ( const std::string &  filename)

open a specific library

Definition at line 115 of file dl_wrapper.cpp.

115 {
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}
#define __PRETTY_FUNCTION__
Definition macros.h:90

References __PRETTY_FUNCTION__, and _filenames.

Referenced by g2o::loadStandardSolver(), g2o::loadStandardTypes(), and openLibraries().

◆ operator=()

DlWrapper & g2o::DlWrapper::operator= ( const DlWrapper )
private

Member Data Documentation

◆ _filenames

std::vector<std::string> g2o::DlWrapper::_filenames
protected

Definition at line 74 of file dl_wrapper.h.

Referenced by clear(), openLibraries(), and openLibrary().


The documentation for this class was generated from the following files: