Callback用法
Callback
Callback 的本质,实际上是一种指针到成员函数 的 封装。 用来在不同的widgets控件中注册回调函数。
比如,当一个Button控件被点击时,可以执行一个成员函数。
该类是一个模板,用于提供成员函数所驻留的对象的类类型以及要调用的函数的参数类型。
有点拗口,什么意思呢?
驻留的对象,
要从GenericCallback说起,
//GenericCallback is the base class for callbacks.
//The reason this base class exists, is that a normal Callback requires the class type
//where the callback function resides to be known.
就是说这个callback要知道是驻留在哪个类中的。在声明的时候,就要初始化好。
class MainView : public MainViewBase
protected:
// Callback that is assigned to each list element
Callback<MainView, CustomListElement&> listElementClickedCallback; //listElementClickedCallback 是一个Callback类型的成员。
}
模板的參數
- dest_type The type of the class in which the member function
- resides. T1 The type of the first argument in the member function,or void if none.
- T2 The type of the second argument in the member function, or void if none.
- T3 The type of the third argument in the member function, or void if none.
Callback实现
/**
* A Callback is basically a wrapper of a pointer-to-member-function.
*
* It is used for registering callbacks between widgets. For instance, a Button can be
* configured to call a member function when it is clicked.
*
* The class is templated in order to provide the class type of the object in which the
* member function resides, and the argument types of the function to call.
*
* The Callback class exists in four versions, for supporting member functions with 0, 1,
* 2 or 3 arguments. The compiler will infer which type to use automatically.
*
* @tparam dest_type The type of the class in which the member function resides.
* @tparam T1 The type of the first argument in the member function, or void if none.
*
* @note The member function to call must return void. The function can have zero, one, two or
* three arguments of any type.
*/
template <class dest_type, typename T1>
struct Callback<dest_type, T1, void, void> : public GenericCallback<T1>
{
/*...
*...
*.../
};
template <class dest_type, typename T1>
- class dest_type 就是告诉Callback类,当前该Callback是驻留在哪个类中的。如同MainView这个dest_type参数。
class MainView : public MainViewBase
protected:
// Callback that is assigned to each list element
Callback<MainView, CustomListElement&> listElementClickedCallback;
}
- typename T1是传入的第二个参数,那么该参数表示一个类型名。 它和class还是有区别的。比如Callback<MainView, CustomListElement&> 的第二个参数,CustomListElement, 可以换成其他参数。
相当于“类型参数化”, 将类型作为参数传给Callback。
/**
* Initializes a Callback with an object and a pointer to the member function in that
* object to call.
*
* @param [in] pobject Pointer to the object on which the function should be called.
* @param [in] pmemfun_1 Address of member function. This is the version where function takes
* one argument.
*/
Callback(dest_type* pobject, void (dest_type::*pmemfun_1)(T1))
{
this->pobject = pobject;
this->pmemfun_1 = pmemfun_1;
}
注意“Initializes a Callback with an object and a pointer to the member function in that object to call.”
意思就是说,初始化一个Callback类,使用对象和指向该对象中要调用的成员函数的指针来初始化Callback.
- dest_type *pobject是对像
- void (dest_type::*pmemfun_1) 对象中的成员函数
-初始化部分
MainView::MainView()
: listElementClickedCallback(this, &MainView::listElementClicked)
{
/*...*/
}