第一印象
範例程式
截圖
與其它函式庫比較
相關應用
下載
Documentation
作品展示(Bazaar)
現狀及發展藍圖
常見問題(FAQ)
作者與授權
論壇
資助Ultimate++
搜尋本網站
語言
中文(繁體)













SourceForge.net Logo

本頁面還沒有被翻譯. 你要協助翻譯嗎?

 

Callbacks

Callbacks can be described as a very generalized form of function pointers. Each Callback represents some kind of action (usually calling a certain function or a certain object method) that can be invoked at any time.

If you are happy enough to be able to use C++11, you can also assign C++ lambda to Callback.

There are several basic callback types, depending on number of parameters passed and return value. In order to keep description of callbacks short, all these types are described in a single "parametrized" description, with parameters highlighted.

 

Callback types:

 

CallbackType

parameters

arguments

return_type

Callback

none

none

void

Callback1

class P1

P1

void

Callback2

class P1, class P2

P1, P2

void

Callback3

class P1, class P2, class P3

P1, P2, P3

void

Callback4

class P1, class P2, class P3, class P4

P1, P2, P3, P4

void

Gate

none

none

bool

Gate1

class P1

P1

bool

Gate2

class P1, class P2

P1, P2

bool

Gate3

class P1, class P2, class P3

P1, P2, P3

bool

Gate4

class P1, class P2, class P3, class P4

P1, P2, P3, P4

bool

 

[ template <parameters> ]

class CallbackType

 

Callback type. Callbacks are moveable types with fast deep copy (using reference counting).

 

 

explicit CallbackType(CallbackTypeAction<arguments> *newaction)

Constructs callback based from new action.

newaction

Action. Must be created using new operator. Callback takes over its ownership.

 

 

CallbackType()

Creates an empty callback.

 


 

Callback1& operator=(std::function<void (arguments)> lambda)

Only in C++ 11

Assigns C++11 lambda to callback.

 


 

CallbackType(const CallbackTypec)

Copy constructor.

c

Source callback.

 


 

~CallbackType()

Destructor.

 


 

void Clear()

Empties the callback.

 


 

void ClearTrue()

Only in Gate callbacks.

Clears Gate. After this method is invoked, the Gate performs no actions but returns true.

 


 

void ClearFalse()

Only in Gate callbacks.

Clears Gate. After this method is invoked, the Gate performs no actions and returns false. This is the same as Clear.

 


 

operator bool() const

Return value

True if the callback is valid. Invoking a valid callback invokes an action.

 


 

return_type Execute(arguments) const

Executes the callback with given set of arguments.

 


 

return_type operator()(arguments) const

Same as the Execute method.

 

 

[ template <parameters> ]

struct CallbackTypeAction

This is the abstract base class for callback action implementations. Callback holds reference counted pointer to instance of class derived from this class.

 

 

CallbackTypeAction()

Constructor. Assigns 1 to the reference count.

 


 

~CallbackTypeAction()

Virtual destructor.

 


 

virtual return_type Execute(arguments) = 0

Performs the action with specified set of arguments.

 


 

virtual bool IsValid() const

Return value

true if the Action is valid. Default implementation returns true.

 


 

Atomic count

Member variable used as reference count.

 

 

 

 

Common functions creating callbacks

 

template <class OBJECT, class METHOD, parameters>

CallbackType<arguments> callback(OBJECT *object, void (METHOD::*method)(arguments))

Creates a callback that invokes the specified method of the specified object.

object

Pointer to the object.

method

Pointer to the method.

Return value

Created callback.

 


 

template <class OBJECT, class METHOD, parameters>

CallbackType<arguments> callback(const OBJECT *object, void (METHOD::*method)(arguments) const)

Constant variant of callback. The created Callback object invokes a constant method of the object.

object

Pointer to the object.

method

Pointer to the constant method to invoke.

Return value

Created callback.

 


 

[ template <parameters> ]

CallbackType<arguments> pteback(OBJECT *object, void (METHOD::*method)(arguments))

Creates a Callback object which is similar to the one created by callback, but which can be safely invoked even after destruction of object (results in no operation).

object

Pointer to the object. Must be an ancestor of the Pte class.

method

Pointer to the method.

Return value

Created callback.

 


 

[ template <parameters> ]

CallbackType<arguments> callback(void (*fn)(arguments))

Creates a Callback object that invokes a non-member function.

fn

Pointer to the function.

Return value

Created callback.

 


 

[ template <parameters> ]

CallbackType<arguments> Proxy(CallbackType<arguments>& cb)

Creates a Callback object that invokes another Callback object. Useful for callback routing (e.g. when some parent Ctrl wants to reroute some of its child callbacks via its interface). Note that this function stores a reference to the target callback in the created callback - storing callbacks that are used as Proxy targets in Vector flavor of containers is not a good idea.

cb

Target callback.

Return value

Created callback.

 


 

[ template <parameters> ]

CallbackType<arguments> callback(CallbackType<arguments>  cb1, CallbackType<arguments>  cb2)

Creates a Callback object that invokes two other callbacks. Note that this function stores a reference to the target callbacks in the created callback - storing callbacks that are used as targets here in Vector flavor of containers is not a good idea. For Gate callbacks, the result of this operation is the boolean value returned by cb2.

cb1

First target callback.

cb2

Second target callback.

Return value

Created callback.

 


 

[ template <parameters> ]

CallbackType<arguments>& operator<<(CallbackType<arguments> & a, CallbackType<argumentsb)

Operator variant of the previous function. Allows chaining thus imitating callback insertion.

a

First callback. Its value is replaced by a callback that calls both the first and the second callback.

b

Second callback.

Return value

Reference to the first callback.

 

 

Special functions creating callbacks

 

template <class Object, class M, class P, class T>

Callback callback1(Object *object, void (M::*method)(P), T arg)

Returns a no-parameter callback for a method expecting a single parameter. The parameter is supplied as arg and stored in the Callback.

object

Object.

method

Method pointer.

arg

Argument

Return value

Created callback.

 


 

template <class Object, class M, class P, class T>

Callback callback1(const Object *object, void (M::*method)(P) const, T arg)

Constant variant of the previous callback1 routine.

object

Object.

method

Method pointer.

arg

Argument

Return value

Created callback.

 


 

template <class Object, class M, class P, class T>

Callback pteback1(Object *object, void (M::*method)(P), T arg)

Returns a no-parameter callback for a method expecting a single parameter. Parameter is supplied as arg and stored in the Callback. The created Callback object can be safely invoked even after the object instance is destructed.

object

Object. Must be an ancestor of Pte.

method

Method pointer.

arg

Argument

Return value

Created callback.

 


 

template <class T, class P>

Callback callback1(void (*fn)(P), T arg)

Returns a no-parameter callback for a non-member function expecting a single parameter. Parameter is supplied as arg and stored in the Callback.

fn

Function.

arg

Argument.

Return value

Created callback.

 


 

template <class Object, class M, class P1, class P, class T>

Callback1<P1callback1(Object *object, void (M::*method)(P1, P), T arg)

Returns a single-parameter callback for a method expecting two parameters. Additional parameter is supplied as arg and stored in the Callback.

object

Object.

method

Method pointer.

arg

Second argument to the method.

Return value

Created callback.

 


 

template <class Object, class M, class P1, class P, class T>

Callback1<P1callback1(const Object *object, void (M::*method)(P1, P) const, T arg)

Constant variant of the previous callback.

object

Object.

method

Method pointer.

arg

Second argument to the method.

Return value

Created callback.

 


 

template <class Object, class M, class P1, class P, class T>

Callback1<P1pteback1(Object *object, void (M::*method)(P1, P), T arg)

Returns a single-parameter callback for a method expecting two parameters. Additional parameter is supplied as arg and stored in the Callback. The created Callback can be safely invoked even after the object instance is destructed.

object

Object. Must be an ancestor of Pte.

method

Method pointer.

arg

Second argument to the method.

 


 

template <class T, class P1, class P>

Callback1<P1callback1(void (*fn)(P1, P), T arg)

Returns a single-parameter callback for a non-member function expecting two parameters. Additional parameter is supplied as arg and stored in the Callback.

fn

Function.

arg

Method pointer.

Return value

Created callback.

 


 

template <class T, class P>

Callback callback1(Callback1<Pcb, T arg)

Combines a single-parameter callback and an argument into a no-parameter callback.

cb

Single-parameter callback.

arg

Argument.

Return value

Created callback.

 


 

template <class T, class P1, class P>

Callback1<P1callback1(Callback2<P1, Pcb, T arg)

Combines a double-parameter callback and an argument into a single parameter callback.

cb

Double-parameter callback.

arg

Argument.

Return value

Created callback.

 


 

template <class Object, class R, class O, class A, class B, class T1, class T2>

Callback callback2(Object *object, R (O::*method)(A, B), T1 arg1, T2 arg2)

Returns a no-parameter callback for a method taking two parameters. Parameters are supplied as arg1 and arg2. They are stored in the created callback object.

object

Object.

method

Method pointer.

arg1

First argument.

arg2

Second argument.

Return value

Created callback.

 


 

template <class Object, class R, class O, class A, class B, class T1, class T2>

Callback callback2(const Object *object, R (O::*method)(A, B) const, T1 arg1, T2 arg2)

Constant version of the previous callback.

object

Object.

method

Method.

arg1

First argument.

arg2

Second argument.

Return value

Created callback.

 


 

template <class Object, class R, class O, class A, class B, class T1, class T2>

Callback pteback2(Object *object, R (O::*method)(A, B), T1 arg1, T2 arg2)

Returns a no-parameter callback for a method taking two parameters. Parameter are supplied as arg1 and arg2. They are stored in the created callback. The created Callback can be safely invoked even after the object instance is destructed.

object

Object.

method

Method pointer.

arg1

First argument.

arg2

Second argument.

Return value

Created callback.

 


 

template <class R, class A, class B, class T1, class T2>

Callback callback2(R (*fn)(A, B), T1 arg1, T2 arg2)

Returns a no-parameter callback for a non-member function taking two parameters. Parameter are supplied as arg1 and arg2. They are stored in the created callback.

fn

Function pointer.

arg1

First argument.

arg2

Second argument.

Return value

Created callback.

 


 

template <class A, class B, class T1, class T2>

Callback callback2(Callback2<A, Bcb, T1 arg1, T2 arg2)

Combines a two-parameter callback and two arguments into a no-parameter callback.

cb

Two-parameter callback.

arg1

First argument.

arg2

Second argument.

Return value

Created callback.

 

 

 

Callback macros

Callback macros reduce verbosity of instance method callbacks. They expect CLASSNAME to be typedef-ed as the type of current class and return method callbacks bound to the this pointer.

 

 

THISBACK(x)

Expands to callback(this, &CLASSNAME::x).

x

Name of method.

 


 

THISBACK1(x, arg)

Expands to callback1(this, &CLASSNAME::x, arg).

x

Name of method.

arg

Additional parameter.

 


 

THISBACK2(m, a, b)

Expands to callback2(this, &CLASSNAME::x, a, b).

m

Name of method.

a

First additional parameter.

b

Second additional parameter.

 


 

PTEBACK(x)

Expands to pteback(this, &CLASSNAME::x).

x

Name of method.

 


 

PTEBACK1(x, arg)

Expands to pteback1(this, &CLASSNAME::x, arg).

x

Name of method.

arg

Additional parameter.

 


 

PTEBACK2(m, a, b)

Expands to pteback2(this, &CLASSNAME::x, a, b).

m

Name of method.

a

First additional parameter.

b

Second additional parameter.

 

 

 

 

CallbackArgTarget

 

template <class T>

class CallbackArgTarget

This helper class is useful in a situation when a set of callbacks define some output value. Example is pop-up menu that provides selection of one character - in that case, CallbackArgTarget can be used instead of dedicated method to obtain result of user selection.

T

Type of output value. Must be able to be assigned Null.

 

 

Public Member List

 

CallbackArgTarget()

Assigns Null to the output value.

 


 

operator const T&() const

Return value

The result. If no CallbackArgTarget was invoked, returns Null.

 


 

bool IsNullInstance() const

Return value

True if there is not Null in output value.

 


 

Callback operator[](const T& value)

Returns a Callback that, when invoked, assigns value to the output value.

value

Value.

Return value

Callback.

 

 

本頁也正在english中. 你想幫忙嗎?