libxorp-callback Class Index


Annotated List
Files
Globals
Hierarchy
Index

Callback Overview

XORP is an asynchronous programming environment and as a result there are many places where callbacks are useful. Callbacks are typically invoked to signify the completion or advancement of an asynchronous operation.

XORP provides a generic and flexible callback interface that utilizes overloaded templatized functions for for generating callbacks in conjunction with many small templatized classes. Whilst this makes the syntax a little ugly, it provides a great deal of flexibility.

XorpCallbacks are callback objects are created by the callback() function which returns a reference pointer to a newly created callback object. The callback is invoked by calling dispatch(), eg.



#include <iostream>

#include "config.h"
#include "libxorp/callback.hh"

static void hello_world() { 
    cout << "Hello World" << endl;
}

int main() {
    // Typedef callback() return type for readability.  SimpleCallback
    // declares a XorpCallback taking 0 arguments and of return type void.
    typedef XorpCallback0<void>::RefPtr SimpleCallback;

    // Create XorpCallback object using callback()
    SimpleCallback cb = callback(hello_world);
    
    // Invoke callback, results in call to hello_world.
    cb->dispatch();
    return 0;
}


The callback() method is overloaded and can also be used to create callbacks to member functions, eg.



#include <iostream>

#include "config.h"
#include "libxorp/callback.hh"

class Foo {
public:
    void hello_world() { 
	cout << "Foo::Hello World" << endl;
    }
};

int main() {
    typedef XorpCallback0<void>::RefPtr SimpleCallback;

    Foo f;

    // Create a callback to a member function
    SimpleCallback cb = callback(&f, &Foo::hello_world);

    // Invoke f.hello_world
    cb->dispatch();

    return 0;
}


In addition, to being able to invoke member functions, callbacks can also store arguments to functions. eg.



#include <iostream>

#include "config.h"
#include "libxorp/callback.hh"

static int sum(int x, int y) {
    cout << "sum(x = " << x << ", y = " << y << ")" << endl;
    return x + y;
}

int main() {
    // Callback to function returning "int"
    typedef XorpCallback0<int>::RefPtr NoArgCallback;

    NoArgCallback cb1 = callback(sum, 1, 2);
    cout << "cb1->dispatch() returns " << cb1->dispatch() << endl; // "3"

    // Callback to function returning int and taking an integer argument
    typedef XorpCallback1<int,int>::RefPtr OneIntArgCallback;

    OneIntArgCallback cb2 = callback(sum, 5);
    cout << "cb2->dispatch(10) returns " << cb2->dispatch(10) << endl; // 15
    cout << "cb2->dispatch(20) returns " << cb2->dispatch(20) << endl; // 25

    // Callback to function returning int and taking  2 integer arguments
    typedef XorpCallback2<int,int,int>::RefPtr TwoIntArgCallback;

    TwoIntArgCallback cb3 = callback(sum);
    cout << "cb3->dispatch() returns " << cb3->dispatch(50, -50) << endl; // 0

    return 0;
}


Bound arguments, as with member functions, are implemented by the overloading of the callback() method. At dispatch time, the bound arguments are last arguments past to the wrappered function. If you compile and run the program you will see:


sum(x = 10, y = 5)
cb2->dispatch(10) returns 15

and:


sum(x = 20, y = 5)
cb2->dispatch(10) returns 25

for the one bound argument cases.

Declaring Callback Types

There are a host of XorpCallbackN types. The N denotes the number of arguments that will be passed at dispatch time by the callback invoker. The template parameters to XorpCallbackN types are the return value followed by the types of arguments that will be passed at dispatch time. Thus type:


XorpCallback1<double, int>::RefPtr

corresponds to callback object returning a double when invoked and requiring an integer argument to passed at dispatch time.

When arguments are bound to a callback they are not specified in the templatized argument list. So the above declaration is good for a function taking an integer argument followed by upto the maximum number of bound arguments.

Note: In this header file, support is provided for upto 6 bound arguments and 12 dispatch arguments.

Ref Pointer Helpers

Callback objects may be set to NULL, since they use reference pointers to store the objects. Callbacks may be unset using the ref_ptr::release() method:


    cb.release();

and to tested using the ref_ptr::is_empty() method:


if (! cb.is_empty()) {
    cb->dispatch();
}

In many instances, the RefPtr associated with a callback on an object will be stored by the object itself. For instance, a class may own a timer object and the associated timer expiry callback which is a member function of the containing class. Because the containing class owns the callback object corresponding the timer callback, there is never an opportunity for the callback to be dispatched on a deleted object or with invalid data.

XorpCallback0 - XorpConstMemberCallback8B5
XorpCallback0
XorpCallback1
XorpCallback10
XorpCallback11
XorpCallback12
XorpCallback2
XorpCallback3
XorpCallback4
XorpCallback5
XorpCallback6
XorpCallback7
XorpCallback8
XorpCallback9
XorpConstMemberCallback0B0
XorpConstMemberCallback0B1
XorpConstMemberCallback0B2
XorpConstMemberCallback0B3
XorpConstMemberCallback0B4
XorpConstMemberCallback0B5
XorpConstMemberCallback0B6
XorpConstMemberCallback10B0
XorpConstMemberCallback10B1
XorpConstMemberCallback10B2
XorpConstMemberCallback10B3
XorpConstMemberCallback10B4
XorpConstMemberCallback10B5
XorpConstMemberCallback10B6
XorpConstMemberCallback11B0
XorpConstMemberCallback11B1
XorpConstMemberCallback11B2
XorpConstMemberCallback11B3
XorpConstMemberCallback11B4
XorpConstMemberCallback11B5
XorpConstMemberCallback11B6
XorpConstMemberCallback12B0
XorpConstMemberCallback12B1
XorpConstMemberCallback12B2
XorpConstMemberCallback12B3
XorpConstMemberCallback12B4
XorpConstMemberCallback12B5
XorpConstMemberCallback12B6
XorpConstMemberCallback1B0
XorpConstMemberCallback1B1
XorpConstMemberCallback1B2
XorpConstMemberCallback1B3
XorpConstMemberCallback1B4
XorpConstMemberCallback1B5
XorpConstMemberCallback1B6
XorpConstMemberCallback2B0
XorpConstMemberCallback2B1
XorpConstMemberCallback2B2
XorpConstMemberCallback2B3
XorpConstMemberCallback2B4
XorpConstMemberCallback2B5
XorpConstMemberCallback2B6
XorpConstMemberCallback3B0
XorpConstMemberCallback3B1
XorpConstMemberCallback3B2
XorpConstMemberCallback3B3
XorpConstMemberCallback3B4
XorpConstMemberCallback3B5
XorpConstMemberCallback3B6
XorpConstMemberCallback4B0
XorpConstMemberCallback4B1
XorpConstMemberCallback4B2
XorpConstMemberCallback4B3
XorpConstMemberCallback4B4
XorpConstMemberCallback4B5
XorpConstMemberCallback4B6
XorpConstMemberCallback5B0
XorpConstMemberCallback5B1
XorpConstMemberCallback5B2
XorpConstMemberCallback5B3
XorpConstMemberCallback5B4
XorpConstMemberCallback5B5
XorpConstMemberCallback5B6
XorpConstMemberCallback6B0
XorpConstMemberCallback6B1
XorpConstMemberCallback6B2
XorpConstMemberCallback6B3
XorpConstMemberCallback6B4
XorpConstMemberCallback6B5
XorpConstMemberCallback6B6
XorpConstMemberCallback7B0
XorpConstMemberCallback7B1
XorpConstMemberCallback7B2
XorpConstMemberCallback7B3
XorpConstMemberCallback7B4
XorpConstMemberCallback7B5
XorpConstMemberCallback7B6
XorpConstMemberCallback8B0
XorpConstMemberCallback8B1
XorpConstMemberCallback8B2
XorpConstMemberCallback8B3
XorpConstMemberCallback8B4
XorpConstMemberCallback8B5
XorpConstMemberCallback8B6 - XorpFunctionCallback9B3
XorpConstMemberCallback8B6
XorpConstMemberCallback9B0
XorpConstMemberCallback9B1
XorpConstMemberCallback9B2
XorpConstMemberCallback9B3
XorpConstMemberCallback9B4
XorpConstMemberCallback9B5
XorpConstMemberCallback9B6
XorpFunctionCallback0B0
XorpFunctionCallback0B1
XorpFunctionCallback0B2
XorpFunctionCallback0B3
XorpFunctionCallback0B4
XorpFunctionCallback0B5
XorpFunctionCallback0B6
XorpFunctionCallback10B0
XorpFunctionCallback10B1
XorpFunctionCallback10B2
XorpFunctionCallback10B3
XorpFunctionCallback10B4
XorpFunctionCallback10B5
XorpFunctionCallback10B6
XorpFunctionCallback11B0
XorpFunctionCallback11B1
XorpFunctionCallback11B2
XorpFunctionCallback11B3
XorpFunctionCallback11B4
XorpFunctionCallback11B5
XorpFunctionCallback11B6
XorpFunctionCallback12B0
XorpFunctionCallback12B1
XorpFunctionCallback12B2
XorpFunctionCallback12B3
XorpFunctionCallback12B4
XorpFunctionCallback12B5
XorpFunctionCallback12B6
XorpFunctionCallback1B0
XorpFunctionCallback1B1
XorpFunctionCallback1B2
XorpFunctionCallback1B3
XorpFunctionCallback1B4
XorpFunctionCallback1B5
XorpFunctionCallback1B6
XorpFunctionCallback2B0
XorpFunctionCallback2B1
XorpFunctionCallback2B2
XorpFunctionCallback2B3
XorpFunctionCallback2B4
XorpFunctionCallback2B5
XorpFunctionCallback2B6
XorpFunctionCallback3B0
XorpFunctionCallback3B1
XorpFunctionCallback3B2
XorpFunctionCallback3B3
XorpFunctionCallback3B4
XorpFunctionCallback3B5
XorpFunctionCallback3B6
XorpFunctionCallback4B0
XorpFunctionCallback4B1
XorpFunctionCallback4B2
XorpFunctionCallback4B3
XorpFunctionCallback4B4
XorpFunctionCallback4B5
XorpFunctionCallback4B6
XorpFunctionCallback5B0
XorpFunctionCallback5B1
XorpFunctionCallback5B2
XorpFunctionCallback5B3
XorpFunctionCallback5B4
XorpFunctionCallback5B5
XorpFunctionCallback5B6
XorpFunctionCallback6B0
XorpFunctionCallback6B1
XorpFunctionCallback6B2
XorpFunctionCallback6B3
XorpFunctionCallback6B4
XorpFunctionCallback6B5
XorpFunctionCallback6B6
XorpFunctionCallback7B0
XorpFunctionCallback7B1
XorpFunctionCallback7B2
XorpFunctionCallback7B3
XorpFunctionCallback7B4
XorpFunctionCallback7B5
XorpFunctionCallback7B6
XorpFunctionCallback8B0
XorpFunctionCallback8B1
XorpFunctionCallback8B2
XorpFunctionCallback8B3
XorpFunctionCallback8B4
XorpFunctionCallback8B5
XorpFunctionCallback8B6
XorpFunctionCallback9B0
XorpFunctionCallback9B1
XorpFunctionCallback9B2
XorpFunctionCallback9B3
XorpFunctionCallback9B4 - XorpMemberCallback9B6
XorpFunctionCallback9B4
XorpFunctionCallback9B5
XorpFunctionCallback9B6
XorpMemberCallback0B0
XorpMemberCallback0B1
XorpMemberCallback0B2
XorpMemberCallback0B3
XorpMemberCallback0B4
XorpMemberCallback0B5
XorpMemberCallback0B6
XorpMemberCallback10B0
XorpMemberCallback10B1
XorpMemberCallback10B2
XorpMemberCallback10B3
XorpMemberCallback10B4
XorpMemberCallback10B5
XorpMemberCallback10B6
XorpMemberCallback11B0
XorpMemberCallback11B1
XorpMemberCallback11B2
XorpMemberCallback11B3
XorpMemberCallback11B4
XorpMemberCallback11B5
XorpMemberCallback11B6
XorpMemberCallback12B0
XorpMemberCallback12B1
XorpMemberCallback12B2
XorpMemberCallback12B3
XorpMemberCallback12B4
XorpMemberCallback12B5
XorpMemberCallback12B6
XorpMemberCallback1B0
XorpMemberCallback1B1
XorpMemberCallback1B2
XorpMemberCallback1B3
XorpMemberCallback1B4
XorpMemberCallback1B5
XorpMemberCallback1B6
XorpMemberCallback2B0
XorpMemberCallback2B1
XorpMemberCallback2B2
XorpMemberCallback2B3
XorpMemberCallback2B4
XorpMemberCallback2B5
XorpMemberCallback2B6
XorpMemberCallback3B0
XorpMemberCallback3B1
XorpMemberCallback3B2
XorpMemberCallback3B3
XorpMemberCallback3B4
XorpMemberCallback3B5
XorpMemberCallback3B6
XorpMemberCallback4B0
XorpMemberCallback4B1
XorpMemberCallback4B2
XorpMemberCallback4B3
XorpMemberCallback4B4
XorpMemberCallback4B5
XorpMemberCallback4B6
XorpMemberCallback5B0
XorpMemberCallback5B1
XorpMemberCallback5B2
XorpMemberCallback5B3
XorpMemberCallback5B4
XorpMemberCallback5B5
XorpMemberCallback5B6
XorpMemberCallback6B0
XorpMemberCallback6B1
XorpMemberCallback6B2
XorpMemberCallback6B3
XorpMemberCallback6B4
XorpMemberCallback6B5
XorpMemberCallback6B6
XorpMemberCallback7B0
XorpMemberCallback7B1
XorpMemberCallback7B2
XorpMemberCallback7B3
XorpMemberCallback7B4
XorpMemberCallback7B5
XorpMemberCallback7B6
XorpMemberCallback8B0
XorpMemberCallback8B1
XorpMemberCallback8B2
XorpMemberCallback8B3
XorpMemberCallback8B4
XorpMemberCallback8B5
XorpMemberCallback8B6
XorpMemberCallback9B0
XorpMemberCallback9B1
XorpMemberCallback9B2
XorpMemberCallback9B3
XorpMemberCallback9B4
XorpMemberCallback9B5
XorpMemberCallback9B6

Generated by: pavlin on possum.icir.org on Mon Jun 9 13:23:46 2003, using kdoc 2.0a54+XORP.