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__
24 #include <glibmm/thread.h>
26 #include <boost/noncopyable.hpp>
27 #include <boost/bind.hpp>
28 #include <boost/bind/protect.hpp>
29 #include <boost/function.hpp>
30 #include <boost/thread/mutex.hpp>
31 #include <boost/enable_shared_from_this.hpp>
32 #include <boost/optional.hpp>
34 #include "pbd/event_loop.h"
43 virtual ~SignalBase () {}
44 virtual void disconnect (boost::shared_ptr<Connection>) = 0;
50 class Connection : public boost::enable_shared_from_this<Connection>
53 Connection (SignalBase* b) : _signal (b) {}
57 boost::mutex::scoped_lock lm (_mutex);
59 _signal->disconnect (shared_from_this ());
64 void signal_going_away ()
66 boost::mutex::scoped_lock lm (_mutex);
76 class OptionalLastValue
79 typedef boost::optional<R> result_type;
81 template <typename Iter>
82 result_type operator() (Iter first, Iter last) const {
84 while (first != last) {
93 typedef boost::shared_ptr<Connection> UnscopedConnection;
95 class ScopedConnection
98 ScopedConnection () {}
99 ScopedConnection (UnscopedConnection c) : _c (c) {}
100 ~ScopedConnection () {
111 ScopedConnection& operator= (UnscopedConnection const & o)
123 UnscopedConnection _c;
126 class ScopedConnectionList : public boost::noncopyable
129 ScopedConnectionList();
130 virtual ~ScopedConnectionList ();
132 void add_connection (const UnscopedConnection& c);
133 void drop_connections ();
136 /* this class is not copyable */
137 ScopedConnectionList(const ScopedConnectionList&);
139 /* Even though our signals code is thread-safe, this additional list of
140 scoped connections needs to be protected in 2 cases:
142 (1) (unlikely) we make a connection involving a callback on the
143 same object from 2 threads. (wouldn't that just be appalling
146 (2) where we are dropping connections in one thread and adding
152 typedef std::list<ScopedConnection*> ConnectionList;
153 ConnectionList _list;
156 #include "pbd/signals_generated.h"
160 #endif /* __pbd_signals_h__ */