diff --git a/src/uvw.hpp b/src/uvw.hpp index 5978f081..888012e9 100644 --- a/src/uvw.hpp +++ b/src/uvw.hpp @@ -1,4 +1,5 @@ #include "uvw/check.hpp" +#include "uvw/handle.hpp" #include "uvw/idle.hpp" #include "uvw/loop.hpp" #include "uvw/prepare.hpp" diff --git a/src/uvw/check.hpp b/src/uvw/check.hpp index b2ec4fe9..31b988a0 100644 --- a/src/uvw/check.hpp +++ b/src/uvw/check.hpp @@ -4,20 +4,20 @@ #include #include #include -#include "resource.hpp" +#include "handle.hpp" #include "util.hpp" namespace uvw { -class Check final: public Resource { +class Check final: public Handle { static void startCallback(Check &check, std::function &cb, uv_check_t *) { cb(UVWError{}, check); } explicit Check(std::shared_ptr ref) - : Resource{HandleType{}, std::move(ref)} + : Handle{HandleType{}, std::move(ref)} { initialized = (uv_check_init(parent(), get()) == 0); } diff --git a/src/uvw/resource.hpp b/src/uvw/handle.hpp similarity index 86% rename from src/uvw/resource.hpp rename to src/uvw/handle.hpp index 295727ce..8b9a50a0 100644 --- a/src/uvw/resource.hpp +++ b/src/uvw/handle.hpp @@ -18,7 +18,7 @@ struct HandleType { }; template -class Resource; +class Handle; namespace details { @@ -53,7 +53,7 @@ private: template -class Resource: public std::enable_shared_from_this { +class Handle: public std::enable_shared_from_this { template friend struct details::UVCallbackFactory; @@ -66,7 +66,7 @@ protected: using CallbackFactory = details::UVCallbackFactory; template - explicit Resource(HandleType, std::shared_ptr r) + explicit Handle(HandleType, std::shared_ptr r) : uvHandle{std::make_shared()}, 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, T>::value, "!"); } + ~Handle() { static_assert(std::is_base_of, T>::value, "!"); } - void operator=(const Resource &) = delete; - void operator=(Resource &&) = delete; + void operator=(const Handle &) = delete; + void operator=(Handle &&) = delete; - Handle handle() noexcept { return pLoop->handle(Resource::shared_from_this()); } + Resource resource() noexcept { return pLoop->resource(Handle::shared_from_this()); } Loop& loop() const noexcept { return *pLoop; } bool active() const noexcept { return !(uv_is_active(get()) == 0); } @@ -103,7 +103,7 @@ public: void close(std::function cb) noexcept { using CBF = CallbackFactory; - auto func = CBF::template once<&Resource::closeCallback>(*static_cast(this), std::move(cb)); + auto func = CBF::template once<&Handle::closeCallback>(*static_cast(this), std::move(cb)); uv_close(get(), func); } @@ -121,7 +121,7 @@ namespace details { template template &, H, Args...)> auto UVCallbackFactory::once(T &ref, std::function cb) { - Resource &res = ref; + Handle &res = ref; res.callback = std::move(cb); res.leak = res.shared_from_this(); res.template get()->data = &ref; @@ -131,7 +131,7 @@ auto UVCallbackFactory::once(T &ref, std::function template &, H, Args...)> auto UVCallbackFactory::on(T &ref, std::function cb) { - Resource &res = ref; + Handle &res = ref; res.callback = std::move(cb); res.leak = res.shared_from_this(); res.template get()->data = &ref; diff --git a/src/uvw/idle.hpp b/src/uvw/idle.hpp index 44a2b1ba..f38af571 100644 --- a/src/uvw/idle.hpp +++ b/src/uvw/idle.hpp @@ -4,20 +4,20 @@ #include #include #include -#include "resource.hpp" +#include "handle.hpp" #include "util.hpp" namespace uvw { -class Idle final: public Resource { +class Idle final: public Handle { static void startCallback(Idle &idle, std::function &cb, uv_idle_t *) { cb(UVWError{}, idle); } explicit Idle(std::shared_ptr ref) - : Resource{HandleType{}, std::move(ref)} + : Handle{HandleType{}, std::move(ref)} { initialized = (uv_idle_init(parent(), get()) == 0); } diff --git a/src/uvw/loop.hpp b/src/uvw/loop.hpp index 1ac9fd38..f0d3311c 100644 --- a/src/uvw/loop.hpp +++ b/src/uvw/loop.hpp @@ -16,33 +16,33 @@ class Loop; template -class Handle { +class Resource { template - friend class Handle; + friend class Resource; friend class Loop; template - explicit constexpr Handle(std::shared_ptr&& l, Args&&... args) + explicit constexpr Resource(std::shared_ptr&& l, Args&&... args) : res{R::create(std::move(l), std::forward(args)...)} { } - explicit constexpr Handle(std::shared_ptr ptr): res{std::move(ptr)} { } + explicit constexpr Resource(std::shared_ptr ptr): res{std::move(ptr)} { } public: - explicit constexpr Handle(): res{} { } + explicit constexpr Resource(): res{} { } template::value>* = nullptr> - constexpr Handle(const Handle &other): res{other.res} { } + constexpr Resource(const Resource &other): res{other.res} { } template::value>* = nullptr> - constexpr Handle(Handle &&other): res{std::move(other.res)} { } + constexpr Resource(Resource &&other): res{std::move(other.res)} { } template::value>* = nullptr> - constexpr void operator=(const Handle &other) { res = other.res; } + constexpr void operator=(const Resource &other) { res = other.res; } template::value>* = nullptr> - constexpr void operator=(Handle &&other) { res = std::move(other.res); } + constexpr void operator=(Resource &&other) { res = std::move(other.res); } constexpr explicit operator bool() const { return static_cast(res); } @@ -56,7 +56,7 @@ private: class Loop final: public std::enable_shared_from_this { template - friend class Resource; + friend class Handle; using Deleter = std::function; @@ -106,13 +106,13 @@ public: } template - Handle handle(std::shared_ptr ptr) { - return Handle{std::move(ptr)}; + Resource resource(std::shared_ptr ptr) { + return Resource{std::move(ptr)}; } template - Handle handle(Args&&... args) { - return Handle{shared_from_this(), std::forward(args)...}; + Resource resource(Args&&... args) { + return Resource{shared_from_this(), std::forward(args)...}; } UVWError close() noexcept { diff --git a/src/uvw/prepare.hpp b/src/uvw/prepare.hpp index b2802a46..06ccc4c4 100644 --- a/src/uvw/prepare.hpp +++ b/src/uvw/prepare.hpp @@ -4,20 +4,20 @@ #include #include #include -#include "resource.hpp" +#include "handle.hpp" #include "util.hpp" namespace uvw { -class Prepare final: public Resource { +class Prepare final: public Handle { static void startCallback(Prepare &prepare, std::function &cb, uv_prepare_t *) { cb(UVWError{}, prepare); } explicit Prepare(std::shared_ptr ref) - : Resource{HandleType{}, std::move(ref)} + : Handle{HandleType{}, std::move(ref)} { initialized = (uv_prepare_init(parent(), get()) == 0); } diff --git a/src/uvw/stream.hpp b/src/uvw/stream.hpp index 41e3e1e6..a6324e36 100644 --- a/src/uvw/stream.hpp +++ b/src/uvw/stream.hpp @@ -2,7 +2,7 @@ #include -#include "resource.hpp" +#include "handle.hpp" #include "loop.hpp" @@ -10,7 +10,7 @@ namespace uvw { template -class Stream: public Resource { +class Stream: public Handle { static constexpr unsigned int DEFAULT_BACKLOG = 128; static void listenCallback(T &t, std::function &cb, uv_stream_t *, int status) { @@ -18,7 +18,7 @@ class Stream: public Resource { } protected: - using Resource::Resource; + using Handle::Handle; public: // TODO shutdown @@ -28,7 +28,7 @@ public: } void listen(int backlog, std::function cb) noexcept { - using CBF = typename Resource::template CallbackFactory; + using CBF = typename Handle::template CallbackFactory; auto func = CBF::template on<&Stream::listenCallback>(*static_cast(this), cb); auto err = uv_listen(this->template get(), backlog, func); if(err) { Stream::error(err); } diff --git a/src/uvw/timer.hpp b/src/uvw/timer.hpp index 21981816..d6270d6f 100644 --- a/src/uvw/timer.hpp +++ b/src/uvw/timer.hpp @@ -6,20 +6,20 @@ #include #include #include -#include "resource.hpp" +#include "handle.hpp" #include "util.hpp" namespace uvw { -class Timer final: public Resource { +class Timer final: public Handle { static void startCallback(Timer &timer, std::function &cb, uv_timer_t *) { cb(UVWError{}, timer); } explicit Timer(std::shared_ptr ref) - : Resource{HandleType{}, std::move(ref)} + : Handle{HandleType{}, std::move(ref)} { initialized = (uv_timer_init(parent(), get()) == 0); } diff --git a/test/main.cpp b/test/main.cpp index d6cc9923..d8a6cce0 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -3,14 +3,14 @@ void listen(uvw::Loop &loop) { - uvw::Handle handle = loop.handle(); + uvw::Resource resource = loop.resource(); auto cb = [](uvw::UVWError err, uvw::Tcp &srv) mutable { std::cout << "listen: " << ((bool)err) << std::endl; if(!err) { - uvw::Handle handle = srv.loop().handle(); - uvw::Tcp &client = handle; + uvw::Resource resource = srv.loop().resource(); + 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("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 handle = loop.handle(); + uvw::Resource resource = loop.resource(); 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(std::string{"127.0.0.1"}, 4242, cb); }