I'm using the C libstrophe library to make an xmpp application in C++11. I'm trying to register message handlers for specific IDs, so I can recognize a specific return message, using xmpp_id_handler_add.
void xmpp_id_handler_add(xmpp_conn_t * const conn,
xmpp_handler handler,
const char * const id,
void * const userdata)
But there's something about strophe's implementation of this that I don't understand.
Strophe will only accept a function pointer of the form
typedef int (*xmpp_handler)(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza,
void * const userdata);
That's easy enough to do with a static function, but looking through the source code I find this
/* check if handler is already in the list */
item = (xmpp_handlist_t *)hash_get(conn->id_handlers, id);
while (item) {
if (item->handler == (void *)handler)
break;
item = item->next;
}
if (item) return;
Meaning that if I try to call xmpp_id_handler_add twice with the same static function, but a different id and userdata, it will reject the second call.
So I thought that maybe I could make a lambda every time I want to add a new ID handler, like so
auto msgHandler = [](xmpp_conn_t* const pConn,
xmpp_stanza_t* const pStanza,
void* const pUserdata) -> int
But when I looked at the pointer value of the lambda
printf("%p\n", (void*)((xmpp_handler)msgHandler));
And ran it twice, I got the same value both times. It seems the lambda is just like a static function in this case.
So, how can I make a new, unique function pointer every time I want to listen for a new ID? Alternately, am I misunderstanding how libstrophe is supposed to be used? Are you supposed to have a new static function for each new ID you want to listen for?
Same problem has already been mentioned in the libstrophe issue tracker: https://github.com/strophe/libstrophe/issues/97. There is a patch in separated branch which is going to be merged to the master branch. Therefore, minor release
0.9.2
will contain it. The patch allows to add unique pairs ofhandler
plususerdata
instead of onlyhandler
.