2017-09-05 01:08:26 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-12-14 19:28:37 +00:00
|
|
|
#include <dlfcn.h>
|
2017-09-05 01:08:26 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2017-09-07 21:04:48 +00:00
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
|
2017-09-05 01:08:26 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2017-09-07 21:04:48 +00:00
|
|
|
|
2018-12-14 19:28:37 +00:00
|
|
|
/** Allows you to open a dynamic library and get a pointer to a function from it.
|
2017-09-05 01:08:26 +00:00
|
|
|
*/
|
|
|
|
class SharedLibrary : private boost::noncopyable
|
|
|
|
{
|
|
|
|
public:
|
2021-03-14 14:19:48 +00:00
|
|
|
explicit SharedLibrary(std::string_view path, int flags = RTLD_LAZY);
|
2017-09-05 01:08:26 +00:00
|
|
|
|
|
|
|
~SharedLibrary();
|
|
|
|
|
|
|
|
template <typename Func>
|
2021-03-14 14:19:48 +00:00
|
|
|
Func get(std::string_view name)
|
2017-09-05 01:08:26 +00:00
|
|
|
{
|
|
|
|
return reinterpret_cast<Func>(getImpl(name));
|
|
|
|
}
|
2021-03-14 14:19:48 +00:00
|
|
|
|
2017-09-05 01:08:26 +00:00
|
|
|
template <typename Func>
|
2021-03-14 14:19:48 +00:00
|
|
|
Func tryGet(std::string_view name)
|
2017-09-05 01:08:26 +00:00
|
|
|
{
|
|
|
|
return reinterpret_cast<Func>(getImpl(name, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-03-14 14:19:48 +00:00
|
|
|
void * getImpl(std::string_view name, bool no_throw = false);
|
2017-09-05 01:08:26 +00:00
|
|
|
|
|
|
|
void * handle = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
using SharedLibraryPtr = std::shared_ptr<SharedLibrary>;
|
2017-09-07 21:04:48 +00:00
|
|
|
|
2017-09-05 01:08:26 +00:00
|
|
|
}
|