Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site











SourceForge.net Logo

About Callbacks and Bars

This article discusses callback types and function overloads involved in Bar operations (MenuBar, ToolBar).  The Bar class is a base class of BarCtrl which is a base class of MenuBar and ToolBar.

 

The typical pattern you can see in U++ code for handling Bars looks like this:

 

void HelloWorld::About()

{

    ...

}

 

void HelloWorld::FileMenu(Bar& bar)

{

    bar.Add("About..", THISBACK(About));

    bar.Separator();

    bar.Add("Exit", THISBACK(Close));

}

 

void HelloWorld::FileMenu2(Bar& bar, int val)

{

    bar.Add("About..", THISBACK(About));

    if (val == 1)

    {

        bar.Separator();

        bar.Add("Exit", THISBACK(Close));

    }

}

 

HelloWorld::HelloWorld()

{

    menu.Add("File", THISBACK(FileMenu));

    menu.Add("File2",THISBACK1(FileMenu2,0);

 

Here THISBACK macro expands to

 

    callback(this, &HelloWorld::FileMenu)

 

callback is an overloaded non member template function which has overloads that allow the member function being called back to have none, 1,2,3 or 4 arguments.  Hence the FileMenu function can have none, 1,2,3,or 4 parameters and the appropriate overload of callback will be selected.  When the target called back function is called (e.g. FileMenu), the actual arguments that are passed to it are provided by the event dispatcher which knows how many arguments it needs to pass for a particular event.  In the example above, the HelloWorld constructor calls the callback function which creates a Callback object (on the heap) containing the identity of the target function (FileMenu) to be called.  The menu.Add function stores the address of the Callback object which will be used to call the FileMenu function when an event is triggered.  There are several different Callback classes, including the Callback1MethodAction class, which is used for the above case.  The Callback classes are functors and usually have a member function named Execute that is called by the event dispatching mechanism.  For the Callback1MethodAction class, the Execute member function takes one parameter (provided by the event dispatcher) which is passed to the target callback function FileMenu.

 

There is also a THISBACK1 macro, which allows specification of  an additional value that is passed to the called back function (e.g. FileMenu2) when an event is triggered.

 

The  THISBACK1 macro in the above example expands to

 

    callback1(this, &HelloWorld::FileMenu2, 0)

 

The FileMenu2 function takes two parameters, the first of which is supplied by the event dispatcher and the second comes from the additional value 0 specified in the invocation of THISBACK1.  A possible use for this is to allow the target function to be called from multiple places., each of which identifies itself using the additional parameter.  For the above example, the Callback object involved is of type CallbackMethodActionArg1.and this object stores the additional value (0 in this case) to be passed to the FileMenu2 function when an event is triggered.  If you use the THISBACK1 macro with a member function that takes only one argument then the Callback object is of type  CallbackMethodActionArg.

 

There is also a THISBACK2 macro which allows two additional arguments.  The member function used with THISBACK, can have 0,1,2,3 or 4 parameters.  For THISBACK1, the member function must have at least one parameter and for THISBACK2 the member function can have 2,3 or 4 parameters.

 

If the member function used in the THISBACK macros is overloaded (e.g. FileMenu(int)) then the code won't compile. In this case you have to use a forwarding member function that is not overloaded. (You can't supply argument types when you take the address of a member function.  If the function is overloaded, the one that's chosen depends on the target, but in this case the target is also overloaded so it can't be resolved).

 

In the above example, the Add member function of Bar is an overloaded function and which Add function that is used depends on the return value of the callback function.  For the FileMenu example above, the return type of the callback function is Callback1<Bar&>. (In U++, Callback classes start with upper case C and callback functions have lower case c).

 

Last edit by cxl on 05/29/2013. Do you want to contribute?. T++