不包括升压信号呼叫

时间:2009-10-09 05:58:41

标签: c++ templates boost boost-signals

有信号和几个带插槽的物体。我想在一个对象调用signal并阻塞自己的连接时实现该行为。我猜一个小片段会提供更多信息:


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方法?

1 个答案:

答案 0 :(得分:4)

对于你的第一个问题:我不知道实现你想要的“标准提升方式”。您可以将问题发布到boost users mailing list

对于你的第二个问题:没有varidic模板和rvalue引用,转发总是很麻烦。

一些建议,没有特别的顺序:

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_connection。

可悲的是,这并没有给调用者一个很好的语法。它看起来像是:

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

3)另一种选择是要求用户在调用站点将元组打包在元组中(我在下面使用boost::fusion::vector),然后使用{{3}打开它们并调用信号。

#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"));