LSSTApplications  17.0+124,17.0+14,17.0+73,18.0.0+37,18.0.0+80,18.0.0-4-g68ffd23+4,18.1.0-1-g0001055+12,18.1.0-1-g03d53ef+5,18.1.0-1-g1349e88+55,18.1.0-1-g2505f39+44,18.1.0-1-g5315e5e+4,18.1.0-1-g5e4b7ea+14,18.1.0-1-g7e8fceb+4,18.1.0-1-g85f8cd4+48,18.1.0-1-g8ff0b9f+4,18.1.0-1-ga2c679d+1,18.1.0-1-gd55f500+35,18.1.0-10-gb58edde+2,18.1.0-11-g0997b02+4,18.1.0-13-gfe4edf0b+12,18.1.0-14-g259bd21+21,18.1.0-19-gdb69f3f+2,18.1.0-2-g5f9922c+24,18.1.0-2-gd3b74e5+11,18.1.0-2-gfbf3545+32,18.1.0-26-g728bddb4+5,18.1.0-27-g6ff7ca9+2,18.1.0-3-g52aa583+25,18.1.0-3-g8ea57af+9,18.1.0-3-gb69f684+42,18.1.0-3-gfcaddf3+6,18.1.0-32-gd8786685a,18.1.0-4-gf3f9b77+6,18.1.0-5-g1dd662b+2,18.1.0-5-g6dbcb01+41,18.1.0-6-gae77429+3,18.1.0-7-g9d75d83+9,18.1.0-7-gae09a6d+30,18.1.0-9-gc381ef5+4,w.2019.45
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