3

我正在尝试将一些使用不同系统的代码转换为使用 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
    ]

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

4

1 回答 1

0

boost::function 没有实现 operator==,所以我们不能像 f1 == f2 那样进行比较。当我们需要使用 boost::function 作为参数断开函数与信号的连接时,这会导致 boost::signal 库出现问题。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();
}
于 2014-05-29T20:07:08.950 回答