Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Version 1 (modified by landauf, 16 years ago) (diff)

Executor

TracNav(TracNav/TOC_Development)?

Description

Executor is a wrapper around Functor. Executor implements additional features like a name, default values, descriptions and more. While Functor already allows you to bind an object to the function, Executor allows you to bind arguments to the function by using default values. With that you can store a whole functioncall (including object and arguments) in one single class and execute it whenever and wherever you want. This features is used by Timer, ConsoleCommand and many more.

Usage

Creation

To create an Executor, you first have to create a Functor (usually with createFunctor(function-pointer)). Then you call createExecutor(functor) to create the Executor. Of course you can also use new Executor(functor).

void function();

Executor* myexecutor = createExecutor(createFunctor(&function));

Important: createExecutor uses new to create the executor. If you are responsible for the Executor, you have to delete it afterwards.

Call

You can call an Executor just as you call a Functor:

void function(int value);
Executor* myexecutor = createExecutor(createFunctor(&function));

(*myexecutor)(10); // equivalent to function(10);

Default Values

It's possible to add a default value for every argument:

void function(bool value1, float value2, int value3);
Executor* myexecutor = createExecutor(createFunctor(&function));

// Variant 1: Bind a default value to all arguments:
myexecutor->setDefaultValues(false, 2.5, 10);
// Variant 2: Bind default values to specific arguments:
myexecutor->setDefaultValue(0, false); // the first argument
myexecutor->setDefaultValue(1, 2.5);   // the second argument
myexecutor->setDefaultValue(2, 10);    // the third argument

Then you can call the Executor without passing arguments:

(*myexecutor)(); // equivalent to function(false, 2.5, 10);

The Executor uses the previously added default values to compensate the missing arguments.

Of course you can still use arguments:

(*myexecutor)(true, 3.3); // equivalent to function(true, 3.3, 10);

Other Functions

Attachments (1)

Download all attachments as: .zip