使用回调作为参数

时间:2014-12-13 13:05:28

标签: c++ opengl

我有以下课程:

class Timer
{
public:
    Timer(){};
    ~Timer(){};

    void timer(int);
    //...

private:
    //...
};

我的函数timer(int value)是我在glutTimerFunc()中使用的回调,在函数timer(int value)中我需要再次使用函数timer,如下所示:

void Timer::timer(int value)
{
    //...

    glutTimerFunc(state->getTimer(), this->timer, 0);
}

如何在不使用静态功能的情况下执行此操作?

1 个答案:

答案 0 :(得分:0)

你需要一个全局调度程序,将传递给int的{​​{1}}转换为c ++回调(成员函数,lambda等)

类似这样的事情

glutTimerFunc

现在使用如下:

struct timer_dispatch
{
  using callback_t = std::function<void()>;

  int start_timer(int msecs, callback_t callback) {
    std::unique_lock<std::mutex> lock(_mutex);
    int ident = _next_id++;
    _callbacks.emplace(ident, std::move(callback));
    glutTimerFunc(msecs, &timer_dispatch::dispatch_timer, ident);
    return ident;
  }

  // implement similar function for stop timer - don't forget the mutex
  void stop_timer(int ident) {
    std::unique_lock<std::mutex> lock(_mutex);
    _callbacks.erase(ident);
  }

  static timer_dispatch& instance() {
    static timer_dispatch _;
    return _;
  }  

private:
  // private constructor ensures use via the instance() static method;
  timer_dispatch() = default; 
  static void dispatch_timer(int ident) {
    auto self = instance();
    std::unique_lock<std::mutex> lock(self._mutex);
    auto it = self._callbacks.find(ident);
    if (it != self._callbacks.end()) {
      auto my_copy = std::move(it->second);
      self._callbacks.erase(it);
      lock.unlock();
      my_copy();
    }
  }

private:      
  std::unordered_map<int, callback_t> _callbacks;
  std::mutex _mutex;
  int _next_id = 0;
};