renaming - it happens that handle is a keyword for libuv :-)
This commit is contained in:
parent
6ecf6782b7
commit
ee0143246c
@ -1,4 +1,5 @@
|
||||
#include "uvw/check.hpp"
|
||||
#include "uvw/handle.hpp"
|
||||
#include "uvw/idle.hpp"
|
||||
#include "uvw/loop.hpp"
|
||||
#include "uvw/prepare.hpp"
|
||||
|
||||
@ -4,20 +4,20 @@
|
||||
#include <utility>
|
||||
#include <memory>
|
||||
#include <uv.h>
|
||||
#include "resource.hpp"
|
||||
#include "handle.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
|
||||
namespace uvw {
|
||||
|
||||
|
||||
class Check final: public Resource<Check> {
|
||||
class Check final: public Handle<Check> {
|
||||
static void startCallback(Check &check, std::function<void(UVWError, Check &)> &cb, uv_check_t *) {
|
||||
cb(UVWError{}, check);
|
||||
}
|
||||
|
||||
explicit Check(std::shared_ptr<Loop> ref)
|
||||
: Resource{HandleType<uv_check_t>{}, std::move(ref)}
|
||||
: Handle{HandleType<uv_check_t>{}, std::move(ref)}
|
||||
{
|
||||
initialized = (uv_check_init(parent(), get<uv_check_t>()) == 0);
|
||||
}
|
||||
|
||||
@ -18,7 +18,7 @@ struct HandleType { };
|
||||
|
||||
|
||||
template<typename>
|
||||
class Resource;
|
||||
class Handle;
|
||||
|
||||
|
||||
namespace details {
|
||||
@ -53,7 +53,7 @@ private:
|
||||
|
||||
|
||||
template<typename T>
|
||||
class Resource: public std::enable_shared_from_this<T> {
|
||||
class Handle: public std::enable_shared_from_this<T> {
|
||||
template<typename, typename>
|
||||
friend struct details::UVCallbackFactory;
|
||||
|
||||
@ -66,7 +66,7 @@ protected:
|
||||
using CallbackFactory = details::UVCallbackFactory<T, F>;
|
||||
|
||||
template<typename U>
|
||||
explicit Resource(HandleType<U>, std::shared_ptr<Loop> r)
|
||||
explicit Handle(HandleType<U>, std::shared_ptr<Loop> r)
|
||||
: uvHandle{std::make_shared<U>()}, pLoop{std::move(r)}
|
||||
{ }
|
||||
|
||||
@ -83,15 +83,15 @@ protected:
|
||||
uv_loop_t* parent() const noexcept { return pLoop->loop.get(); }
|
||||
|
||||
public:
|
||||
explicit Resource(const Resource &) = delete;
|
||||
explicit Resource(Resource &&) = delete;
|
||||
explicit Handle(const Handle &) = delete;
|
||||
explicit Handle(Handle &&) = delete;
|
||||
|
||||
~Resource() { static_assert(std::is_base_of<Resource<T>, T>::value, "!"); }
|
||||
~Handle() { static_assert(std::is_base_of<Handle<T>, T>::value, "!"); }
|
||||
|
||||
void operator=(const Resource &) = delete;
|
||||
void operator=(Resource &&) = delete;
|
||||
void operator=(const Handle &) = delete;
|
||||
void operator=(Handle &&) = delete;
|
||||
|
||||
Handle<T> handle() noexcept { return pLoop->handle(Resource<T>::shared_from_this()); }
|
||||
Resource<T> resource() noexcept { return pLoop->resource(Handle<T>::shared_from_this()); }
|
||||
Loop& loop() const noexcept { return *pLoop; }
|
||||
|
||||
bool active() const noexcept { return !(uv_is_active(get<uv_handle_t>()) == 0); }
|
||||
@ -103,7 +103,7 @@ public:
|
||||
|
||||
void close(std::function<void(UVWError, T &)> cb) noexcept {
|
||||
using CBF = CallbackFactory<void(uv_handle_t*)>;
|
||||
auto func = CBF::template once<&Resource<T>::closeCallback>(*static_cast<T*>(this), std::move(cb));
|
||||
auto func = CBF::template once<&Handle<T>::closeCallback>(*static_cast<T*>(this), std::move(cb));
|
||||
uv_close(get<uv_handle_t>(), func);
|
||||
}
|
||||
|
||||
@ -121,7 +121,7 @@ namespace details {
|
||||
template<typename T, typename H, typename... Args>
|
||||
template<void(*F)(T &, std::function<void(UVWError, T &)> &, H, Args...)>
|
||||
auto UVCallbackFactory<T, void(H, Args...)>::once(T &ref, std::function<void(UVWError, T &)> cb) {
|
||||
Resource<T> &res = ref;
|
||||
Handle<T> &res = ref;
|
||||
res.callback = std::move(cb);
|
||||
res.leak = res.shared_from_this();
|
||||
res.template get<uv_handle_t>()->data = &ref;
|
||||
@ -131,7 +131,7 @@ auto UVCallbackFactory<T, void(H, Args...)>::once(T &ref, std::function<void(UVW
|
||||
template<typename T, typename H, typename... Args>
|
||||
template<void(*F)(T &, std::function<void(UVWError, T &)> &, H, Args...)>
|
||||
auto UVCallbackFactory<T, void(H, Args...)>::on(T &ref, std::function<void(UVWError, T &)> cb) {
|
||||
Resource<T> &res = ref;
|
||||
Handle<T> &res = ref;
|
||||
res.callback = std::move(cb);
|
||||
res.leak = res.shared_from_this();
|
||||
res.template get<uv_handle_t>()->data = &ref;
|
||||
@ -4,20 +4,20 @@
|
||||
#include <utility>
|
||||
#include <memory>
|
||||
#include <uv.h>
|
||||
#include "resource.hpp"
|
||||
#include "handle.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
|
||||
namespace uvw {
|
||||
|
||||
|
||||
class Idle final: public Resource<Idle> {
|
||||
class Idle final: public Handle<Idle> {
|
||||
static void startCallback(Idle &idle, std::function<void(UVWError, Idle &)> &cb, uv_idle_t *) {
|
||||
cb(UVWError{}, idle);
|
||||
}
|
||||
|
||||
explicit Idle(std::shared_ptr<Loop> ref)
|
||||
: Resource{HandleType<uv_idle_t>{}, std::move(ref)}
|
||||
: Handle{HandleType<uv_idle_t>{}, std::move(ref)}
|
||||
{
|
||||
initialized = (uv_idle_init(parent(), get<uv_idle_t>()) == 0);
|
||||
}
|
||||
|
||||
@ -16,33 +16,33 @@ class Loop;
|
||||
|
||||
|
||||
template<typename R>
|
||||
class Handle {
|
||||
class Resource {
|
||||
template<typename>
|
||||
friend class Handle;
|
||||
friend class Resource;
|
||||
|
||||
friend class Loop;
|
||||
|
||||
template<typename... Args>
|
||||
explicit constexpr Handle(std::shared_ptr<Loop>&& l, Args&&... args)
|
||||
explicit constexpr Resource(std::shared_ptr<Loop>&& l, Args&&... args)
|
||||
: res{R::create(std::move(l), std::forward<Args>(args)...)}
|
||||
{ }
|
||||
|
||||
explicit constexpr Handle(std::shared_ptr<R> ptr): res{std::move(ptr)} { }
|
||||
explicit constexpr Resource(std::shared_ptr<R> ptr): res{std::move(ptr)} { }
|
||||
|
||||
public:
|
||||
explicit constexpr Handle(): res{} { }
|
||||
explicit constexpr Resource(): res{} { }
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_base_of<R, T>::value>* = nullptr>
|
||||
constexpr Handle(const Handle<T> &other): res{other.res} { }
|
||||
constexpr Resource(const Resource<T> &other): res{other.res} { }
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_base_of<R, T>::value>* = nullptr>
|
||||
constexpr Handle(Handle<T> &&other): res{std::move(other.res)} { }
|
||||
constexpr Resource(Resource<T> &&other): res{std::move(other.res)} { }
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_base_of<R, T>::value>* = nullptr>
|
||||
constexpr void operator=(const Handle<T> &other) { res = other.res; }
|
||||
constexpr void operator=(const Resource<T> &other) { res = other.res; }
|
||||
|
||||
template<typename T, std::enable_if_t<std::is_base_of<R, T>::value>* = nullptr>
|
||||
constexpr void operator=(Handle<T> &&other) { res = std::move(other.res); }
|
||||
constexpr void operator=(Resource<T> &&other) { res = std::move(other.res); }
|
||||
|
||||
constexpr explicit operator bool() const { return static_cast<bool>(res); }
|
||||
|
||||
@ -56,7 +56,7 @@ private:
|
||||
|
||||
class Loop final: public std::enable_shared_from_this<Loop> {
|
||||
template<typename>
|
||||
friend class Resource;
|
||||
friend class Handle;
|
||||
|
||||
using Deleter = std::function<void(uv_loop_t *)>;
|
||||
|
||||
@ -106,13 +106,13 @@ public:
|
||||
}
|
||||
|
||||
template<typename R>
|
||||
Handle<R> handle(std::shared_ptr<R> ptr) {
|
||||
return Handle<R>{std::move(ptr)};
|
||||
Resource<R> resource(std::shared_ptr<R> ptr) {
|
||||
return Resource<R>{std::move(ptr)};
|
||||
}
|
||||
|
||||
template<typename R, typename... Args>
|
||||
Handle<R> handle(Args&&... args) {
|
||||
return Handle<R>{shared_from_this(), std::forward<Args>(args)...};
|
||||
Resource<R> resource(Args&&... args) {
|
||||
return Resource<R>{shared_from_this(), std::forward<Args>(args)...};
|
||||
}
|
||||
|
||||
UVWError close() noexcept {
|
||||
|
||||
@ -4,20 +4,20 @@
|
||||
#include <utility>
|
||||
#include <memory>
|
||||
#include <uv.h>
|
||||
#include "resource.hpp"
|
||||
#include "handle.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
|
||||
namespace uvw {
|
||||
|
||||
|
||||
class Prepare final: public Resource<Prepare> {
|
||||
class Prepare final: public Handle<Prepare> {
|
||||
static void startCallback(Prepare &prepare, std::function<void(UVWError, Prepare &)> &cb, uv_prepare_t *) {
|
||||
cb(UVWError{}, prepare);
|
||||
}
|
||||
|
||||
explicit Prepare(std::shared_ptr<Loop> ref)
|
||||
: Resource{HandleType<uv_prepare_t>{}, std::move(ref)}
|
||||
: Handle{HandleType<uv_prepare_t>{}, std::move(ref)}
|
||||
{
|
||||
initialized = (uv_prepare_init(parent(), get<uv_prepare_t>()) == 0);
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
|
||||
#include <uv.h>
|
||||
#include "resource.hpp"
|
||||
#include "handle.hpp"
|
||||
#include "loop.hpp"
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@ namespace uvw {
|
||||
|
||||
|
||||
template<typename T>
|
||||
class Stream: public Resource<T> {
|
||||
class Stream: public Handle<T> {
|
||||
static constexpr unsigned int DEFAULT_BACKLOG = 128;
|
||||
|
||||
static void listenCallback(T &t, std::function<void(UVWError, T &)> &cb, uv_stream_t *, int status) {
|
||||
@ -18,7 +18,7 @@ class Stream: public Resource<T> {
|
||||
}
|
||||
|
||||
protected:
|
||||
using Resource<T>::Resource;
|
||||
using Handle<T>::Handle;
|
||||
|
||||
public:
|
||||
// TODO shutdown
|
||||
@ -28,7 +28,7 @@ public:
|
||||
}
|
||||
|
||||
void listen(int backlog, std::function<void(UVWError, T &)> cb) noexcept {
|
||||
using CBF = typename Resource<T>::template CallbackFactory<void(uv_stream_t *, int)>;
|
||||
using CBF = typename Handle<T>::template CallbackFactory<void(uv_stream_t *, int)>;
|
||||
auto func = CBF::template on<&Stream<T>::listenCallback>(*static_cast<T*>(this), cb);
|
||||
auto err = uv_listen(this->template get<uv_stream_t>(), backlog, func);
|
||||
if(err) { Stream<T>::error(err); }
|
||||
|
||||
@ -6,20 +6,20 @@
|
||||
#include <chrono>
|
||||
#include <ratio>
|
||||
#include <uv.h>
|
||||
#include "resource.hpp"
|
||||
#include "handle.hpp"
|
||||
#include "util.hpp"
|
||||
|
||||
|
||||
namespace uvw {
|
||||
|
||||
|
||||
class Timer final: public Resource<Timer> {
|
||||
class Timer final: public Handle<Timer> {
|
||||
static void startCallback(Timer &timer, std::function<void(UVWError, Timer &)> &cb, uv_timer_t *) {
|
||||
cb(UVWError{}, timer);
|
||||
}
|
||||
|
||||
explicit Timer(std::shared_ptr<Loop> ref)
|
||||
: Resource{HandleType<uv_timer_t>{}, std::move(ref)}
|
||||
: Handle{HandleType<uv_timer_t>{}, std::move(ref)}
|
||||
{
|
||||
initialized = (uv_timer_init(parent(), get<uv_timer_t>()) == 0);
|
||||
}
|
||||
|
||||
@ -3,14 +3,14 @@
|
||||
|
||||
|
||||
void listen(uvw::Loop &loop) {
|
||||
uvw::Handle<uvw::Tcp> handle = loop.handle<uvw::Tcp>();
|
||||
uvw::Resource<uvw::Tcp> resource = loop.resource<uvw::Tcp>();
|
||||
|
||||
auto cb = [](uvw::UVWError err, uvw::Tcp &srv) mutable {
|
||||
std::cout << "listen: " << ((bool)err) << std::endl;
|
||||
|
||||
if(!err) {
|
||||
uvw::Handle<uvw::Tcp> handle = srv.loop().handle<uvw::Tcp>();
|
||||
uvw::Tcp &client = handle;
|
||||
uvw::Resource<uvw::Tcp> resource = srv.loop().resource<uvw::Tcp>();
|
||||
uvw::Tcp &client = resource;
|
||||
|
||||
err = srv.accept(client);
|
||||
std::cout << "accept: " << ((bool)err) << std::endl;
|
||||
@ -30,10 +30,10 @@ void listen(uvw::Loop &loop) {
|
||||
}
|
||||
|
||||
if(!err) {
|
||||
client.close([handle = srv.handle()](uvw::UVWError err, uvw::Tcp &) mutable {
|
||||
client.close([resource = srv.resource()](uvw::UVWError err, uvw::Tcp &) mutable {
|
||||
std::cout << "close: " << ((bool)err) << std::endl;
|
||||
|
||||
uvw::Tcp &srv = handle;
|
||||
uvw::Tcp &srv = resource;
|
||||
srv.close([](uvw::UVWError err, uvw::Tcp &) mutable {
|
||||
std::cout << "close: " << ((bool)err) << std::endl;
|
||||
});
|
||||
@ -42,7 +42,7 @@ void listen(uvw::Loop &loop) {
|
||||
}
|
||||
};
|
||||
|
||||
uvw::Tcp &tcp = handle;
|
||||
uvw::Tcp &tcp = resource;
|
||||
uvw::UVWError err = tcp.bind<uvw::Tcp::IPv4>("127.0.0.1", 4242);
|
||||
std::cout << "bind: " << ((bool)err) << std::endl;
|
||||
|
||||
@ -57,7 +57,7 @@ void listen(uvw::Loop &loop) {
|
||||
|
||||
|
||||
void conn(uvw::Loop &loop) {
|
||||
uvw::Handle<uvw::Tcp> handle = loop.handle<uvw::Tcp>();
|
||||
uvw::Resource<uvw::Tcp> resource = loop.resource<uvw::Tcp>();
|
||||
|
||||
auto cb = [](uvw::UVWError err, uvw::Tcp &tcp) mutable {
|
||||
std::cout << "connect: " << ((bool)err) << std::endl;
|
||||
@ -69,7 +69,7 @@ void conn(uvw::Loop &loop) {
|
||||
tcp.close(cb);
|
||||
};
|
||||
|
||||
uvw::Tcp &tcp = handle;
|
||||
uvw::Tcp &tcp = resource;
|
||||
tcp.connect<uvw::Tcp::IPv4>(std::string{"127.0.0.1"}, 4242, cb);
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user