touchGFX 之 Callback 和 GenericCallbak

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类型的成员。
}

模板的參數

  1. dest_type The type of the class in which the member function
  2. resides. T1 The type of the first argument in the member function,or void if none.
  3. T2 The type of the second argument in the member function, or void if none.
  4. 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)
{
    /*...*/
}

关系图

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值