Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Threads Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

8.5 Invoking Functors

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.

Example 72: Invoking functors

8.5.1 Running Unknown Functions

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.

Example 73: Building a timer with functors

//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().



Previous fileTop of DocumentContentsIndex pageNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.