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:10 2002 for NMSTL