From 4dde37e7b22de831f724c0c387556aab221724d5 Mon Sep 17 00:00:00 2001 From: Pawel Kurdybacha Date: Tue, 8 May 2018 21:56:15 +0100 Subject: [PATCH] Make poller default movable Latest modification to the poller made move constructor and move assigment operator not complete. In order to prevent that in the future poller should be default movable. Unique pointer has been used to manager zmq_poller. That makes code simpler and safer now. --- zmq.hpp | 44 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/zmq.hpp b/zmq.hpp index f2efacd..8145324 100644 --- a/zmq.hpp +++ b/zmq.hpp @@ -1018,36 +1018,19 @@ namespace zmq class poller_t { public: - poller_t () : poller_ptr (zmq_poller_new ()) + poller_t () { if (!poller_ptr) throw error_t (); } - ~poller_t () - { - if (poller_ptr) - { - int rc = zmq_poller_destroy (&poller_ptr); - assert(rc == 0); - } - } + ~poller_t () = default; poller_t(const poller_t&) = delete; poller_t &operator=(const poller_t&) = delete; - poller_t(poller_t&& src) - : poller_ptr(src.poller_ptr) - , poller_events(std::move (src.poller_events)) - { - src.poller_ptr = NULL; - } - poller_t &operator=(poller_t&& src) - { - poller_ptr = src.poller_ptr; - poller_events = std::move (src.poller_events); - src.poller_ptr = NULL; - return *this; - } + + poller_t(poller_t&& src) = default; + poller_t &operator=(poller_t&& src) = default; using handler_t = std::function; @@ -1056,7 +1039,7 @@ namespace zmq auto it = std::end (handlers); auto inserted = false; std::tie(it, inserted) = handlers.emplace (socket.ptr, std::make_shared (std::move (handler))); - if (0 == zmq_poller_add (poller_ptr, socket.ptr, inserted && *(it->second) ? it->second.get() : nullptr, events)) { + if (0 == zmq_poller_add (poller_ptr.get (), socket.ptr, inserted && *(it->second) ? it->second.get() : nullptr, events)) { need_rebuild = true; return; } @@ -1068,7 +1051,7 @@ namespace zmq void remove (zmq::socket_t &socket) { - if (0 == zmq_poller_remove (poller_ptr, socket.ptr)) { + if (0 == zmq_poller_remove (poller_ptr.get (), socket.ptr)) { handlers.erase (socket.ptr); need_rebuild = true; return; @@ -1078,7 +1061,7 @@ namespace zmq void modify (zmq::socket_t &socket, short events) { - if (0 != zmq_poller_modify (poller_ptr, socket.ptr, events)) + if (0 != zmq_poller_modify (poller_ptr.get (), socket.ptr, events)) throw error_t (); } @@ -1095,7 +1078,7 @@ namespace zmq } need_rebuild = false; } - int rc = zmq_poller_wait_all (poller_ptr, poller_events.data (), + int rc = zmq_poller_wait_all (poller_ptr.get (), poller_events.data (), static_cast (poller_events.size ()), static_cast(timeout.count ())); if (rc > 0) { @@ -1117,7 +1100,14 @@ namespace zmq } private: - void *poller_ptr {nullptr}; + std::unique_ptr> poller_ptr + { + zmq_poller_new (), + [](void *ptr) { + int rc = zmq_poller_destroy (&ptr); + ZMQ_ASSERT (rc == 0); + } + }; bool need_rebuild {false}; std::unordered_map> handlers {}; std::vector poller_events {};