Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 395481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:27:30+00:00 2026-05-12T16:27:30+00:00

There is a signal and several objects with slots. I want to implement the

  • 0

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:27:30+00:00Added an answer on May 12, 2026 at 4:27 pm

    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"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.