FLTK 1.4.0
|
This file provides macros for easy function and method callbacks with multiple type safe arguments. More...
#include <stdlib.h>
Go to the source code of this file.
Macros | |
#define | FL_FUNCTION_CALLBACK_3(WIDGET, FUNC, TYPE0, VALUE0, TYPE1, VALUE1, TYPE2, VALUE2) |
Declare a C function callback with custom parameters. | |
#define | FL_INLINE_CALLBACK_2(WIDGET, TYPE0, NAME0, VALUE0, TYPE1, NAME1, VALUE1, LAMBDA) |
Creates code to declare a callback function in line with instantiating a widget. | |
#define | FL_METHOD_CALLBACK_1(WIDGET, CLASS, SELF, METH, TYPE0, VALUE0) |
Declare a non-static class method callback with custom parameters. | |
This file provides macros for easy function and method callbacks with multiple type safe arguments.
#define FL_FUNCTION_CALLBACK_3 | ( | WIDGET, | |
FUNC, | |||
TYPE0, | |||
VALUE0, | |||
TYPE1, | |||
VALUE1, | |||
TYPE2, | |||
VALUE2 | |||
) |
Declare a C function callback with custom parameters.
You can declare a plain C function callback or a static method callback with custom parameters using this macro. It simplifies the process of calling arbitrary functions with up to five custom parameters. The macro generates code that ensures type safety and expands FLTK's standard callbacks, which are limited to a single void*
or long
argument.
To use the macro, you provide the widget that will handle the callback as the first argument. The second argument can be either a regular function or a static method in any class.
Following these arguments, you can include up to five pairs, where each pair consists of a type and a value. For example, int, 3
specifies an integer parameter with a value of 3. If you need to pass two arguments, you can use two pairs, like this: int, 3, int, 4
. The last digit of the macro name must be the same as the number of pairs (0..5)
Whenever the code generated by the macro is called, the custom parameters are duplicated and marked for automatic deallocation using delete
when the callback widget is destroyed.
You can find a small demonstration program showcasing the usage of FL_*_CALLBACK_*
in the examples/callbacks.cxx
file.
WIDGET | the widget that will call the callback |
FUNC | a C/C++ function or a static class method |
TYPE0,VALUE0,TYPE1,VALUE1,TYPE2,VALUE2 | a list of zero to five type/value pairs, all separated by commas |
#define FL_INLINE_CALLBACK_2 | ( | WIDGET, | |
TYPE0, | |||
NAME0, | |||
VALUE0, | |||
TYPE1, | |||
NAME1, | |||
VALUE1, | |||
LAMBDA | |||
) |
Creates code to declare a callback function in line with instantiating a widget.
You can use this macro to create a function as a callback, allowing you to define the callback function right where the widget and callback are declared, similar to a Lambda function.
The first argument of the macro specifies the widget that will handle the callback. Next, you can include up to five triplets, where each triplet consists of a type, a parameter name, and a value. For example, int, x, 3
specifies an integer parameter with a value of 3. If you need to pass two arguments, you can use two triplets, such as int, x, 3, int, y, 4
. The last digit of the macro name must be the same as the number of triplets (0..5).
The last argument is the actual function body itself.
The function body is limited to a syntax that the macro preprocessor can handle. It should include the leading '{' and trailing '}' and may contain local variable declarations, use global variables and functions, and use also the variables listed and initialized in the argument triples of the macro. Very large function bodies should be avoided because they may exceed the admissible size of a macro argument.
Whenever the code generated by the macro is called, the custom parameters are duplicated and marked for automatic deallocation using delete
when the callback widget is destroyed.
You can find a small demonstration program showcasing the usage of FL_*_CALLBACK_*
in the examples/callbacks.cxx
file.
WIDGET | the widget that will call the callback |
TYPE0 | the type of the first parameter in the function call |
NAME0 | an arbitrary variable name that can be used as a parameter in the function body |
VALUE0 | a constant value or a variable; the value of the variable is copied when the callback is created |
TYPE1,NAME1,VALUE1 | as above; there are six macros that support 0 to 5 parameters |
LAMBDA | the function body within the limits of the C macro preprocessor |
#define FL_METHOD_CALLBACK_1 | ( | WIDGET, | |
CLASS, | |||
SELF, | |||
METH, | |||
TYPE0, | |||
VALUE0 | |||
) |
Declare a non-static class method callback with custom parameters.
You can declare a callback for a non-static class method with custom parameters using this macro. It provides a convenient way to call arbitrary methods in any class, overcoming FLTK's limitation of passing only a single void*
or long
argument. Furthermore, it ensures type safety.
The first argument of the macro specifies the widget that will handle the callback. The second argument indicates the class type to be called. The third argument must be a pointer to an instance of that class. The fourth argument is the name of the method within the class. That method must be public and should not be static.
Following these arguments, you can include up to five pairs, where each pair consists of a type and a value. For example, int, 3
specifies an integer parameter with a value of 3. If you need to pass two arguments, you can use two pairs, like this: int, 3, int, 4
. The last digit of the macro name must be the same as the number of pairs (0..5)
Whenever the code generated by the macro is called, the custom parameters are duplicated and marked for automatic deallocation using delete
when the callback widget is destroyed.
You can find a small demonstration program showcasing the usage of FL_*_CALLBACK_*
in the examples/callbacks.cxx
file.
WIDGET | the widget that will call the callback |
CLASS | the class type |
SELF | a pointer to an instance of the class |
METH | a C++ class method that must be public and not static |
TYPE0,VALUE0 | a list of zero to five type/value pairs, all separated by commas |