g2o
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 44 of file dl_wrapper.h.

Constructor & Destructor Documentation

g2o::DlWrapper::DlWrapper ( )

Definition at line 56 of file dl_wrapper.cpp.

57 {
58 }
g2o::DlWrapper::~DlWrapper ( )
virtual

Definition at line 60 of file dl_wrapper.cpp.

61 {
62  //clear();
63 }
g2o::DlWrapper::DlWrapper ( const DlWrapper )
private

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

Member Function Documentation

void g2o::DlWrapper::clear ( )

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

Definition at line 100 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:72
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 65 of file dl_wrapper.cpp.

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

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

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 }
bool openLibrary(const std::string &filename)
Definition: dl_wrapper.cpp:115
#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
std::vector< std::string > _filenames
Definition: dl_wrapper.h:72
bool g2o::DlWrapper::openLibrary ( const std::string &  filename)

open a specific library

Definition at line 115 of file dl_wrapper.cpp.

References __PRETTY_FUNCTION__.

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

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 }
#define __PRETTY_FUNCTION__
Definition: macros.h:89
std::vector< std::string > _filenames
Definition: dl_wrapper.h:72
DlWrapper& g2o::DlWrapper::operator= ( const DlWrapper )
private

Member Data Documentation

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

Definition at line 72 of file dl_wrapper.h.


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