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:
|
2018-12-14 19:28:37 +00:00
|
|
|
explicit SharedLibrary(const std::string & path, int flags = RTLD_LAZY);
|
2017-09-05 01:08:26 +00:00
|
|
|
|
|
|
|
~SharedLibrary();
|
|
|
|
|
|
|
|
template <typename Func>
|
|
|
|
Func get(const std::string & name)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<Func>(getImpl(name));
|
|
|
|
}
|
|
|
|
template <typename Func>
|
|
|
|
Func tryGet(const std::string & name)
|
|
|
|
{
|
|
|
|
return reinterpret_cast<Func>(getImpl(name, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void * getImpl(const std::string & name, bool no_throw = false);
|
|
|
|
|
|
|
|
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
|
|
|
}
|