不包括 boost 信号调用

发布于 2024-08-07 00:20:13 字数 719 浏览 6 评论 0 原文

有一个信号和几个带槽的对象。我想实现当一个对象调用信号并阻止其自己的连接时的行为。我想一个小片段会提供更多信息:


typedef boost::signal<void()> TSignal;

template<class TSignal>
class SlotObject
{
public:

    void Connect(boost::shared_ptr<TSignal> pSignal, boost::function slot)
    {
        m_connection = pSignal->connect(slot);
        m_pSignal = pSignal;
    }

    // How to define TSignal signature here?
    VOID Call()
    {
        m_connection.block();
        (*m_pSignal)();
        m_connection.unblock();
    }

    boost::shared_ptr<TSignal> m_pSignal;
    boost::signals::connection m_connection;
};

问题:

  1. 是否有一些增强内容的标准方法?我要重新发明轮子吗?
  2. 如何使用 TSignal 签名定义 Call 方法?

There is a signal and several objects with slots. I want to implement the behavior when one object calls signal and blocks its own connection. I guess a small snippet will be more informative:


typedef boost::signal<void()> TSignal;

template<class TSignal>
class SlotObject
{
public:

    void Connect(boost::shared_ptr<TSignal> pSignal, boost::function slot)
    {
        m_connection = pSignal->connect(slot);
        m_pSignal = pSignal;
    }

    // How to define TSignal signature here?
    VOID Call()
    {
        m_connection.block();
        (*m_pSignal)();
        m_connection.unblock();
    }

    boost::shared_ptr<TSignal> m_pSignal;
    boost::signals::connection m_connection;
};

The questions:

  1. Is there a standard approach with some boost stuff? Do I reinventing the wheel?
  2. How to define Call method with TSignal signature?

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

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

发布评论

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

评论(1

似最初 2024-08-14 00:20:13

对于你的第一个问题:我不知道有“标准提升方式”来实现你想要的。您可以将问题发布到 Boost 用户邮件列表

对于第二个问题:如果没有可变模板和右值引用,转发总是很麻烦。

一些建议,排名不分先后:

1) 您可以查看 boost/signal.hpp 和 boost/signals/ 中的文件,以了解如何完成此类工作预处理器,但这里有一个部分实现来展示这个想法(警告:未经测试):

template<size_t Arity, class SignalT>
struct SlotBase;

template<class SignalT>
struct SlotBase<0, SignalT>
{
    typedef SignalT::slot_function_type SlotType;

    SlotBase(boost::shared_ptr<SignalT> S, SlotType F)
        : m_Signal(S), m_Connection(S->connect(F))){};

    void operator()()const
    {
        m_Connection.block();
        m_Signal();
        m_Connection.unblock()
    };

private:
    boost::shared_ptr<SignalT> > m_Signal;
    boost::signals::connection m_Connection;
};

template<class SignalT>
struct SlotBase<1, SignalT>
{
    // as above, except for operator()
    // ...

    void operator()(typename SignalT::arg1_type arg1)
    {
        m_Connection.block();
        m_Signal(arg1);
        m_Connection.unblock();
    };
};

template<class SignalT>
struct SlotBase<2, SignalT>
{
    // as above, except for operator()
    // ...

    void operator()(typename SignalT::arg1_type arg1, typename SignalT::arg2_type arg2)
    {
        m_Connection.block();
        m_Signal(arg1, arg2);
        m_Connection.unblock()
    };
};

// repeat for other arities
// ...

template<class SignalT>
class SlotObject : public SlotBase<SignalT::arity, SignalT>
{
    typedef SlotBase<SignalT::arity, SignalT> BaseType;

public:
    Slot(boost::shared_ptr<SignalT>S, 
         typename SignalT::slot_function_type F
    ) : BaseType(S, F)
    {}
};

2) 如果您愿意为 SlotObject 的用户放弃一些语法细节,其他事情也是可能的。一种是使用 boost::shared_ptr 文档中显示的技术包装对信号的调用(http://www.boost.org/doc/libs/1_40_0/libs/smart_ptr/sp_techniques.html#wrapper),即您的 Call() 方法将阻止 m_connection ,并将一个shared_ptr返回给m_signal,该m_signal具有一个可以解除m_connection阻塞的自定义删除器。

遗憾的是,这并没有为调用者提供良好的语法。它看起来像:

SlotObject<signal<void(int, float)> > s = ...;
s.Call()->operator()(1, 1.234);

3) 另一种选择是要求用户将参数打包在一个元组中(我使用的是 boost::fusion::vector 在调用站点,并使用 boost::fusion:: :fused 解压它们并调用信号。

#include <boost/function_types/parameter_types.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/include/mpl.hpp>
#include <boost/fusion/include/fused.hpp>
#include <boost/signal.hpp>
#include <boost/shared_ptr.hpp>

// Metafunction to extract the Signature template parameter
// from a boost::signal instantiation
// For example, SignatureOf<signal<void(int, float)>::type 
// is "void(int, float)"
template<class SignalT>
struct SignatureOf;

template<
    typename Signature, typename Combiner, typename Group,
    typename GroupCompare, typename SlotFunction
>
struct SignatureOf<
    boost::signal<Signature, Combiner, Group, GroupCompare, SlotFunction>
>
{
    typedef Signature type;
};

// The SlotObject    
template<class SignalT>
class SlotObject
{
public:
    typedef typename SignatureOf<SignalT>::type SignatureType;

    // Defines the "packed" parameters type corresponding
    // to the slot's signature
    // For example, for a SignalT of boost::signal<void(int, float)>
    // ArgsType is "boost::fusion::vector<int, float>"
    typedef typename boost::fusion::result_of::as_vector<
        typename boost::function_types::parameter_types<SignatureType>::type
    >::type ArgsType;

    void Call(ArgsType P)
    {
        m_Connection.block();
        boost::fusion::fused<SignalT&> f(*m_Signal);
        f(P);
        m_Connection.unblock();
    }

    //...
};

这将用作:

typedef SlotObject<boost::signal<void(int, float)> > SlotType;
SlotType s = ...;
s.Call(SlotType::ArgsType(1, "foo"));

For your first question: I'm not aware of a "standard boost way" to achieve what you want. You may post your question to the boost users mailing list.

For your second question: Without varidic templates and rvalue references, forwarding is always cumbersome.

A few suggestions, in no particular order:

1) You may look at the boost/signal.hpp and the files in boost/signals/ to get an idea of how this kind of stuff can be done with the preprocessor, but here's a partial implementation to show the idea(warning: untested):

template<size_t Arity, class SignalT>
struct SlotBase;

template<class SignalT>
struct SlotBase<0, SignalT>
{
    typedef SignalT::slot_function_type SlotType;

    SlotBase(boost::shared_ptr<SignalT> S, SlotType F)
        : m_Signal(S), m_Connection(S->connect(F))){};

    void operator()()const
    {
        m_Connection.block();
        m_Signal();
        m_Connection.unblock()
    };

private:
    boost::shared_ptr<SignalT> > m_Signal;
    boost::signals::connection m_Connection;
};

template<class SignalT>
struct SlotBase<1, SignalT>
{
    // as above, except for operator()
    // ...

    void operator()(typename SignalT::arg1_type arg1)
    {
        m_Connection.block();
        m_Signal(arg1);
        m_Connection.unblock();
    };
};

template<class SignalT>
struct SlotBase<2, SignalT>
{
    // as above, except for operator()
    // ...

    void operator()(typename SignalT::arg1_type arg1, typename SignalT::arg2_type arg2)
    {
        m_Connection.block();
        m_Signal(arg1, arg2);
        m_Connection.unblock()
    };
};

// repeat for other arities
// ...

template<class SignalT>
class SlotObject : public SlotBase<SignalT::arity, SignalT>
{
    typedef SlotBase<SignalT::arity, SignalT> BaseType;

public:
    Slot(boost::shared_ptr<SignalT>S, 
         typename SignalT::slot_function_type F
    ) : BaseType(S, F)
    {}
};

2) If you are willing to give up a bit of syntax nicety for the users of SlotObject, other things are possible. One is to wrap the call to the signal using the technique shown in boost::shared_ptr documentation (http://www.boost.org/doc/libs/1_40_0/libs/smart_ptr/sp_techniques.html#wrapper), ie, your Call() method would block the m_connection, and return a shared_ptr to m_signal having a custom deleter that unblocks m_connection.

Sadly, this does not give a nice syntax to the caller. It would look like:

SlotObject<signal<void(int, float)> > s = ...;
s.Call()->operator()(1, 1.234);

3) Another alternative is to ask the user to package the arguments in a tuple (I'm using a boost::fusion::vector below) at the call site, and use boost::fusion:::fused to unpack them and call the signal.

#include <boost/function_types/parameter_types.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/fusion/include/mpl.hpp>
#include <boost/fusion/include/fused.hpp>
#include <boost/signal.hpp>
#include <boost/shared_ptr.hpp>

// Metafunction to extract the Signature template parameter
// from a boost::signal instantiation
// For example, SignatureOf<signal<void(int, float)>::type 
// is "void(int, float)"
template<class SignalT>
struct SignatureOf;

template<
    typename Signature, typename Combiner, typename Group,
    typename GroupCompare, typename SlotFunction
>
struct SignatureOf<
    boost::signal<Signature, Combiner, Group, GroupCompare, SlotFunction>
>
{
    typedef Signature type;
};

// The SlotObject    
template<class SignalT>
class SlotObject
{
public:
    typedef typename SignatureOf<SignalT>::type SignatureType;

    // Defines the "packed" parameters type corresponding
    // to the slot's signature
    // For example, for a SignalT of boost::signal<void(int, float)>
    // ArgsType is "boost::fusion::vector<int, float>"
    typedef typename boost::fusion::result_of::as_vector<
        typename boost::function_types::parameter_types<SignatureType>::type
    >::type ArgsType;

    void Call(ArgsType P)
    {
        m_Connection.block();
        boost::fusion::fused<SignalT&> f(*m_Signal);
        f(P);
        m_Connection.unblock();
    }

    //...
};

This would be used as:

typedef SlotObject<boost::signal<void(int, float)> > SlotType;
SlotType s = ...;
s.Call(SlotType::ArgsType(1, "foo"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文