diff --git a/src/generic_mtrie.hpp b/src/generic_mtrie.hpp index b0c36041..f11533fd 100644 --- a/src/generic_mtrie.hpp +++ b/src/generic_mtrie.hpp @@ -58,11 +58,11 @@ template class generic_mtrie_t // The call_on_uniq_ flag controls if the callback is invoked // when there are no subscriptions left on a topic only (true) // or on every removal (false). - void - rm (value_t *pipe_, - void (*func_) (const unsigned char *data_, size_t size_, void *arg_), - void *arg_, - bool call_on_uniq_); + template + void rm (value_t *pipe_, + void (*func_) (const unsigned char *data_, size_t size_, Arg arg_), + Arg arg_, + bool call_on_uniq_); // Remove specific subscription from the trie. Return true if it was // actually removed rather than de-duplicated. @@ -71,19 +71,21 @@ template class generic_mtrie_t bool rm (prefix_t prefix_, size_t size_, value_t *pipe_); // Signal all the matching pipes. + template void match (prefix_t data_, size_t size_, - void (*func_) (value_t *pipe_, void *arg_), - void *arg_); + void (*func_) (value_t *pipe_, Arg arg_), + Arg arg_); private: bool add_helper (prefix_t prefix_, size_t size_, value_t *pipe_); + template void rm_helper (value_t *pipe_, unsigned char **buff_, size_t buffsize_, size_t maxbuffsize_, - void (*func_) (prefix_t data_, size_t size_, void *arg_), - void *arg_, + void (*func_) (prefix_t data_, size_t size_, Arg arg_), + Arg arg_, bool call_on_uniq_); bool rm_helper (prefix_t prefix_, size_t size_, value_t *pipe_); bool is_redundant () const; diff --git a/src/generic_mtrie_impl.hpp b/src/generic_mtrie_impl.hpp index c28f837c..1b293b45 100644 --- a/src/generic_mtrie_impl.hpp +++ b/src/generic_mtrie_impl.hpp @@ -152,11 +152,12 @@ bool zmq::generic_mtrie_t::add_helper (prefix_t prefix_, template +template void zmq::generic_mtrie_t::rm (value_t *pipe_, void (*func_) (prefix_t data_, size_t size_, - void *arg_), - void *arg_, + Arg arg_), + Arg arg_, bool call_on_uniq_) { unsigned char *buff = NULL; @@ -165,14 +166,15 @@ void zmq::generic_mtrie_t::rm (value_t *pipe_, } template -void zmq::generic_mtrie_t::rm_helper (value_t *pipe_, +template +void zmq::generic_mtrie_t::rm_helper(value_t *pipe_, unsigned char **buff_, size_t buffsize_, size_t maxbuffsize_, void (*func_) (prefix_t data_, size_t size_, - void *arg_), - void *arg_, + Arg arg_), + Arg arg_, bool call_on_uniq_) { // Remove the subscription from this node. @@ -405,10 +407,11 @@ bool zmq::generic_mtrie_t::rm_helper (prefix_t prefix_, } template +template void zmq::generic_mtrie_t::match (prefix_t data_, size_t size_, - void (*func_) (value_t *pipe_, void *arg_), - void *arg_) + void (*func_) (value_t *pipe_, Arg arg_), + Arg arg_) { generic_mtrie_t *current = this; while (true) { diff --git a/src/xpub.cpp b/src/xpub.cpp index 004f3d4c..74cd9167 100644 --- a/src/xpub.cpp +++ b/src/xpub.cpp @@ -35,6 +35,7 @@ #include "err.hpp" #include "msg.hpp" #include "macros.hpp" +#include "generic_mtrie_impl.hpp" zmq::xpub_t::xpub_t (class ctx_t *parent_, uint32_t tid_, int sid_) : socket_base_t (parent_, tid_, sid_), @@ -204,7 +205,7 @@ void zmq::xpub_t::xpipe_terminated (pipe_t *pipe_) // Remove pipe without actually sending the message as it was taken // care of by the manual call above. subscriptions is the real mtrie, // so the pipe must be removed from there or it will be left over. - subscriptions.rm (pipe_, stub, NULL, false); + subscriptions.rm (pipe_, stub, (void *) NULL, false); } else { // Remove the pipe from the trie. If there are topics that nobody // is interested in anymore, send corresponding unsubscriptions @@ -215,10 +216,9 @@ void zmq::xpub_t::xpipe_terminated (pipe_t *pipe_) dist.pipe_terminated (pipe_); } -void zmq::xpub_t::mark_as_matching (pipe_t *pipe_, void *arg_) +void zmq::xpub_t::mark_as_matching (pipe_t *pipe_, xpub_t *self_) { - xpub_t *self = (xpub_t *) arg_; - self->dist.match (pipe_); + self_->dist.match (pipe_); } int zmq::xpub_t::xsend (msg_t *msg_) @@ -297,24 +297,22 @@ bool zmq::xpub_t::xhas_in () void zmq::xpub_t::send_unsubscription (zmq::mtrie_t::prefix_t data_, size_t size_, - void *arg_) + xpub_t *self_) { - xpub_t *self = (xpub_t *) arg_; - - if (self->options.type != ZMQ_PUB) { + if (self_->options.type != ZMQ_PUB) { // Place the unsubscription to the queue of pending (un)subscriptions // to be retrieved by the user later on. blob_t unsub (size_ + 1); *unsub.data () = 0; if (size_ > 0) memcpy (unsub.data () + 1, data_, size_); - self->pending_data.ZMQ_PUSH_OR_EMPLACE_BACK (ZMQ_MOVE (unsub)); - self->pending_metadata.push_back (NULL); - self->pending_flags.push_back (0); + self_->pending_data.ZMQ_PUSH_OR_EMPLACE_BACK (ZMQ_MOVE (unsub)); + self_->pending_metadata.push_back (NULL); + self_->pending_flags.push_back (0); - if (self->manual) { - self->last_pipe = NULL; - self->pending_pipes.push_back (NULL); + if (self_->manual) { + self_->last_pipe = NULL; + self_->pending_pipes.push_back (NULL); } } } diff --git a/src/xpub.hpp b/src/xpub.hpp index 0c18f412..c3f0d6cc 100644 --- a/src/xpub.hpp +++ b/src/xpub.hpp @@ -68,10 +68,10 @@ class xpub_t : public socket_base_t // upstream. static void send_unsubscription (zmq::mtrie_t::prefix_t data_, size_t size_, - void *arg_); + xpub_t *self_); // Function to be applied to each matching pipes. - static void mark_as_matching (zmq::pipe_t *pipe_, void *arg_); + static void mark_as_matching (zmq::pipe_t *pipe_, xpub_t *arg_); // List of all subscriptions mapped to corresponding pipes. mtrie_t subscriptions; diff --git a/unittests/unittest_mtrie.cpp b/unittests/unittest_mtrie.cpp index a2247834..af46e5d6 100644 --- a/unittests/unittest_mtrie.cpp +++ b/unittests/unittest_mtrie.cpp @@ -44,10 +44,9 @@ void test_create () zmq::generic_mtrie_t mtrie; } -void mtrie_count (int *pipe, void *arg) +void mtrie_count (int *pipe, int *count) { LIBZMQ_UNUSED (pipe); - int *count = static_cast (arg); ++*count; } @@ -283,7 +282,7 @@ void test_add_multiple_reverse () zmq::generic_mtrie_t mtrie; for (int i = 2; i >= 0; --i) { - add_indexed_expect_unique (mtrie, pipes, names, (size_t)i); + add_indexed_expect_unique (mtrie, pipes, names, (size_t) i); } for (size_t i = 0; i < 3; ++i) { @@ -324,10 +323,10 @@ void test_rm_multiple_reverse_order () void check_name (zmq::generic_mtrie_t::prefix_t data_, size_t len_, - void *void_name_) + const char *name_) { - TEST_ASSERT_EQUAL_UINT (strlen ((char *) void_name_), len_); - TEST_ASSERT_EQUAL_STRING_LEN (void_name_, data_, len_); + TEST_ASSERT_EQUAL_UINT (strlen (name_), len_); + TEST_ASSERT_EQUAL_STRING_LEN (name_, data_, len_); } template void add_entries_rm_pipes_unique (const char *(&names)[N]) @@ -337,7 +336,7 @@ template void add_entries_rm_pipes_unique (const char *(&names)[N]) add_entries (mtrie, pipes, names); for (size_t i = 0; i < N; ++i) { - mtrie.rm (&pipes[i], check_name, const_cast (names[i]), false); + mtrie.rm (&pipes[i], check_name, names[i], false); } } @@ -357,11 +356,10 @@ void test_rm_with_callback_multiple_reverse_order () void check_count (zmq::generic_mtrie_t::prefix_t data_, size_t len_, - void *void_count_) + int *count_) { - int *count = reinterpret_cast (void_count_); - --count; - TEST_ASSERT_GREATER_OR_EQUAL (0, count); + --count_; + TEST_ASSERT_GREATER_OR_EQUAL (0, count_); } void add_duplicate_entry (zmq::generic_mtrie_t &mtrie, int (&pipes)[2])