LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
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
Unable to load library.
Definition: library.h:16
T * loadSymbol(std::string const &libName, std::string const &symName)
Load a symbol from a dynamic library.
Definition: library.h:51
STL namespace.
std::string getLibraryFilename(std::string const &name)
Get filename for library.
Definition: library.cc:27
std::string libraryExtension()
Return filename extension for libraries.
Definition: library.cc:17
STL class.
#define RTLD_DEEPBIND
Definition: library.h:9
A base class for image defects.
LibraryException(std::string const &name)
Definition: library.h:18
Definition: __init__.py:1
LibraryException(std::string const &lib, std::string const &func)
Definition: library.h:20
T c_str(T... args)
bool canLoadLibrary(std::string const &libName)
Return whether we can load a library.
Definition: library.cc:37