0

I'm trying to make a connection between a function in one class so that I can call it in another class. The answers I've found are either too specific or maybe I'm missing something because I cannot for the life of me figure out what I'm doing wrong. Also, I'm a bit new to the boost library so please excuse any dumb questions I may ask.

The setup for my workflow is basically this...

class MyClass : public BaseClass {

  void setup();
  void myFunc(datatype);

  OtherClass myOtherClass;

}

void setup()
{
   OtherNamespace::addListener(this, myOtherClass);
}

namespace OtherNamespace {

class OtherClass {

  signals::signal<void (datatype)> myConnection;

}

template<class A, class B>
void addListener(A * app, B & connection)
{
   connection.myConnection.connect( 'I don't know what to do here' );
}
}

Basically, the addlistener function won't make the connection between the signal and the function. I know that where I don't know what to do I'm doing something wrong but I just can't figure out what about it is wrong. I'm making this as a helper function so that I can pass functions from one class to another then call them when they're attached. I'm trying to create a domino event from a websocket connection and I am clearly missing something very important. Any help would be truly appreciated.

I'm on xcode and boost and macbook pro.

Igor R.
  • 14,716
  • 2
  • 49
  • 83
Ryan Bartley
  • 606
  • 8
  • 17

1 Answers1

1

You should connect to the signal a matching slot, which is a callable having the appropriate signature. The best solution is to leave the slot creation to the caller of your addListener function (the following is your code, in SSCCE form):

#include <boost/signals2.hpp>

typedef int datatype;
class BaseClass
{};

namespace OtherNamespace 
{
  class OtherClass 
  {
  public:
    boost::signals2::signal<void (datatype)> myConnection;
  };

  template<class A, class B>
  void addListener(A app, B & connection)
  {
     connection.myConnection.connect(app);
  }
}

class MyClass : public BaseClass 
{
public:

  void setup();
  void myFunc(datatype)
  {}

  OtherNamespace::OtherClass myOtherClass;

};

void MyClass::setup()
{
   // let the caller decide how to make the slot
   OtherNamespace::addListener(boost::bind(&MyClass::myFunc, this, _1), myOtherClass);
}
Igor R.
  • 14,716
  • 2
  • 49
  • 83