c++ - 通过仿函数断开 boost 信号 2 连接

标签 c++ visual-c++ boost boost-function boost-signals2

我正在尝试将一些使用不同系统的代码转换为使用 Boost.Signals2。旧代码使用常规函数指针作为仿函数;连接是通过使用仿函数调用特定方法来完成的,而断开连接是通过使用完全相同的仿函数调用另一个方法来完成的。出于兼容性原因,我仍然想支持它,即使我知道它不是 Signals2 下最有效的方法。

namespace bs2 = boost::signals2;
class Foo
{
    typedef bs2::signal<void (const Foo *foo)> UpdateEvent;
    UpdateEvent m_UpdateEvent;

public:
    typedef UpdateEvent::slot_type UpdateCallback;

    bs2::connection Register(const UpdateCallback& callback)
    {
        return m_UpdateEvent.connect(callback);
    }

    void Unregister(const UpdateCallback& callback)
    {
        m_UpdateEvent.disconnect(callback);
    }
};

上面的(简化的)代码可以很好地连接,但是 Unregister 方法不会在 VS2008 中编译:

T:\boost\boost_1_47_0\boost/function_equal.hpp(17) : error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const Foo::UpdateCallback' (or there is no acceptable conversion)
    T:\boost\boost_1_47_0\boost/function/function_base.hpp(808): could be 'bool boost::operator ==<T>(const boost::function_base &,Functor)'
    with
    [
        T=Foo::UpdateCallback,
        Functor=Foo::UpdateCallback
    ]
    T:\boost\boost_1_47_0\boost/function/function_base.hpp(817): or       'bool boost::operator ==<F>(Functor,const boost::function_base &)'
    with
    [
        F=Foo::UpdateCallback,
        Functor=Foo::UpdateCallback
    ]
    C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\guiddef.h(192): or       'int operator ==(const GUID &,const GUID &)' [found using argument-dependent lookup]
    T:\boost\boost_1_47_0\boost/function/function_base.hpp(746): or       'bool boost::operator ==(const boost::function_base &,boost::detail::function::useless_clear_type *)'
    T:\boost\boost_1_47_0\boost/function/function_base.hpp(758): or       'bool boost::operator ==(boost::detail::function::useless_clear_type *,const boost::function_base &)'
    T:\boost\boost_1_47_0\boost/blank.hpp(58): or       'bool boost::operator ==(const boost::blank &,const boost::blank &)'
    while trying to match the argument list '(const Foo::UpdateCallback, const Foo::UpdateCallback)'
    T:\boost\boost_1_47_0\boost/function_equal.hpp(24) : see reference to function template instantiation 'bool boost::function_equal_impl<F,G>(const F &,const G &,long)' being compiled
    with
    [
        F=Foo::UpdateCallback,
        G=Foo::UpdateCallback
    ]
    T:\boost\boost_1_47_0\boost/function/function_base.hpp(811) : see reference to function template instantiation 'bool boost::function_equal<Functor,Functor>(const F &,const G &)' being compiled
    with
    [
        Functor=Foo::UpdateCallback,
        F=Foo::UpdateCallback,
        G=Foo::UpdateCallback
    ]
    T:\boost\boost_1_47_0\boost/signals2/detail/signal_template.hpp(527) : see reference to function template instantiation 'bool boost::operator ==<T>(const boost::function_base &,Functor)' being compiled
    with
    [
        T=Foo::UpdateCallback,
        Functor=Foo::UpdateCallback
    ]
    T:\boost\boost_1_47_0\boost/signals2/detail/signal_template.hpp(221) : see reference to function template instantiation 'void boost::signals2::detail::signal1_impl<R,T1,Combiner,Group,GroupCompare,SlotFunction,ExtendedSlotFunction,Mutex>::do_disconnect<T>(const T &,boost::mpl::bool_<C_>)' being compiled
    with
    [
        R=void,
        T1=const Foo *,
        Combiner=boost::signals2::optional_last_value<void>,
        Group=int,
        GroupCompare=std::less<int>,
        SlotFunction=boost::function<void (const Foo *)>,
        ExtendedSlotFunction=boost::function<void (const boost::signals2::connection &,const Foo *)>,
        Mutex=boost::signals2::mutex,
        T=Foo::UpdateCallback,
        C_=false
    ]
    T:\boost\boost_1_47_0\boost/signals2/detail/signal_template.hpp(691) : see reference to function template instantiation 'void boost::signals2::detail::signal1_impl<R,T1,Combiner,Group,GroupCompare,SlotFunction,ExtendedSlotFunction,Mutex>::disconnect<T>(const T &)' being compiled
    with
    [
        R=void,
        T1=const Foo *,
        Combiner=boost::signals2::optional_last_value<void>,
        Group=int,
        GroupCompare=std::less<int>,
        SlotFunction=boost::function<void (const Foo *)>,
        ExtendedSlotFunction=boost::function<void (const boost::signals2::connection &,const Foo *)>,
        Mutex=boost::signals2::mutex,
        T=Foo::UpdateCallback
    ]
    .\Foo.cpp(72) : see reference to function template instantiation 'void boost::signals2::signal1<R,T1,Combiner,Group,GroupCompare,SlotFunction,ExtendedSlotFunction,Mutex>::disconnect<Foo::UpdateCallback>(const T &)' being compiled
    with
    [
        R=void,
        T1=const Foo *,
        Combiner=boost::signals2::optional_last_value<void>,
        Group=int,
        GroupCompare=std::less<int>,
        SlotFunction=boost::function<void (const Foo *)>,
        ExtendedSlotFunction=boost::function<void (const boost::signals2::connection &,const Foo *)>,
        Mutex=boost::signals2::mutex,
        T=Foo::UpdateCallback
    ]

所以,函数对象不能以某种方式与自身进行比较?我还尝试将 slot_function_type 用于 UpdateCallback,并将其明确指定为具有相同签名的 boost::function;这两个都产生了这个错误:

T:\boost\boost_1_47_0\boost/signals2/detail/signal_template.hpp(527) : error C2666: 'boost::signals2::detail::operator ==' : 4 overloads have similar conversions
    T:\boost\boost_1_47_0\boost/function/function_template.hpp(997): could be 'void boost::operator ==<R,T0>(const boost::function1<R,T0> &,const boost::function1<R,T0> &)' [found using argument-dependent lookup]
    with
    [
        R=void,
        T0=const Foo *
    ]
    T:\boost\boost_1_47_0\boost/function/function_base.hpp(817): or       'bool boost::operator ==<boost::function<Signature>>(Functor,const boost::function_base &)' [found using argument-dependent lookup]
    with
    [
        Signature=void (const Foo *),
        Functor=boost::function<void (const Foo *)>
    ]
    T:\boost\boost_1_47_0\boost/function/function_base.hpp(808): or       'bool boost::operator ==<T>(const boost::function_base &,Functor)' [found using argument-dependent lookup]
    with
    [
        T=Foo::UpdateCallback,
        Functor=Foo::UpdateCallback
    ]
    or       'built-in C++ operator==(void (__thiscall boost::function1<R,T0>::dummy::* )(void), void (__thiscall boost::function1<R,T0>::dummy::* )(void))'
    with
    [
        R=void,
        T0=const Foo *
    ]
    while trying to match the argument list '(boost::function<Signature>, const Foo::UpdateCallback)'
    with
    [
        Signature=void (const Foo *)
    ]
    T:\boost\boost_1_47_0\boost/signals2/detail/signal_template.hpp(221) : see reference to function template instantiation 'void boost::signals2::detail::signal1_impl<R,T1,Combiner,Group,GroupCompare,SlotFunction,ExtendedSlotFunction,Mutex>::do_disconnect<T>(const T &,boost::mpl::bool_<C_>)' being compiled
    with
    [
        R=void,
        T1=const Foo *,
        Combiner=boost::signals2::optional_last_value<void>,
        Group=int,
        GroupCompare=std::less<int>,
        SlotFunction=boost::function<void (const Foo *)>,
        ExtendedSlotFunction=boost::function<void (const boost::signals2::connection &,const Foo *)>,
        Mutex=boost::signals2::mutex,
        T=Foo::UpdateCallback,
        C_=false
    ]
    T:\boost\boost_1_47_0\boost/signals2/detail/signal_template.hpp(691) : see reference to function template instantiation 'void boost::signals2::detail::signal1_impl<R,T1,Combiner,Group,GroupCompare,SlotFunction,ExtendedSlotFunction,Mutex>::disconnect<T>(const T &)' being compiled
    with
    [
        R=void,
        T1=const Foo *,
        Combiner=boost::signals2::optional_last_value<void>,
        Group=int,
        GroupCompare=std::less<int>,
        SlotFunction=boost::function<void (const Foo *)>,
        ExtendedSlotFunction=boost::function<void (const boost::signals2::connection &,const Foo *)>,
        Mutex=boost::signals2::mutex,
        T=Foo::UpdateCallback
    ]
    .\Foo.cpp(72) : see reference to function template instantiation 'void boost::signals2::signal1<R,T1,Combiner,Group,GroupCompare,SlotFunction,ExtendedSlotFunction,Mutex>::disconnect<Foo::UpdateCallback>(const T &)' being compiled
    with
    [
        R=void,
        T1=const Foo *,
        Combiner=boost::signals2::optional_last_value<void>,
        Group=int,
        GroupCompare=std::less<int>,
        SlotFunction=boost::function<void (const Foo *)>,
        ExtendedSlotFunction=boost::function<void (const boost::signals2::connection &,const Foo *)>,
        Mutex=boost::signals2::mutex,
        T=Foo::UpdateCallback
    ]

所以我猜它已经从选择太少变成了太多。我该如何解决这个问题?

最佳答案

boost::function 没有实现 operator==,所以我们不能像 f1 == f2 那样进行比较。 当我们需要断开函数与信号的连接时,这会导致 boost::signal 库出现问题 通过使用 boost::function 作为参数。 boost::signal 尝试通过将参数与其自身的函数进行比较来查找函数,这会导致错误。我的解决方案如下。还提出了其他可能的解决方案,例如存储连接......如果我的方法适合你,你应该选择它,因为它不存储任何东西,所以你不需要处理手动连接管理。

class Button {
public:

    typedef void (*EventHandler)();
    typedef boost::signal<void ()> Event;

    Button () {
    }

    virtual ~Button () {
    }

    void addClickHandler(EventHandler handler) {
        clickEvent.connect(handler);
    }

    void removeClickHandler(EventHandler handler) {
        clickEvent.disconnect(handler);
    }

    void click() {
        clickEvent();
    }

private:
    Event clickEvent;
};

void print() {
    cout << "print" << endl;
}

void app1_TestButton() {
    Button b;
    b.addClickHandler(print);
    b.click();

    b.removeClickHandler(print);
    b.click();
}

关于c++ - 通过仿函数断开 boost 信号 2 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11096429/

相关文章:

c++ - MSVC C++ ADL 错误?

c++ - 为什么在 Visual C++ 2008 中链接静态运行时库在运行时仍然需要 MSVCR71.dll?

c++ - 如何将 boost::bind(&myClass::fun, this, _1, _2, _3) 转换为 typedef void (*fun)(arg1, arg2, arg3)?

boost - Boost、OpenCV 和 Eigen 库之间存在冲突?

c++ - 如何通过不同版本的tcp传输不同的对象

c++ - 作为成员变量的 vector 上的基于范围的 for 循环

c++ - QPlainText 的子类不会扩展以填充布局

c++ - .exe 窗口退出后

c++ - 如何在 Windows 上启用 ssl 的情况下编译 gSoap?

c++ - 如何测试 std::memory_order_relaxed 的行为?