Tools.h++ Manual

14-8 104011 Tandem Computers Incorporated
14
Apply functions
The second kind of user-defined function is an "apply function". Its general
form is:
void
yourApplyFunction
(
type
* ty, void* a)
where yourApplyFunction is the name of the function and type is the type of the
members of the collection. Apply functions give you the opportunity to
perform some operation on each member of a collection (perhaps print it out or
draw it on a screen). The second argument is designed to hold "client data" to
be used by the function (perhaps the handle of a window on which the object
is to be drawn).
Here is an example, using class
RWGDlist
(type)—a generic doubly-linked list:
#include <rw/gdlist.h>
#include <rw/rstream.h>
class Foo {
public:
int
val
;
Foo(int i) {
val
= i;}
};
declare(RWGDlist, Foo)
void printAFoo(Foo* ty, void* sp)
{
ostream* s = (ostream*)sp;
(*s) << ty->
val
<< "\n";}
main()
{
RWGDlist(Foo) gd;
gd.append(new Foo(1));
gd.append(new Foo(2));
gd.append(new Foo(3));
gd.append(new Foo(4));
gd.apply(printAFoo, &cout);
return 0;
}