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"
44 class LIBPBD_API Connection;
46 class LIBPBD_API SignalBase
49 virtual ~SignalBase () {}
50 virtual void disconnect (boost::shared_ptr<Connection>) = 0;
53 Glib::Threads::Mutex _mutex;
56 class LIBPBD_API Connection : public boost::enable_shared_from_this<Connection>
59 Connection (SignalBase* b) : _signal (b) {}
63 Glib::Threads::Mutex::Lock lm (_mutex);
65 _signal->disconnect (shared_from_this ());
70 void signal_going_away ()
72 Glib::Threads::Mutex::Lock lm (_mutex);
77 Glib::Threads::Mutex _mutex;
82 class /*LIBPBD_API*/ OptionalLastValue
85 typedef boost::optional<R> result_type;
87 template <typename Iter>
88 result_type operator() (Iter first, Iter last) const {
90 while (first != last) {
99 typedef boost::shared_ptr<Connection> UnscopedConnection;
101 class LIBPBD_API ScopedConnection
104 ScopedConnection () {}
105 ScopedConnection (UnscopedConnection c) : _c (c) {}
106 ~ScopedConnection () {
117 ScopedConnection& operator= (UnscopedConnection const & o)
129 UnscopedConnection _c;
132 class LIBPBD_API ScopedConnectionList : public boost::noncopyable
135 ScopedConnectionList();
136 virtual ~ScopedConnectionList ();
138 void add_connection (const UnscopedConnection& c);
139 void drop_connections ();
142 /* this class is not copyable */
143 ScopedConnectionList(const ScopedConnectionList&);
145 /* Even though our signals code is thread-safe, this additional list of
146 scoped connections needs to be protected in 2 cases:
148 (1) (unlikely) we make a connection involving a callback on the
149 same object from 2 threads. (wouldn't that just be appalling
152 (2) where we are dropping connections in one thread and adding
156 Glib::Threads::Mutex _lock;
158 typedef std::list<ScopedConnection*> ConnectionList;
159 ConnectionList _list;
162 #include "pbd/signals_generated.h"
166 #endif /* __pbd_signals_h__ */