/*
- Copyright (C) 2009 Paul Davis
+ Copyright (C) 2009-2012 Paul Davis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#ifndef __pbd_signals_h__
#define __pbd_signals_h__
+#include <csignal>
+
#include <list>
-#include <glibmm/thread.h>
-#include <boost/signals2.hpp>
+#include <map>
+
+#ifdef nil
+#undef nil
+#endif
+
+#include <glibmm/threads.h>
+
#include <boost/noncopyable.hpp>
+#include <boost/bind.hpp>
+#include <boost/bind/protect.hpp>
+#include <boost/function.hpp>
+#include <boost/enable_shared_from_this.hpp>
+#include <boost/optional.hpp>
+
+#include "pbd/libpbd_visibility.h"
+#include "pbd/event_loop.h"
+
+#ifndef NDEBUG
+#define DEBUG_PBD_SIGNAL_CONNECTIONS
+#endif
+
+#ifdef DEBUG_PBD_SIGNAL_CONNECTIONS
+#include "pbd/stacktrace.h"
+#include <iostream>
+#endif
namespace PBD {
-typedef boost::signals2::connection UnscopedConnection;
-typedef boost::signals2::connection Connection;
-typedef boost::signals2::scoped_connection ScopedConnection;
+class LIBPBD_API Connection;
+
+class LIBPBD_API SignalBase
+{
+public:
+ SignalBase ()
+#ifdef DEBUG_PBD_SIGNAL_CONNECTIONS
+ : _debug_connection (false)
+#endif
+ {}
+ virtual ~SignalBase () {}
+ virtual void disconnect (boost::shared_ptr<Connection>) = 0;
+#ifdef DEBUG_PBD_SIGNAL_CONNECTIONS
+ void set_debug_connection (bool yn) { _debug_connection = yn; }
+#endif
+
+protected:
+ mutable Glib::Threads::Mutex _mutex;
+#ifdef DEBUG_PBD_SIGNAL_CONNECTIONS
+ bool _debug_connection;
+#endif
+};
+
+class LIBPBD_API Connection : public boost::enable_shared_from_this<Connection>
+{
+public:
+ Connection (SignalBase* b, PBD::EventLoop::InvalidationRecord* ir) : _signal (b), _invalidation_record (ir)
+ {
+ if (_invalidation_record) {
+ _invalidation_record->ref ();
+ }
+ }
+
+ void disconnect ()
+ {
+ Glib::Threads::Mutex::Lock lm (_mutex);
+ if (_signal) {
+ _signal->disconnect (shared_from_this ());
+ _signal = 0;
+ }
+ }
+
+ void disconnected ()
+ {
+ if (_invalidation_record) {
+ _invalidation_record->unref ();
+ }
+ }
+
+ void signal_going_away ()
+ {
+ Glib::Threads::Mutex::Lock lm (_mutex);
+ if (_invalidation_record) {
+ _invalidation_record->unref ();
+ }
+ _signal = 0;
+ }
+
+private:
+ Glib::Threads::Mutex _mutex;
+ SignalBase* _signal;
+ PBD::EventLoop::InvalidationRecord* _invalidation_record;
+};
+
+template<typename R>
+class /*LIBPBD_API*/ OptionalLastValue
+{
+public:
+ typedef boost::optional<R> result_type;
+
+ template <typename Iter>
+ result_type operator() (Iter first, Iter last) const {
+ result_type r;
+ while (first != last) {
+ r = *first;
+ ++first;
+ }
+
+ return r;
+ }
+};
+
+typedef boost::shared_ptr<Connection> UnscopedConnection;
-class ScopedConnectionList : public boost::noncopyable
+class LIBPBD_API ScopedConnection
+{
+public:
+ ScopedConnection () {}
+ ScopedConnection (UnscopedConnection c) : _c (c) {}
+ ~ScopedConnection () {
+ disconnect ();
+ }
+
+ void disconnect ()
+ {
+ if (_c) {
+ _c->disconnect ();
+ }
+ }
+
+ ScopedConnection& operator= (UnscopedConnection const & o)
+ {
+ if (_c == o) {
+ return *this;
+ }
+
+ disconnect ();
+ _c = o;
+ return *this;
+ }
+
+private:
+ UnscopedConnection _c;
+};
+
+class LIBPBD_API ScopedConnectionList : public boost::noncopyable
{
public:
ScopedConnectionList();
- ~ScopedConnectionList ();
-
+ virtual ~ScopedConnectionList ();
+
void add_connection (const UnscopedConnection& c);
void drop_connections ();
- template<typename S> void scoped_connect (S& sig, const typename S::slot_function_type& sf) {
- add_connection (sig.connect (sf));
- }
-
private:
/* this class is not copyable */
- ScopedConnectionList(const ScopedConnectionList&) {}
-
- /* this lock is shared by all instances of a ScopedConnectionList.
- We do not want one mutex per list, and since we only need the lock
- when adding or dropping connections, which are generally occuring
- in object creation and UI operations, the contention on this
- lock is low and not of significant consequence. Even though
- boost::signals2 is thread-safe, this additional list of
+ ScopedConnectionList(const ScopedConnectionList&);
+
+ /* Even though our signals code is thread-safe, this additional list of
scoped connections needs to be protected in 2 cases:
(1) (unlikely) we make a connection involving a callback on the
- same object from 2 threads. (wouldn't that just be appalling
+ same object from 2 threads. (wouldn't that just be appalling
programming style?)
-
+
(2) where we are dropping connections in one thread and adding
one from another.
*/
- static Glib::StaticMutex _lock;
+ Glib::Threads::Mutex _lock;
typedef std::list<ScopedConnection*> ConnectionList;
ConnectionList _list;
};
-template<typename R>
-class Signal0 {
-public:
- Signal0 () {}
- typedef boost::signals2::signal<R()> SignalType;
-
- void connect (ScopedConnectionList& clist,
- const typename SignalType::slot_function_type& slot) {
- clist.add_connection (_signal.connect (slot));
- }
-
- void connect (Connection& c,
- const typename SignalType::slot_function_type& slot) {
- c = _signal.connect (slot);
- }
-
- typename SignalType::result_type operator()() {
- return _signal ();
- }
-
-private:
- SignalType _signal;
-};
-
-template<typename R, typename A>
-class Signal1 {
-public:
- Signal1 () {}
- typedef boost::signals2::signal<R(A)> SignalType;
-
- void connect (ScopedConnectionList& clist,
- const typename SignalType::slot_function_type& slot) {
- clist.add_connection (_signal.connect (slot));
- }
-
- void connect (Connection& c,
- const typename SignalType::slot_function_type& slot) {
- c = _signal.connect (slot);
- }
-
- typename SignalType::result_type operator()(A arg1) {
- return _signal (arg1);
- }
-
-private:
- SignalType _signal;
-};
-
-template<typename R, typename A1, typename A2>
-class Signal2 {
-public:
- Signal2 () {}
- typedef boost::signals2::signal<R(A1, A2)> SignalType;
-
- void connect (ScopedConnectionList& clist,
- const typename SignalType::slot_function_type& slot) {
- clist.add_connection (_signal.connect (slot));
- }
-
- void connect (Connection& c,
- const typename SignalType::slot_function_type& slot) {
- c = _signal.connect (slot);
- }
-
- typename SignalType::result_type operator()(A1 arg1, A2 arg2) {
- return _signal (arg1, arg2);
- }
-
-private:
- SignalType _signal;
-};
-
-template<typename R, typename A1, typename A2, typename A3>
-class Signal3 {
-public:
- Signal3 () {}
- typedef boost::signals2::signal<R(A1,A2,A3)> SignalType;
-
- void connect (ScopedConnectionList& clist,
- const typename SignalType::slot_function_type& slot) {
- clist.add_connection (_signal.connect (slot));
- }
-
- void connect (Connection& c,
- const typename SignalType::slot_function_type& slot) {
- c = _signal.connect (slot);
- }
-
- typename SignalType::result_type operator()(A1 arg1, A2 arg2, A3 arg3) {
- return _signal (arg1, arg2, arg3);
- }
-
-private:
- SignalType _signal;
-};
+#include "pbd/signals_generated.h"
} /* namespace */