FLTK 回调不接受我的函数指针

FLTK callbacks won't accept my function pointers

所以我有这个 class 它应该包含我的 window 和我将用作回调的所有函数。

#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Group.H>
#include <FL/Fl_Value_Input.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Slider.H>
#include <FL/Fl_Check_Button.H>
#include "world.hpp"

class Controller
{
    private:
        Fl_Double_Window *window;
    public:
        int process(int argc, char **argv);
        void nextStep(Fl_Widget *widget, void *data);
        void togglePlay(Fl_Widget *widget, void *data);
        void newWorld(Fl_Widget *widget, void *data);
        void randFill(Fl_Widget *widget, void *data);
        void togglePreviz(Fl_Widget *widget, void *data);
        void updateSpeed(Fl_Widget *widget, void *data);
        Fl_Double_Window* make_window();
};

现在在 "make_window()" 的实现中(最初是使用 FLUID 创建的)我想将我的功能分配给适当的小部件。

Fl_Double_Window* Controller::make_window() 
{
    //Generated with FLUID designer
    Fl_Double_Window* w;
    { Fl_Double_Window* o = new Fl_Double_Window(1200, 900);
    w = o; if (w) {/* empty */}
    o->color(FL_FOREGROUND_COLOR);
    { World* o = new World(25, 25, 800, 800);
      o->box(FL_DOWN_BOX);
      o->color((Fl_Color)30);
      o->end();
    } // Fl_Group* o
    { Fl_Group* o = new Fl_Group(850, 25, 300, 805);
      o->box(FL_DOWN_BOX);
      o->color(FL_INACTIVE_COLOR);
      { Fl_Value_Input* o = new Fl_Value_Input(875, 50, 100, 24, "Cellules");
        o->align(Fl_Align(FL_ALIGN_RIGHT));
      } // Fl_Value_Input* o
      { Fl_Button* o = new Fl_Button(900, 100, 200, 40, "Creation Monde");
        o->box(FL_ROUNDED_BOX);
        o->callback(newWorld);
      } // Fl_Button* o
      { Fl_Slider* o = new Fl_Slider(FL_HOR_NICE_SLIDER, 900, 175, 200, 25, "Vitesse");
        o->callback(updateSpeed);
        o->align(Fl_Align(FL_ALIGN_TOP));
        o->bounds(0.0, 1.0);
      } // Fl_Slider* o
      { Fl_Check_Button* o = new Fl_Check_Button(875, 225, 64, 20, "Mode PreViz");
        o->callback(togglePreviz);
        o->down_box(FL_DOWN_BOX);
      } // Fl_Check_Button* o
      { Fl_Value_Input* o = new Fl_Value_Input(875, 300, 100, 24, "Taux");
        o->align(Fl_Align(FL_ALIGN_RIGHT));
      } // Fl_Value_Input* o
      { Fl_Button* o = new Fl_Button(900, 350, 200, 40, "Remplissage Aleatoire");
        o->callback(randFill);
        o->box(FL_ROUNDED_BOX);
      } // Fl_Button* o
      { Fl_Button* o = new Fl_Button(950, 500, 100, 100, "@>");
        o->callback(togglePlay);
        o->box(FL_ROUNDED_BOX);
        o->labelsize(50);
      } // Fl_Button* o
      { Fl_Button* o = new Fl_Button(950, 650, 100, 100, "@->");
        o->callback(nextStep);
        o->box(FL_ROUNDED_BOX);
        o->labelsize(50);
      } // Fl_Button* o
      o->end();
    } // Fl_Group* o
    o->end();
  } // Fl_Double_Window* o
  return w;
}

问题是,在我调用 "o->callback(myFunctionPointer);" 的每一行,我都收到一条错误消息,告诉我

no instance of overloaded function "Fl_Button::callback" matches the argument list -- argument types are: (void (Fl_Widget *widget, void *data)) -- object type is: Fl_Button

根据我的研究,这应该意味着我没有在我的 "callback" 函数中发送适当数量或类型的参数。在深入研究文档后,我发现 "callback" 函数至少需要一个指向 Fl_callback 对象的指针,但我看到了人们只发送一个函数指针的代码示例,它似乎在他们的代码上工作得很好。

为什么 FLTK 不接受我的指点?我宣布他们错了吗?

如有任何帮助,我们将不胜感激

谢谢

FLTK 回调是自由函数(静态或 none-成员函数)。您正在传递实例成员函数。如果不需要 this 参数,您可以声明您的函数 static。或者安装一些临时回调管理器(假设支持 c++17):

typedef void fltk_cb(Fl_Widget *, void*);

void Controller::nextStep(Fl_Widget *);

template<auto cb, typename cb_type=decltype(cb)>
struct mem_cb{
static_assert(std::is_same_v<cb_type, void Controller::(*)(Fl_Widget *)>,"incompatible type");
    constexpr operator fltk_cb*()const{return &static_cb;};
    static void static_cb(Fl_Widget * w, void * that){
        (reinterpret_cast<Controller*>(that)->*cb)(w);
    };
};

//...
 o->callback(mem_cb<&nextStep>{},this);

c++17 之前的语法可能很笨拙。使用 std::functionboost::signal2 以及更复杂一点的方法可以实现更好的临时回调管理。但我想你只需要一些启动器。 一个较短的解决方案是 none-capture lambda:

void Controller::nextStep(Fl_Widget *);

o->callback(
    [](Fl_Widget *w, void *that){
        reinterpret_cast<Controller*>(that)->nextStep(w);
    }
,this);

a none-捕获 lambda - 按照惯例 - 衰减为具有兼容签名的自由函数指针。此解决方案更简单,但模板将潜在风险转换封装在 class 中,以减少出现错误的机会。

每个回调一个静态是我们在 C++08、lambda 和未来版本出现之前的做法

{ Fl_Check_Button* o = new Fl_Check_Button(875, 225, 64, 20, "Mode PreViz");
   o->callback(_togglePreviz, this);
   o->down_box(FL_DOWN_BOX);
} // Fl_Check_Button* o

static void _togglePreviz(Widget* w, void* me)
{
    Fl_Check_Button* cb = dynamic_cast<Fl_Check_Button*>(w);
    Controller* self = reinterpret_cast<Controller*>(me);
    self->togglePreviz(cb);
}

void togglePreviz(Fl_Check_Button* cb)
{
     ....
}