Search code examples
c++boostboost-signals

How to convert an existing callback interface to use boost signals & slots


I've currently got a class that can notify a number of other objects via callbacks:

class Callback {
   virtual NodulesChanged() =0;
   virtual TurkiesTwisted() =0;
};

class Notifier
{
  std::vector<Callback*> m_Callbacks;

  void AddCallback(Callback* cb) {m_Callbacks.push(cb); }
  ...
  void ChangeNodules() {
     for (iterator it=m_Callbacks.begin(); it!=m_Callbacks.end(); it++) {
        (*it)->NodulesChanged();
     }
  }
};

I'm considering changing this to use boost's signals and slots as it would be beneficial to reduce the likelihood of dangling pointers when the callee gets deleted, among other things. However, as it stands boost's signals seems more oriented towards dealing with function objects. What would be the best way of adapting my code to still use the callback interface but use signals and slots to deal with the connection and notification aspects?


Solution

  • Compared to my other answer, this solution is much more generic and eliminates boilerplate code:

    #include <iostream>
    #include <boost/bind.hpp>
    #include <boost/signal.hpp>
    
    ///////////////////////////////////////////////////////////////////////////////
    // GENERIC REUSABLE PART FOR ALL SUBJECTS
    ///////////////////////////////////////////////////////////////////////////////
    
    //-----------------------------------------------------------------------------
    template <class CallbackType>
    class CallbackInvoker
    {
    public:
        virtual ~CallbackInvoker() {}
        virtual void operator()(CallbackType* callback) const {};
    };
    
    //-----------------------------------------------------------------------------
    template <class CallbackType, class Binding>
    class BoundInvoker : public CallbackInvoker<CallbackType>
    {
    public:
        BoundInvoker(const Binding& binding) : binding_(binding) {}
        void operator()(CallbackType* callback) const {binding_(callback);}
    
    private:
        Binding binding_;
    };
    
    //-----------------------------------------------------------------------------
    template <class CallbackType>
    class CallbackSlot
    {
    public:
        CallbackSlot(CallbackType* callback) : callback_(callback) {}
        void operator()(const CallbackInvoker<CallbackType>& invoker)
            {invoker(callback_);}
    
    private:
        CallbackType* callback_;
    };
    
    //-----------------------------------------------------------------------------
    template <class CallbackType>
    class Subject
    {
    public:
        virtual ~Subject() {}
        boost::signals::connection Connect(CallbackType* callback)
            {return signal_.connect(CallbackSlot<CallbackType>(callback));}
    
    protected:
        template <class Binding> void Signal(const Binding& binding)
        {
            signal_(BoundInvoker<CallbackType,Binding>(binding));
        }
    
    private:
        boost::signal<void (const CallbackInvoker<CallbackType>&)> signal_;
    };
    
    
    ///////////////////////////////////////////////////////////////////////////////
    // THIS PART SPECIFIC TO ONE SUBJECT
    ///////////////////////////////////////////////////////////////////////////////
    //------------------------------------------------------------------------------
    class MyCallback
    {
    public:
        virtual ~MyCallback() {}
        virtual void NodulesChanged() =0;
        virtual void TurkiesTwisted(int arg) =0;
    };
    
    //-----------------------------------------------------------------------------
    class FooCallback : public MyCallback
    {
    public:
        virtual ~FooCallback() {}
        void NodulesChanged() {std::cout << "Foo nodules changed\n";}
        void TurkiesTwisted(int arg)
            {std::cout << "Foo " << arg << " turkies twisted\n";}
    };
    
    //-----------------------------------------------------------------------------
    class BarCallback : public MyCallback
    {
    public:
        virtual ~BarCallback() {}
        void NodulesChanged() {std::cout << "Bar nodules changed\n";}
        void TurkiesTwisted(int arg)
            {std::cout << "Bar " << arg << " turkies twisted\n";}
    };
    
    //-----------------------------------------------------------------------------
    class MySubject : public Subject<MyCallback>
    {
    public:
        void OnNoduleChanged()
            {this->Signal(boost::bind(&MyCallback::NodulesChanged, _1));}
        void OnTurkiedTwisted(int arg)
            {this->Signal(boost::bind(&MyCallback::TurkiesTwisted, _1, arg));}
    };
    
    ///////////////////////////////////////////////////////////////////////////////
    // CLIENT CODE
    ///////////////////////////////////////////////////////////////////////////////
    
    //-----------------------------------------------------------------------------
    int main()
    {
        MySubject subject;
        FooCallback fooCb;
        BarCallback barCb;
    
        subject.Connect(&fooCb);
        subject.Connect(&barCb);
    
        subject.OnNoduleChanged();
        subject.OnTurkiedTwisted(42);
    }
    

    Hooray for boost::bind! :-)