Change zmq_monitor_fn type to cast between pointer-to-object and pointer-to-function in a more standards compliant way

This commit is contained in:
Lourens Naudé 2012-05-20 18:16:23 +01:00
parent 5ef63318f8
commit 06cce15479
4 changed files with 14 additions and 11 deletions

View File

@ -434,7 +434,7 @@ Applicable socket types:: all listening sockets, when using TCP transports.
ZMQ_MONITOR: Registers a callback for socket state changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Registers a callback function / event sink for changes in underlying socket state.
Expected signature is `void (zmq_monitor_fn) (void *s, int event, zmq_event_data_t *data)`
Expected signature of function member of zmq_monitor_fn union is `void (*function)(void *s, int event, zmq_event_data_t *data);`
To remove the callback function call `zmq_setsockopt(socket, ZMQ_MONITOR, NULL, 0)`
The default value of `NULL` means no monitor callback function.
Supported events are :

View File

@ -300,7 +300,10 @@ typedef union {
} zmq_event_data_t;
/* Callback template for socket state changes */
typedef void (zmq_monitor_fn) (void *s, int event, zmq_event_data_t *data);
typedef union {
void *object;
void (*function)(void *s, int event, zmq_event_data_t *data);
} zmq_monitor_fn;
ZMQ_EXPORT void *zmq_socket (void *, int type);
ZMQ_EXPORT int zmq_close (void *s);

View File

@ -1030,7 +1030,7 @@ void zmq::socket_base_t::monitor_event (int event_, ...)
default:
zmq_assert (false);
}
options.monitor ((void *)this, event_, &data);
options.monitor->function ((void *)this, event_, &data);
va_end (args);
}
}

View File

@ -99,20 +99,20 @@ int main (int argc, char *argv [])
assert (rep);
// Expects failure - invalid size
zmq_monitor_fn *monitor;
monitor = listening_sock_monitor;
zmq_monitor_fn monitor;
monitor.function = listening_sock_monitor;
rc = zmq_setsockopt (rep, ZMQ_MONITOR, *(void **)&monitor, 20);
rc = zmq_setsockopt (rep, ZMQ_MONITOR, &monitor, 20);
assert (rc == -1);
assert (errno == EINVAL);
rc = zmq_setsockopt (rep, ZMQ_MONITOR, *(void **)&monitor, sizeof (void *));
rc = zmq_setsockopt (rep, ZMQ_MONITOR, &monitor, sizeof (void *));
assert (rc == 0);
size_t sz = sizeof (void *);
rc = zmq_getsockopt (rep, ZMQ_MONITOR, &monitor, &sz);
assert (rc == 0);
assert (monitor == listening_sock_monitor);
assert (monitor.function == listening_sock_monitor);
// Remove socket monitor callback
rc = zmq_setsockopt (rep, ZMQ_MONITOR, NULL, 0);
@ -120,7 +120,7 @@ int main (int argc, char *argv [])
rc = zmq_getsockopt (rep, ZMQ_MONITOR, &monitor, &sz);
assert (rc == 0);
assert (monitor == listening_sock_monitor);
assert (monitor.function == listening_sock_monitor);
rc = zmq_bind (rep, "tcp://127.0.0.1:5560");
assert (rc == 0);
@ -128,8 +128,8 @@ int main (int argc, char *argv [])
void *req = zmq_socket (ctx, ZMQ_REQ);
assert (req);
monitor = connecting_sock_monitor;
rc = zmq_setsockopt (req, ZMQ_MONITOR, *(void **)&monitor, sizeof (void *));
monitor.function = connecting_sock_monitor;
rc = zmq_setsockopt (req, ZMQ_MONITOR, &monitor, sizeof (void *));
assert (rc == 0);
rc = zmq_connect (req, "tcp://127.0.0.1:5560");