| 1 | = Executor = |
| 2 | [[TracNav(TracNav/TOC_Development)]] |
| 3 | |
| 4 | == Description == |
| 5 | Executor is a wrapper around [wiki: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 [wiki:Timer], [wiki:ConsoleCommand] and many more. |
| 6 | |
| 7 | == Usage == |
| 8 | === Creation === |
| 9 | To create an Executor, you first have to create a [wiki:Functor] (usually with '''createFunctor('''''function-pointer''''')'''). Then you call '''createExecutor('''''functor''''')''' to create the Executor. Of course you can also use '''new Executor('''''functor''''')'''. |
| 10 | {{{ |
| 11 | void function(); |
| 12 | |
| 13 | Executor* myexecutor = createExecutor(createFunctor(&function)); |
| 14 | }}} |
| 15 | |
| 16 | '''Important''': createExecutor uses '''new''' to create the executor. If you are responsible for the Executor, you have to delete it afterwards. |
| 17 | |
| 18 | === Call === |
| 19 | You can call an Executor just as you call a [wiki:Functor]: |
| 20 | {{{ |
| 21 | void function(int value); |
| 22 | Executor* myexecutor = createExecutor(createFunctor(&function)); |
| 23 | |
| 24 | (*myexecutor)(10); // equivalent to function(10); |
| 25 | }}} |
| 26 | |
| 27 | === Default Values === |
| 28 | It's possible to add a default value for every argument: |
| 29 | {{{ |
| 30 | void function(bool value1, float value2, int value3); |
| 31 | Executor* myexecutor = createExecutor(createFunctor(&function)); |
| 32 | |
| 33 | // Variant 1: Bind a default value to all arguments: |
| 34 | myexecutor->setDefaultValues(false, 2.5, 10); |
| 35 | }}} |
| 36 | {{{ |
| 37 | // Variant 2: Bind default values to specific arguments: |
| 38 | myexecutor->setDefaultValue(0, false); // the first argument |
| 39 | myexecutor->setDefaultValue(1, 2.5); // the second argument |
| 40 | myexecutor->setDefaultValue(2, 10); // the third argument |
| 41 | }}} |
| 42 | |
| 43 | Then you can call the Executor without passing arguments: |
| 44 | {{{ |
| 45 | (*myexecutor)(); // equivalent to function(false, 2.5, 10); |
| 46 | }}} |
| 47 | The Executor uses the previously added default values to compensate the missing arguments. |
| 48 | |
| 49 | Of course you can still use arguments: |
| 50 | {{{ |
| 51 | (*myexecutor)(true, 3.3); // equivalent to function(true, 3.3, 10); |
| 52 | }}} |
| 53 | |
| 54 | === Other Functions === |