Orxonox  0.0.5 Codename: Arcturus
Classes | Namespaces | Typedefs | Functions | Variables
Functor.h File Reference

Definition of orxonox::Functor and its specialized subclasses, as well as the createFunctor() functions. More...

#include "core/CorePrereqs.h"
#include <typeinfo>
#include "util/Output.h"
#include "util/MultiType.h"
#include "core/object/Destroyable.h"
#include "FunctorPtr.h"

Go to the source code of this file.

Classes

class  orxonox::Functor
 The Functor classes are used to wrap function pointers. More...
 
class  orxonox::FunctorMember< O >
 FunctorMember is a child class of Functor and expands it with an object-pointer, that is used for member-functions, as well as an overloaded execution operator. More...
 
class  orxonox::FunctorMember< void >
 Specialization of FunctorMember with T = void. More...
 
class  orxonox::FunctorPointer< F, O >
 FunctorPointer is a child class of FunctorMember and extends it with a function-pointer (or a function-object). More...
 
class  orxonox::FunctorTemplate< F, R, O, isconst, Params >
 FunctorTemplate is a child class of FunctorPointer and implements all functions that need to know the exact types of the parameters, return-value, and class. More...
 

Namespaces

 orxonox
 Die Wagnis Klasse hat die folgenden Aufgaben:
 

Typedefs

typedef FunctorMember< voidorxonox::FunctorStatic
 FunctorStatic is just a typedef of FunctorMember with T = void. More...
 

Functions

template<class R , class O , class OO , class... Params>
FunctorMemberPtr< O > orxonox::createFunctor (R(O::*functionPointer)(Params...), OO *object)
 Creates a new FunctorMember with the given function-pointer and an assigned object. More...
 
template<class R , class O , class... Params>
FunctorMemberPtr< O > orxonox::createFunctor (R(O::*functionPointer)(Params...))
 Creates a new FunctorMember with the given function-pointer. More...
 
template<class R , class... Params>
FunctorStaticPtr orxonox::createFunctor (R(*functionPointer)(Params...))
 Creates a new FunctorStatic with the given function-pointer. More...
 
template<class F >
FunctorStaticPtr orxonox::createFunctor (const F &functionObject)
 Take care that this functor does not outlive objects that have been captured by reference in a lambda. More...
 
template<class T >
std::string orxonox::typeToString ()
 Returns the name of type T as string. More...
 

Variables

const unsigned int orxonox::MAX_FUNCTOR_ARGUMENTS = 5
 The maximum number of parameters of a function that is supported by Functor. More...
 

Detailed Description

Definition of orxonox::Functor and its specialized subclasses, as well as the createFunctor() functions.

Functors can be used to wrap function-pointers. While function-pointers have a very complicated syntax in C++, Functors are always the same and you can call the wrapped function-pointer independently of its parameter with arguments of type MultiType. These arguments are then automatically converted to the right type.

To create a Functor, the helper function createFunctor() is used. It returns an instance of orxonox::FunctorPtr which is simply a typedef of "std::shared_ptr<Functor>". This means you don't have to delete the Functor after using it, because it is managed by the std::shared_ptr.

Example:

int myStaticFunction(int value) // Definition of a static function
{
return (value * 2); // Return the double of the value
}
FunctorPtr functor = createFunctor(&myStaticFunction); // Create a Functor
int result = (*functor)(5); // Calls the functor with value = 5, result == 10
int result = (*functor)("7"); // Calls the functor with a string which is converted to an integer, result == 14

Functors can also be used if you work with member-functions. In this case createFunctor() returns an instance of orxonox::FunctorMemberPtr - this allows you to define the object that will be used to call the function.

Example:

class MyClass // Define a class
{
public:
MyClass(const std::string& text) // Constructor
{
this->text_ = text;
}
bool contains(const std::string& word) // Function that searches for "word" in "text"
{
return (this->text_.find(word) != std::string::npos);
}
private:
std::string text_; // Member variable
};
MyClass* object = new MyClass("Hello World"); // Create an object of type MyClass and set its text to "Hello World"
FunctorPtr functor = createFunctor(&MyClass:contains, object); // Create a Functor (note the object!)
bool result = (*functor)("World"); // result == true
bool result = (*functor)("test"); // result == false

Instead of assigning the object directly to the functor when creating it, you can also define it at any later point or when you call the functor. Note however that this works only with orxonox::FunctorMember.

MyClass* object1 = new MyClass("Hello World"); // Create an object
MyClass* object2 = new MyClass("this is a test"); // Create another object
FunctorMemberPtr functor = createFunctor(&MyClass:contains); // Create a FunctorMember (note: no object this time)
bool result = (*functor)("World"); // result == false and an error: "Error: Can't execute FunctorMember, no object set."
bool result = (*functor)(object1, "World"); // result == true
bool result = (*functor)(object1, "test"); // result == false
bool result = (*functor)(object2, "test"); // result == true
functor->setObject(object1); // Assign an object to the FunctorMember
bool result = (*functor)("World"); // result == true (no error this time, because the object was set using setObject())