| 1 | #pragma once |
|---|---|
| 2 | |
| 3 | #include <dlfcn.h> |
| 4 | #include <memory> |
| 5 | #include <string> |
| 6 | #include <boost/noncopyable.hpp> |
| 7 | |
| 8 | |
| 9 | namespace DB |
| 10 | { |
| 11 | |
| 12 | /** Allows you to open a dynamic library and get a pointer to a function from it. |
| 13 | */ |
| 14 | class SharedLibrary : private boost::noncopyable |
| 15 | { |
| 16 | public: |
| 17 | explicit SharedLibrary(const std::string & path, int flags = RTLD_LAZY); |
| 18 | |
| 19 | ~SharedLibrary(); |
| 20 | |
| 21 | template <typename Func> |
| 22 | Func get(const std::string & name) |
| 23 | { |
| 24 | return reinterpret_cast<Func>(getImpl(name)); |
| 25 | } |
| 26 | template <typename Func> |
| 27 | Func tryGet(const std::string & name) |
| 28 | { |
| 29 | return reinterpret_cast<Func>(getImpl(name, true)); |
| 30 | } |
| 31 | |
| 32 | private: |
| 33 | void * getImpl(const std::string & name, bool no_throw = false); |
| 34 | |
| 35 | void * handle = nullptr; |
| 36 | }; |
| 37 | |
| 38 | using SharedLibraryPtr = std::shared_ptr<SharedLibrary>; |
| 39 | |
| 40 | } |
| 41 |