Okay. But with std::function it is possible to have any capture you want!!!!. See this example:
======== closure.cpp
#include <iostream>
#include <functional>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl.H>
using FunctionCallback = std::function<void (Fl_Widget* w, void* d)>;
auto adapter = [](Fl_Widget* w, void* data)
{
FunctionCallback* pFunc = reinterpret_cast<FunctionCallback*>(data);
(*pFunc)(w, NULL);
};
int main( int argc, const char** argv )
{
int counter = 0;
Fl_Double_Window win(640, 400);
Fl_Button but( 10, 10, 50, 20, "Push" );
// Here we capture two things, the window and the counter!!!
auto callback_lambda = [&counter, &win](Fl_Widget* w, void* d){
std::cerr << counter << ") WxH=" << win.w() << "x" << win.h()
<< " w->label=" << w->label() << std::endl;
};
FunctionCallback callback_object = callback_lambda;
but.callback( adapter, &callback_object);
win.end();
win.show();
return Fl::run();
}
======
CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
project( closure )
set( CMAKE_CXX_STANDARD 14 )
set( CMAKE_CXX_STANDARD_REQUIRED ON )
set( CMAKE_CXX_EXTENSIONS OFF )
find_package( FLTK NO_MODULE )
set( HEADERS
)
set(SOURCES
closure.cpp
)
include_directories( ${FLTK_INCLUDE_DIRS} )
link_directories( ${FLTK_LIBRARY_DIR} )
if( APPLE )
add_definitions( -D GL_SILENCE_DEPRECATION )
set( OSX_FRAMEWORKS "-framework Cocoa -framework IOKit" )
list( APPEND LIBRARIES ${OSX_FRAMEWORKS} )
endif()
add_executable(closure ${SOURCES} ${HEADERS})
list( APPEND LIBRARIES fltk )
target_link_libraries(closure ${LIBRARIES} )
set_target_properties(closure PROPERTIES FOLDER bin)
set_target_properties(closure PROPERTIES PUBLIC_HEADER "${HEADERS}”)
========
This is SUPER COOL!!!
—
Gonzalo Garramuno
ggar...@gmail.com