vendredi 19 avril 2019

How to pass function pointer to bind?

I have the following code:

_createNewObjectlistener = eventDispatcher->addCustomEventListener(Constants::EVENT_CREATE_OBJECT, std::bind(&ObjectPlacementManager::receiveCreateObjectEvent, this, std::placeholders::_1));
_eventListeners.insert(_createNewObjectlistener);

_moveNewObjectlistener = eventDispatcher->addCustomEventListener(Constants::EVENT_MOVE_NEW_OBJECT, std::bind(&ObjectPlacementManager::receiveMoveCurrentGameObjectEvent, this, std::placeholders::_1));
_eventListeners.insert(_moveNewObjectlistener);

.... many more listeners created

Since the only difference between the creation code for each listener, is the Constant::EVENT_NAME and the function being called, I am trying to encapsulate it into a function.

The result of bind must be of type const std::function<void(EventCustom*)>&

The functions like ObjectPlacementManager::receiveMoveCurrentGameObjectEvent all have the same signature:

void receiveMoveCurrentGameObjectEvent(EventCustom* event){
 ....
}

I have tried: How to pass argument to std::bind to a function?

But the error I get is:

'No viable conversion' from....

I have also tried making a function:

EventListenerCustom* createCustomListener(void* func, std::string EVENT){
    auto eventDispatcher = _dragNode->getEventDispatcher();
    auto listener = eventDispatcher->addCustomEventListener(EVENT, std::bind(func, this, std::placeholders::_1));
    return listener;
}

But the error I get is:

No viable conversion from '__bind<void *&, mynamespace:: ObjectPlacementManager *, const std::__1::placeholders::__ph<1> &>' to 'const std::function<void (EventCustom *)>'

Aucun commentaire:

Enregistrer un commentaire