C++类成员回调

发布于 2024-11-06 15:20:45 字数 2626 浏览 5 评论 0原文

我编译此代码时出错:

template <class T> class purple_multitimer {
public:

typedef struct _timerinfo timerinfo, *ptimerinfo;
typedef gboolean (T::*multitimer_callback) (ptimerinfo pti);
typedef struct _timerinfo {
    guint id;
    multitimer_callback cb;
    T * pthis;
    purple_multitimer<T> * pmt;
} timerinfo, *ptimerinfo;

    purple_multitimer() {
        memset(m_ti, 0, sizeof(m_ti));
    }

    ~purple_multitimer() {
        stop();
    }

    void start(multitimer_callback mt_cb, T * pthis, guint timeout = 10) {
        ptimerinfo pti = ti_get();
        assert(pti);
        pti->pthis = pthis;
        pti->pmt = this;
        pti->cb = mt_cb;
        pti->id = purple_timeout_add_seconds(timeout, GSourceFunc(timeout_cb), pti);
    }

    void stop(multitimer_callback mt_cb = NULL) {
        for (register guint n = 0; n < sizeof(m_ti)/sizeof(timerinfo); ++ n)
            if (m_ti[n].cb == mt_cb) {
                purple_timeout_remove(m_ti[n].id);
                ti_zero(n);
            }
    }

private:
    timerinfo m_ti[32];

    inline ptimerinfo ti_get(guint n) {
        return &m_ti[n];
    }

    inline ptimerinfo ti_get() {
        for (register guint n = 0; n < sizeof(m_ti)/sizeof(timerinfo); ++ n)
            if (m_ti[n].id == 0) return &m_ti[n];
        return NULL;
    }

    inline ptimerinfo ti_zero(ptimerinfo pti) {
        memset(pti, 0, sizeof(timerinfo));
        return pti;
    }

    inline ptimerinfo ti_zero(guint n) {
        memset(&m_ti[n], 0, sizeof(timerinfo));
        return &m_ti[n];
    }

    static gboolean timeout_cb(ptimerinfo pti) {
        gboolean res = (pti->pthis->*(pti->cb))(pti);
        if (!res) pti->pmt->stop(pti->cb);
        return res;
    }
};

class _ctrl {
    public:
    purple_multitimer<_ctrl> pmt;

    gboolean on_tomeout (purple_multitimer<_ctrl>::ptimerinfo pti) {
        return false;
    };

    void on_connected(PurpleConnection *gc) {
        pmt.start(purple_multitimer<_ctrl>::multitimer_callback(&_ctrl::on_tomeout), this);
    }

    void on_disconnected(PurpleConnection *gc) {
    }
} controller;

编译此代码时出现错误:

[Error] E:\dnc-imexchange\dnc-imexchange.cpp:117: error: no matching function for call to `purple_multitimer<_ctrl>::start(gboolean (_ctrl::*)(_timerinfo*), _ctrl* const)'
[Warning] E:\dnc-imexchange\dnc-imexchange.cpp:52: note: candidates are: void purple_multitimer<T>::start(gboolean (T::*)(_timerinfo*), T*, guint) [with T = _ctrl]

我需要以这种方式实现回调。

I have an error compiling this code:

template <class T> class purple_multitimer {
public:

typedef struct _timerinfo timerinfo, *ptimerinfo;
typedef gboolean (T::*multitimer_callback) (ptimerinfo pti);
typedef struct _timerinfo {
    guint id;
    multitimer_callback cb;
    T * pthis;
    purple_multitimer<T> * pmt;
} timerinfo, *ptimerinfo;

    purple_multitimer() {
        memset(m_ti, 0, sizeof(m_ti));
    }

    ~purple_multitimer() {
        stop();
    }

    void start(multitimer_callback mt_cb, T * pthis, guint timeout = 10) {
        ptimerinfo pti = ti_get();
        assert(pti);
        pti->pthis = pthis;
        pti->pmt = this;
        pti->cb = mt_cb;
        pti->id = purple_timeout_add_seconds(timeout, GSourceFunc(timeout_cb), pti);
    }

    void stop(multitimer_callback mt_cb = NULL) {
        for (register guint n = 0; n < sizeof(m_ti)/sizeof(timerinfo); ++ n)
            if (m_ti[n].cb == mt_cb) {
                purple_timeout_remove(m_ti[n].id);
                ti_zero(n);
            }
    }

private:
    timerinfo m_ti[32];

    inline ptimerinfo ti_get(guint n) {
        return &m_ti[n];
    }

    inline ptimerinfo ti_get() {
        for (register guint n = 0; n < sizeof(m_ti)/sizeof(timerinfo); ++ n)
            if (m_ti[n].id == 0) return &m_ti[n];
        return NULL;
    }

    inline ptimerinfo ti_zero(ptimerinfo pti) {
        memset(pti, 0, sizeof(timerinfo));
        return pti;
    }

    inline ptimerinfo ti_zero(guint n) {
        memset(&m_ti[n], 0, sizeof(timerinfo));
        return &m_ti[n];
    }

    static gboolean timeout_cb(ptimerinfo pti) {
        gboolean res = (pti->pthis->*(pti->cb))(pti);
        if (!res) pti->pmt->stop(pti->cb);
        return res;
    }
};

class _ctrl {
    public:
    purple_multitimer<_ctrl> pmt;

    gboolean on_tomeout (purple_multitimer<_ctrl>::ptimerinfo pti) {
        return false;
    };

    void on_connected(PurpleConnection *gc) {
        pmt.start(purple_multitimer<_ctrl>::multitimer_callback(&_ctrl::on_tomeout), this);
    }

    void on_disconnected(PurpleConnection *gc) {
    }
} controller;

When compiling this code got error:

[Error] E:\dnc-imexchange\dnc-imexchange.cpp:117: error: no matching function for call to `purple_multitimer<_ctrl>::start(gboolean (_ctrl::*)(_timerinfo*), _ctrl* const)'
[Warning] E:\dnc-imexchange\dnc-imexchange.cpp:52: note: candidates are: void purple_multitimer<T>::start(gboolean (T::*)(_timerinfo*), T*, guint) [with T = _ctrl]

I need to implement callbacks in such way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

公布 2024-11-13 15:20:45

如果您想要一些高质量的回调(能够一次调用多个函数,适合观察者模式),我可以建议 boost::signals2

如果您只想调用一个函数作为回调,您可以使用 std::function:

void Foo(const std::function<bool (const int)> &callback)
{
    const int number = 4;
    if (callback(number))
    {
        std::cout << "Callback returned true!" << std::endl;
    }
    else
    {
        std::cout << "Callback returned false!" << std::endl;
    }
}

// Use this if you have C++11

void CallFooLambda()
{
    const auto lambda = [](const int number) -> bool
    {
        return number % 2;
    };

    Foo(lambda);
}

// Else use these functions

bool FooCallback(const int number)
{
    return number % 2;
}

void CallFoo()
{
    Foo(&FooCallback);    
}

If you want some good quality callbacks (able to call multiple functions at once, suitable for observer pattern), may I suggest boost::signals2.

If you just want to call one function as a callback you can use std::function:

void Foo(const std::function<bool (const int)> &callback)
{
    const int number = 4;
    if (callback(number))
    {
        std::cout << "Callback returned true!" << std::endl;
    }
    else
    {
        std::cout << "Callback returned false!" << std::endl;
    }
}

// Use this if you have C++11

void CallFooLambda()
{
    const auto lambda = [](const int number) -> bool
    {
        return number % 2;
    };

    Foo(lambda);
}

// Else use these functions

bool FooCallback(const int number)
{
    return number % 2;
}

void CallFoo()
{
    Foo(&FooCallback);    
}
微凉 2024-11-13 15:20:45

_ctrl 是一个 const 指针,您尝试调用的函数需要一个非常量 ptr-to _ctrl (pthis)。

The _ctrl is a const pointer, and the function you try to call require a non-const ptr-to _ctrl (pthis).

咿呀咿呀哟 2024-11-13 15:20:45

你能按如下方式定义pthis吗?

T *pthis const

这应该使您的代码与错误消息中的“候选”匹配。

this 是一个无法更改的指针。

Can you define pthis as follows?

T *pthis const

That should make your code match the 'candidate' in the error message.

this is a pointer that you can't change.

谎言月老 2024-11-13 15:20:45

Boost.Function 是一个很好的简化回调语法的工具包和实施。

Boost.Function 库包含一个
类模板系列是
函数对象包装器。概念
类似于广义回调。
它与功能共享特征
指针都定义了一个调用
接口(例如,一个函数需要两个
整数参数并返回
浮点值)通过其中
可以调用一些实现,并且
被调用的实现可能
在整个过程中发生变化
程序。

一般来说,任何地方
函数指针将用于
推迟通话或回电,
Boost.Function 可以用来代替
让用户有更大的灵活性
目标的实施。
目标可以是任何“兼容”的
函数对象(或函数指针),
这意味着论点
Boost.Function指定的接口
可以转换为参数
目标函数对象。

Boost.Function is a good toolkit for simplifying callback syntax and implementation.

The Boost.Function library contains a
family of class templates that are
function object wrappers. The notion
is similar to a generalized callback.
It shares features with function
pointers in that both define a call
interface (e.g., a function taking two
integer arguments and returning a
floating-point value) through which
some implementation can be called, and
the implementation that is invoked may
change throughout the course of the
program.

Generally, any place in which a
function pointer would be used to
defer a call or make a callback,
Boost.Function can be used instead to
allow the user greater flexibility in
the implementation of the target.
Targets can be any 'compatible'
function object (or function pointer),
meaning that the arguments to the
interface designated by Boost.Function
can be converted to the arguments of
the target function object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文