Files CCore/inc/gadget/Function.h CCore/src/gadget/Function.cpp
Function is a universal, lightweight callback. The last property is essential in the real-time and system programming. Callback means a generalized function pointer. You can call a function or a class method indirectly using a callback.
template <class R,class ... AA>
class Function<R (AA...)>
{
....
public:
// constructors
Function(); // null function
Function(NothingType); // null function
Function(R (*func)(AA...)); // callback to the static function
Function(R (*func)(Handle ctx,AA...),Handle ctx); // callback to the static function with the handle
template <class T>
Function(T *obj,R (T::*method)(AA...)); // callback to the method with the object
// methods
bool operator + () const; // not null
bool operator ! () const; // is null
template <class ... TT>
R operator () (TT && ... tt); // do call
};
You can bind a Function to a static function, a static function with a handle or an object with a method. Make sure the handle or the object is alive when you do call the function — there is no a life-time control in the Function. The object type must be derived from the Funchor base class.
struct Funchor
{
template <class T,class R,class ... AA>
static Function<R (AA...)> FunctionOf(T *obj,R (T::*method)(AA...));
};
The static method FunctionOf of this class can be used to create a Function object.
There is a non-copyable base type Funchor_nocopy:
using Funchor_nocopy = NoCopyBase<Funchor> ;
The following pattern is used in CCore to work with Functions:
class C : public Funchor { public: void method(); Function<void (void)> function_method() { return FunctionOf(this,&C::method); } };
You may bind virtual methods in Functions, the virtualization is working in such case.