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__
32 #include <glibmm/threads.h>
34 #include <boost/noncopyable.hpp>
35 #include <boost/bind.hpp>
36 #include <boost/bind/protect.hpp>
37 #include <boost/function.hpp>
38 #include <boost/enable_shared_from_this.hpp>
39 #include <boost/optional.hpp>
41 #include "pbd/libpbd_visibility.h"
42 #include "pbd/event_loop.h"
45 #define DEBUG_PBD_SIGNAL_CONNECTIONS
48 #ifdef DEBUG_PBD_SIGNAL_CONNECTIONS
49 #include "pbd/stacktrace.h"
55 class LIBPBD_API Connection;
57 class LIBPBD_API SignalBase
61 #ifdef DEBUG_PBD_SIGNAL_CONNECTIONS
62 : _debug_connection (false)
65 virtual ~SignalBase () {}
66 virtual void disconnect (boost::shared_ptr<Connection>) = 0;
67 #ifdef DEBUG_PBD_SIGNAL_CONNECTIONS
68 void set_debug_connection (bool yn) { _debug_connection = yn; }
72 mutable Glib::Threads::Mutex _mutex;
73 #ifdef DEBUG_PBD_SIGNAL_CONNECTIONS
74 bool _debug_connection;
78 class LIBPBD_API Connection : public boost::enable_shared_from_this<Connection>
81 Connection (SignalBase* b) : _signal (b) {}
85 Glib::Threads::Mutex::Lock lm (_mutex);
87 _signal->disconnect (shared_from_this ());
92 void signal_going_away ()
94 Glib::Threads::Mutex::Lock lm (_mutex);
99 Glib::Threads::Mutex _mutex;
104 class /*LIBPBD_API*/ OptionalLastValue
107 typedef boost::optional<R> result_type;
109 template <typename Iter>
110 result_type operator() (Iter first, Iter last) const {
112 while (first != last) {
121 typedef boost::shared_ptr<Connection> UnscopedConnection;
123 class LIBPBD_API ScopedConnection
126 ScopedConnection () {}
127 ScopedConnection (UnscopedConnection c) : _c (c) {}
128 ~ScopedConnection () {
139 ScopedConnection& operator= (UnscopedConnection const & o)
151 UnscopedConnection _c;
154 class LIBPBD_API ScopedConnectionList : public boost::noncopyable
157 ScopedConnectionList();
158 virtual ~ScopedConnectionList ();
160 void add_connection (const UnscopedConnection& c);
161 void drop_connections ();
164 /* this class is not copyable */
165 ScopedConnectionList(const ScopedConnectionList&);
167 /* Even though our signals code is thread-safe, this additional list of
168 scoped connections needs to be protected in 2 cases:
170 (1) (unlikely) we make a connection involving a callback on the
171 same object from 2 threads. (wouldn't that just be appalling
174 (2) where we are dropping connections in one thread and adding
178 Glib::Threads::Mutex _lock;
180 typedef std::list<ScopedConnection*> ConnectionList;
181 ConnectionList _list;
184 #include "pbd/signals_generated.h"
188 #endif /* __pbd_signals_h__ */