FLTK callbacks won't accept my function pointers
Asked Answered
H

2

0

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 ?

Any help would be appreciated

Thanks

Hazard answered 20/3, 2019 at 8:21 Comment(0)
P
1

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

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.

Pangermanism answered 20/3, 2019 at 18:28 Comment(6)
Looks like that's my problem indeed. Sadly I do need this in my functions and your "ad-hock callback management" is a bit too complicated for me (I'm not a complete beginner but I'm certainly not quite experienced yet). Do you have any other Idea on how I could manage my code to make it work ?Hazard
Take a look at fltk 1.4.0 manual, the hint right before section 5.10. It's on page 28 in the pdf version.Pangermanism
I tried to minimally generalize the idea, but it still needs more meet. I would install a singletone object via the ad-hoc and use boost::signal2 as the actual callback manager.Pangermanism
The hint seems more like something I can work with. I've also found this article that explains in a bit more details. ThanksHazard
@Hazard you need to define one static function per callback. I tried to to create a class that handles all callbacks fo 'controller'. It can be modified(the static_assert part) to handle callbacks for every type of 'this'. The example in the link where you pointed is about passing 'this' alongside arbitrary 'data'; it is complex and - I think - not what you want.Pangermanism
I am going to modify the answer. A short one existsPangermanism
O
0

One static for every callback is how we used to do it before C++08, lambdas and future versions came out

{ 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)
{
     ....
}
Olivares answered 27/3, 2019 at 8:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.