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

Posted by the_mandrill on Stack Overflow See other posts from Stack Overflow or by the_mandrill
Published on 2010-06-08T13:35:32Z Indexed on 2010/06/08 15:12 UTC
Read the original article Hit count: 301

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost