Invoking a functor is straightforward. Each functor class overloads operator() to provide a style of invocation identical to that of a function call, as shown in Example 72.
void function1(void); RWFunctor0 functor1 = rwtMakeFunctor0((void(*)(void))0,function1); functor1(); // Invocation int function2(int); RWTFunctorR1<int,int> functor2 = rwtMakeFunctorR1((int(*)(int))0,function2); int result = functor2(10); // Invocation (passing caller data) void function3(int); RWFunctor0 functor3 = rwtMakeFunctor0((void(*)(void))0,function3,10); functor3(); // Invocation (passes callee data)
Example 73 takes advantage of the type independence of functors to implement a timer. It starts the clock, runs a function, and reports how long it takes to execute. This timer takes a functor of type RWFunctor0, so it can handle any function that can be encapsulated by such a functor. Because RWFunctor0 stores up to three callee arguments, the timer can test any function with up to three arguments. Because a return value can be ignored for the purpose of timing, it was possible to specify a functor that can handle functions either with or without a return value. Notice how simple it is to do something with functors that would be too difficult to attempt otherwise. Without functors, you would have to write a timer for every possible combination of function return type and argument types.
The timer doesn't know anything about the function it is launching. Only the callee software that builds the functor needs to know about the function it encapsulates. In this example, the same routine builds the functors and runs them, but it could just as easily be an entirely separate callee building the functors and passing them to the timer, serving as the caller.
#include <rw/functor/functor0.h> #include <rw/rstream.h> #include <rw/timer.h> #include <math.h> #include <stdlib.h> // This function takes a functor and runs it, returning the time // that it took to execute. double timer( RWFunctor0 functor ){ // 1 RWTimer timer; timer.start(); functor(); // 2 timer.stop; return timer.elapsedTime(); } int main() { ... // Timing the pow function from the math.h header file // a 2 argument function that takes doubles RWFunctor0 pow_functor = rwtMakeFunctor0( 0, pow, // 3 75465223424, 345321); double pow_time = timer( pow_functor ); // 4 cout << "pow from math.h took " << pow_time << " to run" << endl; return 0; // 5 }
//1 | The timer() function takes a functor of type RWFunctor0 and returns a double. |
//2 | timer() runs RWFunctor0's operator(), which invokes the functor instance and launches its encapsulated function. |
//3 | Construct a handle instance, pow_functor, for a functor object. Use the global template function rwtMakeFunctor0() to construct and initialize a functor body instance that is compatible with the pow() function, and to bind that instance to the handle instance. |
//4 | Hand the functor pow_functor to the timer() function. When timer() runs pow_functor, it launches the encapsulated function, pow(). |
©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.