c++: How to obtain context when callback doesn'

2020-07-13 07:33发布

问题:

First, some background:
(Note: Though I'm in non-.NET Win32 land, this is really a C++ question)

I'm using a 3rd party API which requires you to register a callback function in order to know when an async operation is complete. Gotta use the callback, no way around it.

A non-OOP implementation would be something like this:

void __stdcall MyCbFcn(int value)
{
   do something with 'value'...
}

API_RegisterCallback(MyCbFcn);

Pretty standard stuff.

BUT...
My code is OOP, with multiple instances rx'ing the callback, thus the callback needs to be routed to the object that registered it.

Knowing that folks do this, callbacks typically include a user var, something like:

void __stdcall MyCbFcn(int value, U32 user)
{
   do something with 'value'...
}

API_RegisterCallback(MyCbFcn, someUserValue);

and more specifically, when combined with OOP, this user arg allows you to get back into context:
(written inline for brevity):

class MyClass
{
public:
   MyClass()
   {
      API_RegisterCallback(MyClass::StaticCbFcn, (U32)this);
   }

private:
   static void __stdcall StaticCbFcn(int value, U32 user)
   {
      MyClass* pThis = (MyClass*)user;
      pThis->InstanceCbFcn(value);
   }
   void InstanceCbFcn(int value)
   {
      ... do some work in context ...
   }
}

BUT, my API doesn't feature a user arg :(

So now my question:
How I can get back into context?

I've considered kinda sketchy things like defining a "pool" of 100 distinct callbacks and assigning them as objects are created, but that seems like a real hack.

An obvious solution ... if I were in e.g. JavaScript :) ... would be to use an anonymous function, but AFAIK C++ doesn't have anything like that.

Any ideas would be appreciated.

回答1:

"100 distinct callbacks" is really the only thing you can do, thus you use the function address as identifying parameter. It might help to implement the different functions as template with a constant parameter:

template < unsinged N >
void StaticCbFcn( int value )
{
    map[ N ].InstanceCbFcn( value );
}