FLTK 回调不会接受我的函数指针 [英] FLTK callbacks won't accept my function pointers
问题描述
所以我有这门课它应该包含我的窗口和我将用作回调的所有函数.
So I have this class which is supposed to contain my window and all the functions that I'm going to use as callbacks.
#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 创建)的实现中,我想将我的功能分配给适当的小部件.
Now in the implementation of "make_window()" (which was originally created using FLUID) I want to assign my functions to the appropriate widgets.
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);"的每一行我收到一个错误,告诉我
The problem is that at every line where I call "o->callback(myFunctionPointer);" I get an error telling me that
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
根据我的研究,这应该意味着我没有在回调"函数中发送适当数量或类型的参数.在深入研究文档后,我发现回调"函数至少需要一个指向 Fl_callback 对象的指针,但我已经看到人们只是发送一个函数指针的代码示例,它似乎在他们的代码上工作得很好.
From what I've researched it should mean that I don't send the appropriate number or type of parameters in my "callback" function. After digging in the documentation, I've found that the "callback" function expects at least a pointer to a Fl_callback object but I've seen examples of code where people just send a function pointer and it seems to work just fine on their code.
为什么 FLTK 不接受我的指示?我宣布他们错了吗?
Why won't FLTK accept my pointers ? Have I declared them wrong ?
任何帮助将不胜感激
谢谢
推荐答案
FLTK 回调是自由函数(静态或非成员函数).您正在传递实例成员函数.如果不需要 this
参数,您可以声明您的函数 static
.或者安装一些特别的回调管理器(假设支持 c++17):
FLTK callbacks are free functions( static or none-member fuunctions ). You are passing instance member functions. If the this
parameter is not needed you can declare your functions static
. Or install some ad-hoc callback manager(assuming c++17 support):
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::function
或 boost::signal2
和稍微复杂一点的更好的临时回调管理可以实现.但我想你只需要一些开胃菜.更短的解决方案是不捕获 lambda:
prior to c++17 syntax can be awkward. Using std::function
or boost::signal2
and a little bit of more complication better ad-hoc callback management can be achieved. But I guess you just need some starter.
A shorter solution is none-capture lambda:
void Controller::nextStep(Fl_Widget *);
o->callback(
[](Fl_Widget *w, void *that){
reinterpret_cast<Controller*>(that)->nextStep(w);
}
,this);
非捕获 lambda - 按照惯例 - 衰减为具有兼容签名的自由函数指针.这个解决方案更简单,但模板将潜在风险的转换封装在一个类中,以减少出现错误的机会.
a none-capture lambda - by convention - decays to a free function pointer with compatible signature. This solution is simpler but the template one encapsulates the potentially risky cast in a class in order to decrease the chances for bugs.
这篇关于FLTK 回调不会接受我的函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!