lsst.base g238f615269+8827c0d83c
LSST Data Management Base Package
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
12namespace lsst {
13namespace base {
14
17public:
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
40 std::string const& libName
41 );
42
43
50template <typename T>
52 std::string const& libName,
53 std::string const& symName
54 )
55{
56 void* lib = dlopen(getLibraryFilename(libName).c_str(), RTLD_NOW | RTLD_GLOBAL | 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
T c_str(T... args)
Unable to load library.
Definition: library.h:16
LibraryException(std::string const &name)
Definition: library.h:18
LibraryException(std::string const &lib, std::string const &func)
Definition: library.h:20
#define RTLD_DEEPBIND
Definition: library.h:9
bool canLoadLibrary(std::string const &libName)
Return whether we can load a library.
Definition: library.cc:37
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
T * loadSymbol(std::string const &libName, std::string const &symName)
Load a symbol from a dynamic library.
Definition: library.h:51
Definition: mainpage.dox:1
STL namespace.