c++visual-studio-codewindows-10fltk

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();
};

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;
}

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

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.

Why won't FLTK accept my pointers? Have I declared them wrong?


Solution

  • FLTK callbacks are free functions( static or none-member functions). 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);
    

    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);
    

    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.