![]() |
![]() |
Links | Downloads | Tutorials | Projects | Services | ConnectTel | Resume |
|
MotivationDevelopment of a Callback mechanism for C++ which must be simple and portable. An important goal is "easy to use" with all major C++ compilers available.
Callback DefinitionA callback is a function, provided by a client to a subsystem, that allows that subsystem to perform a specific operation in the context of the client.Callbacks are a powerful tool for breaking dependencies between cooperative classes. They can be extremely useful in an event driven design such as graphical user interface framework. Another area where callbacks are useful is for decoupling of the businees logic of an application from its user interface. The indiscriminate use of callbacks can lead to designs that are difficult to understand, debug and maintain. Their ( pseudo ) asynchrous behavior requires a different type of attention from the developers. For a more detailed description of callbacks and its use, check this book:
Large-Scale C++ Sofrware Design NOTE: The definition used here was based on the Callback section of John's book.
Class DiagramThe UML Class Diagram below show the relationships of the C++ callback framework. This framework is probably the most simpler of its kind. It allows clients to define callbacks either as global functions, static methods, or methods of classes. A method of a class can be virtual when used as a callback.A callback is templated on the type of parameter used by the client and the subsystem involved. This "parameter type" will be the unique contract between the parts. For callbacks using member functions, they are also templated on the type of class involved.
It is important to have a good policy about the "parameter type" passd in with the notifications.
We recommend to let the subsystem to handle the creation/deletion of it. This means that the
invoker of a callback, in someway gets the parameter to pass in, makes the call and after the
call is complete, it is responsible for getting rid of it. If the client needs
to keep a copy of the data passed in, it must make a copy of it. This policy varies from
project to project, but it is good to be very clear early on.
CallbackContainers should be used for multi-cast notifications to multiple clients. It should be used with care on a multi-threaded environment. Policy on Creation/DeletionIt is important to establish early on the policy for creation and deletion of the callback instances. Defining who will be responsible for deletion of the instances, and when that happens must be clear for the whole team. Usually, there are two scenarios for using callbacks:
The important thing is that this policy should be clear and documented to the team. Every new member should know it early on.
Return ValueReturn value in callbacks make not much sense. It is there for some application that might need some confirmation back. This always can be done adding extra attributes to the parameter involved. If you do not use the return value, return 0.
ExamplesThis section briefly describes the examples that comes with the test code provided with the Callback framework. Check the test directory in the distribution for a complete example on how to use this framework.
Assume that you have a global function as defined below,
int bar( int * p ) Creating a callback instance that relying on the global function above is as easy as doing this:
FunctionCallback< int > f( bar ); Invoking the callback is as easy as doing this:
    int x = 3;
Let's define a class with a virtual method which we intend to use it as a callback:
class FooBase Creating a callback instance that relies on this member function is as easy as doing this:
FooBase b; Invoking the callback is as easy as doing this:
    int x = 3;
Downloadcallback-1.01.zipArchive with source code and examples of the callback framework.
LicenseThis framework is licensed under the LGPL.Visitors:![]() Last modified Apr 27, 2000 Copyright ©1999-2000 Rosimildo da Silva. All rights reserved. |
e-mail me |
---|