Boost logo

Boost :

From: Johan Torp (johan.torp_at_[hidden])
Date: 2007-06-12 15:30:41


I have an idea of a small class which bundles a value and a way of
listening to changes in it. I find this concept very useful when
developing event-driven applications and wonder if there is any
interest in a small library.

My idea of how this could be implemented builds on top of boost::slots
and signals. The interface could look something like this:

template <typename T> class observable_value
{
public:
  typedef typename boost::signal<void (const T&)>::slot_type slot_type;

  observable_value();
  explicit observable_value(const T& t);

  // set value and call observers if there was a change
  observable_value<T>& operator=(const T& value);

  operator const T& () const;

  boost::signals::connection connect(const slot_type& slot) const;
  void disconnect(const slot_type& slot) const;

  // call observers only if the value was changed
  observable_value<T>& operator+=(const T& t);
  ... other operators
};

User code might look like this:

class SomeClass {
  const observable_value<bool>& is_something() const;
  const observable_value<int>& get_count() const;
};

ADrawableClass create_ok_cancel_dialog(boost::function<void> onOk,

boost::function<void> onCancel,
                                                             const
boost::observable_value<bool>& okEnabled);

A reference implementation and a small visual studio example can be
found at http://svn.boost.org/trac/boost/browser/sandbox/observable_value

Give me feedback :) Do you think this is useful enough to become a
small library or be added to boost signals and slots?

Best Regards, Johan Torp


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