Making a C style callback object oriented?

c++, liblacewing

Solution

Since you're using liblacewing, every class has a `void * Tag` member provided for user data:

/* class method */

void MyServer::onReceive (Lacewing::Server &Server, Lacewing::Server::Client &Client,
            char * Data, int Size)
{
     /* callback body - this is inside the class */
}


/* global function wraps the class method */

void onReceive (Lacewing::Server &Server, Lacewing::Server::Client &Client,
            char * Data, int Size)
{
    ((MyServer *) Server.Tag)->onReceive (Server, Client, Data, Size);
}

then:

Server.Tag = myServerInstance; /* set the class instance pointer */
Server.onReceive (::onReceive); /* register the global function */

Problem

I'm using a library that has callbacks like this: ``` void onReceive (Lacewing::Server &Server, Lacewing::Server::Client &Client, char * Data, int Size) { /* callback body */ } Server.onReceive (onReceive); /* to register the handler */ ``` I would like to be able to wrap this in a class that can decide what to do when it receives a packet (observer pattern). How can I do this with C style callbacks? The library does not define an interface to inherit from. Thanks

Original source