| 1 | = Functor = |
| 2 | [[TracNav(TracNav/TOC_Development)]] |
| 3 | [[TOC]] |
| 4 | |
| 5 | == Description == |
| 6 | The Functor is a wrapper around a function-pointer. You can call the function by executing the Functor without taking care of the types of parameters and return value. This is achieved by using [wiki:MultiType] to pass arguments. |
| 7 | |
| 8 | Functor works with (almost) every function. This is possible because of a number of derived templates, one for every possible combination: |
| 9 | * Static, member or const member |
| 10 | * With or without returnvalue |
| 11 | * With or without parameters (up to five parameters are allowed) |
| 12 | |
| 13 | As you see, this results in a total amount of 3*2*6 = 36 combinations, each of them implemented as a template to allow arbitrary types for classes, returnvalues and parameters. |
| 14 | |
| 15 | Functor.h implements all those templates by using a macro to avoid annoying code repetition. |
| 16 | |
| 17 | '''Limitation''': Because Functor uses [wiki:MultiType] to pass the arguments, parameters and return values must be of a [wiki:MultiType# supported type]. |
| 18 | |
| 19 | == Usage == |
| 20 | === Creation === |
| 21 | To simplify creation of a new Functor, the helper function '''createFunctor('''''function-pointer''''')''' was created. Without that function, you would have to pick the right template (out of 36 possibilities) and specify all template arguments - quite annoying. But with '''createFunctor''' it's really easy: |
| 22 | {{{ |
| 23 | int someFunction(float param1, bool param2); |
| 24 | |
| 25 | Functor* myFunctor = createFunctor(&someFunction); |
| 26 | }}} |
| 27 | In this case, ''&someFunction'' is the function-pointer of the static function "someFuncton". For memberfunctions, this looks a bit different: |
| 28 | {{{ |
| 29 | class SomeClass |
| 30 | { |
| 31 | float someFunction(const std::string& param); |
| 32 | }; |
| 33 | |
| 34 | Functor* myFunctor = createFunctor(&SomeClass::someFunction); |
| 35 | }}} |
| 36 | Remember: This is just basic function-pointer knowledge, nothing special here. Functor does what it has to do and createFunctor works as usual. |
| 37 | |
| 38 | '''Important''': createFunctor uses '''new''' to create the Functor and returns a pointer. If you are responsible for the pointer, you have to delete the Functor after usage. |
| 39 | |
| 40 | === Call === |
| 41 | To call the function, just use operator() and pass the arguments as if you would call the function-pointer directly: |
| 42 | {{{ |
| 43 | void someFunction(int value); |
| 44 | Functor* myFunctor = createFunctor(&someFunction); |
| 45 | |
| 46 | (*myFunctor)(10); // equivalent to someFunction(10); |
| 47 | }}} |
| 48 | |
| 49 | === Returnvalue === |
| 50 | If your function returns a value, use '''getReturnvalue()''': |
| 51 | {{{ |
| 52 | int doubleValue(int value) { return value*2; } |
| 53 | Functor* myFunctor = createFunctor(&doubleValue); |
| 54 | |
| 55 | (*myFunctor)(10); // equivalent to doubleValue(10); |
| 56 | |
| 57 | int result = myFunctor->getReturnvalue(); // result = 20 |
| 58 | }}} |
| 59 | |
| 60 | === Information === |
| 61 | There are some functions returning some information about the function: |
| 62 | * '''getParamCount()''': Returns the amount of parameters the function takes |
| 63 | * '''hasReturnvalue()''': Returns true if the function returns a value |
| 64 | * '''getType()''': Returns the type of the function as an enum: FT_MEMBER, FT_CONSTMEMBER, FT_STATIC |
| 65 | * '''getTypenameParam('''''param number (0-4)''''')''': Returns the typename of the given parameter as a string |
| 66 | * '''getTypenameReturnvalue()''': Returns the typename of the returnvalue as a string |
| 67 | |
| 68 | == Types == |
| 69 | === FunctorStatic === |
| 70 | |
| 71 | === FunctorMember === |
| 72 | |
| 73 | == Template == |
| 74 | |
| 75 | == Examples == |