Boost 跟踪增压信号的连接计数

Boost 跟踪增压信号的连接计数,boost,signals-slots,Boost,Signals Slots,我试图存档的是在boost::signal2::signal对象的连接数发生变化时获取更新 让我们了解整个情况:我正在编写一个GUI应用程序,它显示来自远程服务器的数据。应用程序中的每个窗口都应该获取特定数据集的数据。如果要显示数据集,则需要从服务器远程订阅该数据集。多个窗口可以显示具有不同排序或筛选的相同数据集。我的目标是只订阅一次特定的数据集,并断开不再需要的数据集 背景:HFT软件,显示市场数据订单,交易 到目前为止,我的代码是:一旦我试图实现这个操作符,我就被卡住了 enum Updat

我试图存档的是在boost::signal2::signal对象的连接数发生变化时获取更新

让我们了解整个情况:我正在编写一个GUI应用程序,它显示来自远程服务器的数据。应用程序中的每个窗口都应该获取特定数据集的数据。如果要显示数据集,则需要从服务器远程订阅该数据集。多个窗口可以显示具有不同排序或筛选的相同数据集。我的目标是只订阅一次特定的数据集,并断开不再需要的数据集

背景:HFT软件,显示市场数据订单,交易

到目前为止,我的代码是:一旦我试图实现这个操作符,我就被卡住了

enum UpdateCountMethod {
  UP = 1,
  DOWN = -1
};


/**
 * \brief Connection class which holds a Slot as long as an instance of this class "survives".
 */
class Connection {
public:
  Connection(const boost::function<void (int)> updateFunc, const boost::signals2::connection conn) : update(updateFunc), connection(conn) {
    update(UP); //Increase counter only. Connection was already made.
  }

  ~Connection() {
    update(DOWN); //Decrease counter before disconnecting the slot.
    connection.disconnect();
  }

private:
  const boost::function<void(int)> update; // Functor for updating the connection count.
  const boost::signals2::connection connection; // Actual boost connection this object belongs to.
};


/**
 * \brief This is a Signal/Slot "container" which number of connections can be tracked.
 */
template<typename Signature>
class ObservableSignal{
  typedef typename boost::signals2::slot<Signature> slot_type;

public:
  ObservableSignal() : count(0) {}

  boost::shared_ptr<Connection> connect(const slot_type &t) {
    // Create the boost signal connection and return our shared Connection object.
    boost::signals2::connection conn = signal.connect(t);
    return boost::shared_ptr<Connection>(new Connection(boost::bind(&ObservableSignal::updateCount, this, _1), conn));
  }


  // This is where I don't know anymore.
  void operator() (/* Parameter depend on "Signature" */) {
    signal(/* Parameter depend on "Signature" */); //Call the actual boost signal
  }

private:

  void updateCount(int updown) {
    // TODO: Handle subscription if count is leaving or approaching 0.
    count += updown;
    std::cout << "Count: " << count << std::endl;
  }

  int count; // current count of connections to this signal
  boost::signals2::signal<Signature> signal; // Actual boost signal
};

您的编译器支持可变模板吗?如果是,则将void运算符设为可变模板,并将参数转发给信号调用。@Igor不幸。。。不:我们仍然停留在旧时代。好吧,你必须提供一堆重载的操作符模板——你可以使用Boost。预处理器,看看Signals2本身是如何做到的。