Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Callback< R, T1, T2, etc > Class Template Reference

List of all members.

Detailed Description

template<class R, class T1, class T2, class etc>
class Callback< R, T1, T2, etc >

Generic callbacks.

A Callback<R, T1, T2...> is a function that you can call with arguments T1, T2, ... and obtain a result of type R. (R may be void, and there can be zero or more arguments.) For instance:

You can invoke a Callback just as you'd invoke a function:

de Callback<string, int, bool> my_callback = ...; string s = my_callback(3, true); ndcode

To create a Callback on a function, pass a pointer to the function as the first argument to the Callback constructor:

de string my_function(int a, bool b); Callback<string, int, bool> my_callback(&my_function); ...

// later string s = my_callback(3, true); ndcode

You can also "bind" arguments to your function. They are stored in the Callback object and are passed to your function when it is invoked later:

de string my_function(int a, bool b); Callback<string, bool> my_callback(&my_function, 3); // N.B.: no "int" in type, since we've already specified the int // in the Callback constructor! ...

// later string s = my_callback(true); ndcode

You may also create callbacks from method pointers to objects; just pass a pointer or reference to the object as the first argument to the constructor, and the method pointer second. (Note that create a method pointer in ISO C++, you must use the syntax &ClassName::method_name; compilers are not supposed to let you simply say &method_name within the class.)

de class MyFooClass { int some_instance_variable;

public: string bar(int a, bool b); };

MyFooClass &foo; Callback<string, int, bool> my_callback_1(foo, &MyFooClass::bar); Callback<string, bool> my_callback(foo, &MyFooClass::bar, 3); ...

// later string s = my_callback(3, true); string t = my_callback(true); ndcode

Callbacks may be passed by value. As the name "Callback" suggests, one generally creates a Callback and passes it to other code, which then invokes the Callback at some later point. For instance:

class my_thread : public Thread { public: Callback<bool, int> notifier;

void run() { while (1) { int result;

// perform some lengthy operation ...

// Notify the caller that we're done, and what the // result was. bool happy = notifier(result);

if (happy)

The documentation for this class was generated from the following file:
Generated on Fri Dec 20 13:35:11 2002 for NMSTL