Search code examples
c++boost-bindpointer-to-member

c++: Can boost::bind be used to convert member function to expected function pointer signature?


I'm using a 3rd party library that passes function pointers to a method call.

class RTSPClient{
public:
...
  typedef void (responseHandler)(RTSPClient* rtspClient,
             int resultCode, char* resultString);
...
  unsigned sendOptionsCommand(responseHandler* responseHandler, 
             Authenticator* authenticator = NULL);
};

Normal usage looks as follows:

void continueAfterOPTIONS(RTSPClient* client, 
             int resultCode, char* resultString);
....
RTSPClient* pClient;
....
pClient->sendOptionsCommand(continueAfterOPTIONS, NULL);

Now I would like to make the continueAfterOPTIONS method a member function of a class. Usually I use boost::bind to do this:

pRtspClient>sendOptionsCommand(boost::bind(&RtspClientSessionManager::continueAfterOPTIONS, this), NULL);

resulting in

error C2664: 'RTSPClient::sendOptionsCommand' : cannot convert parameter 1 from 'boost::_bi::bind_t<R,F,L>' to 'RTSPClient::responseHandler (__cdecl *)'

I tried adding in place holders for the arguments of the function and that made no difference. Is what I'm trying to do possible? Is there perhaps a way to cast the bind result?

Thanks!


Solution

  • Not out of the box. However, let me sketch how you could do it.

    struct Foo : RTSPClient {
      boost::function<void(int resultCode, char* resultString)> bound;
      Foo(boost::function<void(int resultCode, char* resultString)> bound) : bound(bound) {}
    
      // Need a static method to get a regaulr function pointer
      static void CallBack(RTSPClient* _this, int resultCode, char* resultString) {
         _this->CallBack(int resultCode, char* resultString
      void CallBack(int resultCode, char* resultString) {
        bound();
      }
    };
    

    It's of course easier if you can derive your RtspClientSessionManager from RtspClient