LSSTApplications  20.0.0
LSSTDataManagementBasePackage
library.h
Go to the documentation of this file.
1 #ifndef LSST_BASE_LIBRARY_H
2 #define LSST_BASE_LIBRARY_H
3 
4 #include <stdexcept>
5 #include <string>
6 
7 #include <dlfcn.h>
8 #ifndef RTLD_DEEPBIND // Non-POSIX flag, so it may not exist
9 #define RTLD_DEEPBIND 0 // Will be ignored
10 #endif
11 
12 namespace lsst {
13 namespace base {
14 
17 public:
19  std::runtime_error("Unable to dynamically load library " + name) {};
20  LibraryException(std::string const& lib, std::string const& func) :
21  std::runtime_error("Unable to find function " + func + " from dynamically loaded library " + lib) {};
22 };
23 
28 
34 
39 bool canLoadLibrary(
40  std::string const& libName
41  );
42 
43 
50 template <typename T>
52  std::string const& libName,
53  std::string const& symName
54  )
55 {
56  void* lib = dlopen(getLibraryFilename(libName).c_str(), RTLD_LAZY | RTLD_DEEPBIND);
57  if (!lib) {
58  throw LibraryException(libName);
59  }
60 
61  T* sym;
62  (void*&)sym = dlsym(lib, symName.c_str());
63  if (!sym) {
64  throw LibraryException(libName, symName);
65  }
66  return sym;
67 }
68 
69 }} // lsst::base
70 
71 
72 #endif
std::string
STL class.
lsst::base::getLibraryFilename
std::string getLibraryFilename(std::string const &name)
Get filename for library.
Definition: library.cc:27
lsst::base::loadSymbol
T * loadSymbol(std::string const &libName, std::string const &symName)
Load a symbol from a dynamic library.
Definition: library.h:51
base
Definition: __init__.py:1
lsst::base::LibraryException::LibraryException
LibraryException(std::string const &lib, std::string const &func)
Definition: library.h:20
lsst::afw::geom.transform.transformContinued.name
string name
Definition: transformContinued.py:32
RTLD_DEEPBIND
#define RTLD_DEEPBIND
Definition: library.h:9
std::string::c_str
T c_str(T... args)
lsst::base::libraryExtension
std::string libraryExtension()
Return filename extension for libraries.
Definition: library.cc:17
std::runtime_error
STL class.
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::base::canLoadLibrary
bool canLoadLibrary(std::string const &libName)
Return whether we can load a library.
Definition: library.cc:37
std
STL namespace.
lsst::base::LibraryException
Unable to load library.
Definition: library.h:16
lsst::base::LibraryException::LibraryException
LibraryException(std::string const &name)
Definition: library.h:18