Delegates
A maxon::Delegate is used to define a callback function.
A function that provides the callback simply defines a maxon::Delegate argument. The maxon::Delegate class template defines both the return value and argument types of the callback functions.
// This example declares a global function. maxon::Delegate is used to define a callback function.//---------------------------------------------------------------------------------------- // Performs a long running task. // @param[in] progress Callback which is triggered during the task. Return false to stop the execution of the task. //---------------------------------------------------------------------------------------- static void LongRunningTask( const maxon::Delegate < maxon::Bool ( maxon::Int32 )>& progress);
maxon::ValueReceiver is a template for a generic delegate function.
Within a function the maxon::Delegate argument can be used like an ordinary function pointer.
// This example defines a function. The given maxon::Delegate object // is invoked to check if the function should continue.There are multiple ways to define the actual callback. It is possible to define the lambda directly, use a lambda object, use a maxon::Delegate object or just use a simple function pointer.
// define the lambda directly LongRunningTask([]( maxon::Int32 p) { DiagnosticOutput ( "Percentage: @" , p); if (p > 50) return false ; return true ; });
// use a lambda object auto lambda = []( maxon::Int32 p) { DiagnosticOutput ( "Percentage: @" , p); if (p > 66) return false ; return true ; }; LongRunningTask(lambda);
// use maxon::Delegate object const maxon::Delegate < maxon::Bool ( maxon::Int32 )> maxonDelegate(SomeFunction); LongRunningTask(maxonDelegate);
// or just a function pointer LongRunningTask(SomeFunction);