mirror of
https://github.com/zeromq/libzmq.git
synced 2025-02-20 22:31:34 +01:00
Problem: casts required due to void* arguments in a C++ class
Solution: introduce a type template argument
This commit is contained in:
parent
15b4f596a8
commit
5d5def40b5
@ -58,11 +58,11 @@ template <typename T> 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 <typename Arg>
|
||||
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 <typename T> class generic_mtrie_t
|
||||
bool rm (prefix_t prefix_, size_t size_, value_t *pipe_);
|
||||
|
||||
// Signal all the matching pipes.
|
||||
template <typename Arg>
|
||||
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 <typename Arg>
|
||||
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;
|
||||
|
@ -152,11 +152,12 @@ bool zmq::generic_mtrie_t<T>::add_helper (prefix_t prefix_,
|
||||
|
||||
|
||||
template <typename T>
|
||||
template <typename Arg>
|
||||
void zmq::generic_mtrie_t<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<T>::rm (value_t *pipe_,
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void zmq::generic_mtrie_t<T>::rm_helper (value_t *pipe_,
|
||||
template <typename Arg>
|
||||
void zmq::generic_mtrie_t<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<T>::rm_helper (prefix_t prefix_,
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename Arg>
|
||||
void zmq::generic_mtrie_t<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) {
|
||||
|
26
src/xpub.cpp
26
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -44,10 +44,9 @@ void test_create ()
|
||||
zmq::generic_mtrie_t<int> mtrie;
|
||||
}
|
||||
|
||||
void mtrie_count (int *pipe, void *arg)
|
||||
void mtrie_count (int *pipe, int *count)
|
||||
{
|
||||
LIBZMQ_UNUSED (pipe);
|
||||
int *count = static_cast<int *> (arg);
|
||||
++*count;
|
||||
}
|
||||
|
||||
@ -283,7 +282,7 @@ void test_add_multiple_reverse ()
|
||||
|
||||
zmq::generic_mtrie_t<int> 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<int>::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 <size_t N> void add_entries_rm_pipes_unique (const char *(&names)[N])
|
||||
@ -337,7 +336,7 @@ template <size_t N> 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<char *> (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<int>::prefix_t data_,
|
||||
size_t len_,
|
||||
void *void_count_)
|
||||
int *count_)
|
||||
{
|
||||
int *count = reinterpret_cast<int *> (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<int> &mtrie, int (&pipes)[2])
|
||||
|
Loading…
x
Reference in New Issue
Block a user