FLTK logo

Re: [fltk/fltk] Add lambdas with captures as callback option. (PR #1597)

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Library      Forums      Links      Apps     Login 
[  All Forums  |  Back to fltk.issues  ]
 
[ Previous Message ] [ New Message | Reply ] [ Next Message ]

Re: [fltk/fltk] Add lambdas with captures as callback option. (PR #1597) Matthias Melcher 22:49 Sep 01  
 
MatthiasWM left a comment (fltk/fltk#1597)

To sum my idea up, this implementation works very well. The code in Fl_Callback_Interface.H is quite packed, but this is a lot better than adding it to the already large Fl_Widget.H and Fl_Widget.cxx.

Using std::function is really neat and keeps the callback where the widget constructor is. My silly test is:

  Fl_Button *a = new Fl_Button(20, 40, 200, 30, "Click Me");
  Fl_Button *b = new Fl_Button(20, 80, 200, 30, "vanish");
  a->callback(
    [a]() {
      printf("%d\n", a->value());
      a->label("Clicked!");
      a->redraw();
    }
  );
  b->callback(
    [a]() {
      delete a;
    }
  );

There is not a single type cast in the code. Calling a->value() is no problem because a is a Fl_Button*, so this is all nice and type-safe.

There is never a win without negatives. So here we go:

  • maintainability: the callback system has become complex. But I think the benefits and modernization outweighs that. Yes, I still need to add comments to the Fl_Callback_Interface class.
  • base class vs. interface vs. multiple inheritance: Fl_Callback_Interface may look like a base class, but it's not. Its constructor and destructor are protected to make that clear. Multiple inheritance is possible if needed because Fl_Callback_Interface has a very small name pollution (callback_interface_ besides the public methods) and no virtual destructor.
  • size and speed: no worries, the compiler will optimize almost everything away.
  • error messages: this is the only bummer. If there is an error in the user's callback code, the error messages become more cryptic, but far from the std library gibberish.

Anyway, if you made it all the way down here, any feedback is appreciated.

—
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!
You are receiving this because you are subscribed to this thread.Message ID: <fltk/fltk/pull/1597/c5501470033@github.com>

[ Direct Link to Message ]
 
     
[ Previous Message ] [ New Message | Reply ] [ Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2026 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.