std :: bind to void * to std :: function

问题描述 投票:2回答:1

我正在尝试创建一个事件系统该系统的用户必须执行此操作才能添加一个输入事件,例如:

addEventHandler(Event::KEYBOARD_INPUT, reinterpret_cast<void*>(&std::bind(&Game::On_Input, this)));

传递的函数将在事件被触发时调用,这些函数存储在std :: vectors中,直到现在没有问题。但是不同的事件有不同的函数签名..而且因为std :: function是一个类型本身我不能让这个函数接受我试图将std :: bind转换为void *的所有事件然后将它强制转换回std :: function但由于impl未定义(0xCCCCCCCC)它不会工作

目前的代码:

void EventManager::addEventHandler(Event event, void* p_Fun)
{
    if (event == Event::KEYBOARD_INPUT) {
        m_InputCallbacks.push_back(*reinterpret_cast<std::function<void(Keycode, unsigned int)>*>(p_Fun));
    }
    /*else if(...){ // other events other push backs in other vectors

    }*/
}

以这种方式调用存储的函数:

void EventManager::HandleEvents(SDL_Event& Event)
{
    while (PollEvent(&Event) != 0) {
        if (Event.type == KEYDOWN || Event.type == KEYUP) {
            //On_Input(Event.key.keysym.sym, Event.type);
            for (auto inputCallbackFunc : m_InputCallbacks) {
                inputCallbackFunc(Event.key.keysym.sym, Event.type);//CRASH HERE!
            }
        }
    }
}

请注意,这个版本只处理输入我不知道将来会添加多少事件(碰撞,多人连接等等)这就是为什么我想通过addEventHandler函数自动化进程,这将检查以下内容:1)什么是事件类型并将void函数指针强制转换为该事件回调函数签名2)将它们存储在适当的std :: vector中

总而言之,如何使AddEventHandler接受传递给它的每个std :: bind,以后可以存储和调用它?

c++ c++11 void-pointers std-function stdbind
1个回答
2
投票

std::function提供了存储单一类型的std::vector所需的所有类型擦除,该类型具有您将使用的签名。没有必要投掷到void*

为了简单起见,这样的事情就是你想要的。

#include <iostream>
#include <vector>
#include <functional>
#include <type_traits>
using namespace std;

enum Event
{
    KEYBOARD_INPUT,
    MOUSE_INPUT
};


std::vector< std::function<void(int,int)> > keyboard_handlers;
std::vector< std::function<void(int,int,int)> > mouse_handlers;



template <Event EventType,typename Func,
     typename std::enable_if<EventType==KEYBOARD_INPUT,bool>::type=true>
void addEventHandler(Func&& func)
{
keyboard_handlers.push_back(
        std::function<void(int,int)>(std::forward<Func>(func)));
}

template <Event EventType,typename Func,
     typename std::enable_if<EventType==MOUSE_INPUT,bool>::type=true>
void addEventHandler(Func&& func)
{
mouse_handlers.push_back(
        std::function<void(int,int,int)>(std::forward<Func>(func)));
}

void call_keyboard_handlers(int a,int b)
{
    for (auto inputCallbackFunc : keyboard_handlers) {
        inputCallbackFunc(a,b);
    }
}


void keyboard_callback_1(std::string name, int a , int b)
{
    std::cout << "keyboard_callback_1 " << name << " " << a << " " << b << std::endl;
}

void keyboard_callback_2(int a , int b)
{
    std::cout << "keyboard_callback_2 "  << a << " " << b << std::endl;
}

struct keyboard_callback_3_type
{
    void operator()(int a,int b)
    {
            std::cout << "keyboard_callback_3 "  << a << " " << b << std::endl;
    }
};

int main() {
    //With an extra bound in string argument.
    addEventHandler<KEYBOARD_INPUT>(std::bind(&keyboard_callback_1,"somestring",std::placeholders::_1,std::placeholders::_2));
    //From a plain function pointer 
    addEventHandler<KEYBOARD_INPUT>(&keyboard_callback_2);

    //From a memberfunction pointer
    keyboard_callback_3_type keyboard_callback_3;
    addEventHandler<KEYBOARD_INPUT>(std::bind(&keyboard_callback_3_type::operator(),&keyboard_callback_3,std::placeholders::_1,std::placeholders::_2));

    //From a non capturing lambda
    addEventHandler<KEYBOARD_INPUT>([](int a,int b){ std::cout << "lambda_callback " << a << " " <<  b <<std::endl;});

    std::string capture = "Captured";
    //From a capturing lambda
    addEventHandler<KEYBOARD_INPUT>([&](int a,int b){ std::cout << "lambda_callback " << capture << " " << a << " " <<  b <<std::endl;});


    call_keyboard_handlers(10,20);

    return 0;
}

Demo

注意,如果要将函数名称保留为addEventHandler,则必须根据事件类型启用函数的不同变体(如示例中所示),或者以不同方式命名它们(即addKeyboardEventHandler,addMouseEventHandler等...)

© www.soinside.com 2019 - 2024. All rights reserved.