Boost logo

Boost :

From: Johan Torp (johan.torp_at_[hidden])
Date: 2007-05-20 15:06:51


I developed some classes which had similar functionality to slots and
signals and now I want to transition to using slots and signals.

I often find myself in the situation of wanting to listen to changes
of a value type variable. I had a templated class called
ConcreteObservable which encapsulated a get/set-able value and
provided ways of listening to changes in the value. I.e.
ConcreteObservable<bool> was a bool which you could get, set and
listen to.

I couldn't find anything corresponding in slots and signals so I wrote
a small class - see end of post - which provides this functionality. I
have a few questions regarding this:

1. Does the idea of a listenable value seem sound to you or do you see
any problems?
2. Is there any existing boost code that already provides this functionality?
3. Do you see any problems with my implementation?
4. Do you have a better naming suggestion?
    Is signalling (user code would read signalling<bool>), listenable
or signal_value perhaps better?

template <class T>
class signalling_value
{
public:
  signalling_value() : mValue(boost::value_initialized<T>()) {}
  signalling_value(const T& t) : mValue(t) {}

  void set(const T& value)
  {
    if (value != mValue)
    {
      mValue=value;
      mSignal(mValue);
    }
  }

  const T& get() const { return mValue; }

  template<class Slot>
  boost::signals::connection connect(const Slot& slot) const
  {
    return mSignal.connect(slot);
  }

  template<class Slot>
  boost::signals::connection connect(int order, const Slot& slot) const
  {
    return mSignal.connect(order, slot);
  }

  template<class Slot>
  void disconnect(const Slot& slot) const
  {
    mSignal.disconnect(slot);
  }

private:
  T mValue;
  mutable boost::signal<void (const T&)> mSignal;
};


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk