Ralf
Ralf

Reputation: 9573

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!

Upvotes: 1

Views: 891

Answers (3)

MSalters
MSalters

Reputation: 179819

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

Upvotes: 3

You cannot do it that way. The bind library creates function objects, not real functions, and the type of the generated pointers will differ.

Upvotes: 1

UncleBens
UncleBens

Reputation: 41331

boost::bind produces a function object which is a completely different thing than a pointer-to-function. It is an object with overloaded operator().

I'd say it can't be used here.

Upvotes: 2

Related Questions