连接未知类的成员函数以提升信号

时间:2017-02-21 14:56:31

标签: c++ boost signals member-function-pointers

我知道围绕这个问题有很多话题,但我无法弄清楚。

我正在为我的游戏工作一个信号驱动的输入管理器,因为我需要控制多个玩家,我需要将PlayerController实例的成员函数连接到我的信号向量。

问题是,不仅要有PlayerController,还要有MenuController等。所以我需要将MenuController的成员函数连接到信号上。

我很确定我接近解决方案,但我似乎无法弄明白。

有人可以帮我解决registerEvent函数签名以及如何调用connect方法。

InputManager.tcc

#include "boost/function.hpp"
template<class T>
void InputManager::registerEvent(SDL_Keycode key,KeyState state,boost::function<void ()> const& function)
{
    auto &inputEvents = (state == KeyState::Up) ? m_keyUpEvents : m_keyDownEvents;

    if(inputEvents.find(key) == inputEvents.end())
    {
        inputEvents.insert(std::make_pair(key, boost::signals2::signal<void()>()));
        m_keyStates[key] = KeyState::Up;
    }

    inputEvents[key].connect(boost::bind(T::function, instance));
}

InputManager.hpp:

#ifndef SSB_INPUTMANAGER_HPP
#define SSB_INPUTMANAGER_HPP

#include <functional>
#include <algorithm>
#include <vector>
#include <map>
#include <SDL_keycode.h>
#include <SDL_events.h>
#include <boost/signals2.hpp>

enum KeyState{
    Down,
    Up
};

class InputManager
{
public:
    InputManager();
    template<class T>
    void registerEvent(SDL_Keycode key,KeyState state, boost::function<void ()> const& function);
void pollEvent(SDL_Event event);

private:
    std::map<SDL_Keycode, boost::signals2::signal<void ()>> m_keyDownEvents;
    std::map<SDL_Keycode, boost::signals2::signal<void ()>> m_keyUpEvents;
    std::map<SDL_Keycode, KeyState> m_keyStates;
};

#include "InputManager.tcc"

#endif //SSB_INPUTMANAGER_HPP

PlayerController.hpp

#ifndef SSB_PLAYERCONTROLLER_HPP
#define SSB_PLAYERCONTROLLER_HPP

class PlayerController
{
public:
    void jump();
private:
    Player m_player;
};

#endif //SSB_PLAYERCONTROLLER_HPP

然后我想在游戏中的某个地方调用初始化方法:

PlayerController playerController;
InputManager inputController;

inputController.registerEvent(SDLK_0, KeyState::Down, playerController.jump());

1 个答案:

答案 0 :(得分:2)

boost::function<>std::function<>都已执行类型删除。这意味着他们“抽象”了任何绑定参数。

this*参数实际上只是 :参数。所以,同样适用。

<强> Live On Coliru (c++11)

#include <boost/function.hpp>
#include <boost/signals2.hpp>
#include <map>
#include <iostream>

enum SDL_Keycode { K_A, K_B, K_C, K_Up, K_Down, K_Right, K_Left, K_LCtrl, /*etc....*/ };
enum KeyState { Down, Up };

struct InputManager {
  void registerEvent(SDL_Keycode key, KeyState state, boost::function<void()> const &function) {
      auto& map = state == Up? m_keyUpEvents : m_keyDownEvents;
      map[key].connect(function);
  }

  void poll_event() {
      // hardcoded for demo
      m_keyDownEvents[K_B]();
  }

private:
  std::map<SDL_Keycode, boost::signals2::signal<void()>> m_keyDownEvents;
  std::map<SDL_Keycode, boost::signals2::signal<void()>> m_keyUpEvents;
  std::map<SDL_Keycode, KeyState> m_keyStates;
};

struct Player {};

class PlayerController {
public:
  void jump() {
      std::cout << "Player jumped\n";
  }

private:
  Player m_player;
};

class CowController {
public:
  void moo() {
      std::cout << "Cow mooed\n";
  }
};

int main() {
    InputManager inputController;
    PlayerController p;

    inputController.poll_event(); // nothing

    inputController.registerEvent(K_B, Down, [&] { p.jump(); });

    inputController.poll_event(); // player jumps

    CowController c;
    inputController.registerEvent(K_B, Down, [&] { c.moo(); });

    inputController.poll_event(); // player jumps, cow moos
}

打印

Player jumped
Player jumped
Cow mooed

C ++ 03

如果您没有lambdas,可以使用Boost Bind(或std::tr1::bind):

inputController.registerEvent(K_B, Down, boost::bind(&PlayerController::jump, boost::ref(p)));

inputController.poll_event(); // player jumps

CowController c;
inputController.registerEvent(K_B, Down, boost::bind(&CowController::moo, boost::ref(c)));

查看 Live On Coliru