2 Copyright (C) 2009-2012 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef __pbd_signals_h__
21 #define __pbd_signals_h__
30 #include <glibmm/threads.h>
32 #include <boost/noncopyable.hpp>
33 #include <boost/bind.hpp>
34 #include <boost/bind/protect.hpp>
35 #include <boost/function.hpp>
36 #include <boost/enable_shared_from_this.hpp>
37 #include <boost/optional.hpp>
39 #include "pbd/libpbd_visibility.h"
40 #include "pbd/event_loop.h"
42 #define DEBUG_PBD_SIGNAL_CONNECTIONS
44 #ifdef DEBUG_PBD_SIGNAL_CONNECTIONS
45 #include "pbd/stacktrace.h"
51 class LIBPBD_API Connection;
53 class LIBPBD_API SignalBase
56 SignalBase () : _debug_connection (false) {}
57 virtual ~SignalBase () {}
58 virtual void disconnect (boost::shared_ptr<Connection>) = 0;
59 #ifdef DEBUG_PBD_SIGNAL_CONNECTIONS
60 void set_debug_connection (bool yn) { _debug_connection = yn; }
64 Glib::Threads::Mutex _mutex;
65 #ifdef DEBUG_PBD_SIGNAL_CONNECTIONS
66 bool _debug_connection;
70 class LIBPBD_API Connection : public boost::enable_shared_from_this<Connection>
73 Connection (SignalBase* b) : _signal (b) {}
77 Glib::Threads::Mutex::Lock lm (_mutex);
79 _signal->disconnect (shared_from_this ());
84 void signal_going_away ()
86 Glib::Threads::Mutex::Lock lm (_mutex);
91 Glib::Threads::Mutex _mutex;
96 class /*LIBPBD_API*/ OptionalLastValue
99 typedef boost::optional<R> result_type;
101 template <typename Iter>
102 result_type operator() (Iter first, Iter last) const {
104 while (first != last) {
113 typedef boost::shared_ptr<Connection> UnscopedConnection;
115 class LIBPBD_API ScopedConnection
118 ScopedConnection () {}
119 ScopedConnection (UnscopedConnection c) : _c (c) {}
120 ~ScopedConnection () {
131 ScopedConnection& operator= (UnscopedConnection const & o)
143 UnscopedConnection _c;
146 class LIBPBD_API ScopedConnectionList : public boost::noncopyable
149 ScopedConnectionList();
150 virtual ~ScopedConnectionList ();
152 void add_connection (const UnscopedConnection& c);
153 void drop_connections ();
156 /* this class is not copyable */
157 ScopedConnectionList(const ScopedConnectionList&);
159 /* Even though our signals code is thread-safe, this additional list of
160 scoped connections needs to be protected in 2 cases:
162 (1) (unlikely) we make a connection involving a callback on the
163 same object from 2 threads. (wouldn't that just be appalling
166 (2) where we are dropping connections in one thread and adding
170 Glib::Threads::Mutex _lock;
172 typedef std::list<ScopedConnection*> ConnectionList;
173 ConnectionList _list;
176 #include "pbd/signals_generated.h"
180 #endif /* __pbd_signals_h__ */