added SharedLib

This commit is contained in:
Michele Caini 2016-07-25 10:27:51 +02:00
parent 6be9eeced8
commit 1e85a8157b
2 changed files with 72 additions and 0 deletions

View File

@ -4,6 +4,7 @@
#include "uvw/fs.hpp"
#include "uvw/fs_poll.hpp"
#include "uvw/idle.hpp"
#include "uvw/lib.hpp"
#include "uvw/loop.hpp"
#include "uvw/pipe.hpp"
#include "uvw/poll.hpp"

71
src/uvw/lib.hpp Normal file
View File

@ -0,0 +1,71 @@
#pragma once
#include <utility>
#include <memory>
#include <string>
#include <uv.h>
#include "resource.hpp"
namespace uvw {
namespace details {
template<typename T> struct IsFunc: std::false_type { };
template<typename R, typename... A> struct IsFunc<R(A...)>: std::true_type { };
}
class SharedLib final {
explicit SharedLib(std::shared_ptr<Loop> ref, std::string filename) noexcept
: pLoop{std::move(ref)}, lib{}
{
opened = (0 == uv_dlopen(filename.data(), &lib));
}
public:
template<typename... Args>
static std::shared_ptr<SharedLib> create(Args&&... args) noexcept {
return std::shared_ptr<SharedLib>{new SharedLib{std::forward<Args>(args)...}};
}
SharedLib(const SharedLib &) = delete;
SharedLib(SharedLib &&) = delete;
~SharedLib() noexcept {
uv_dlclose(&lib);
}
SharedLib& operator=(const SharedLib &) = delete;
SharedLib& operator=(SharedLib &&) = delete;
explicit operator bool() const noexcept { return !opened; }
template<typename F>
F * sym(std::string name) {
static_assert(details::IsFunc<F>::value, "!");
F *func;
auto err = uv_dlsym(&lib, name.data(), reinterpret_cast<void**>(&func));
if(err) { func = nullptr; }
return func;
}
const char * error() const noexcept {
return uv_dlerror(&lib);
}
Loop& loop() const noexcept { return *pLoop; }
private:
std::shared_ptr<Loop> pLoop;
uv_lib_t lib;
bool opened;
};
}