2 Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
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 DCPOMATIC_SIGNALLER_H
21 #define DCPOMATIC_SIGNALLER_H
23 #include "signal_manager.h"
24 #include <boost/thread/mutex.hpp>
34 virtual ~WrapperBase () {}
36 /* Can be called from any thread */
39 boost::mutex::scoped_lock lm (_mutex);
43 bool finished () const {
44 boost::mutex::scoped_lock lm (_mutex);
49 /* Protect _valid and _finished */
50 mutable boost::mutex _mutex;
55 /** Helper class to manage lifetime of signals, specifically to address
56 * the problem where an object containing a signal is deleted before
57 * its signal is emitted.
60 class Wrapper : public WrapperBase
69 /* Called by the UI thread only */
72 boost::mutex::scoped_lock lm (_mutex);
83 /** Parent for any class which needs to raise cross-thread signals (from non-UI
84 * to UI). Subclasses should call, e.g. emit (boost::bind (boost::ref (MySignal), foo, bar));
89 /* Can be called from any thread */
90 virtual ~Signaller () {
91 boost::mutex::scoped_lock lm (_signaller_mutex);
92 for (std::list<WrapperBase*>::iterator i = _wrappers.begin(); i != _wrappers.end(); ++i) {
97 /* Can be called from any thread */
101 Wrapper<T>* w = new Wrapper<T> (signal);
102 if (signal_manager) {
103 signal_manager->emit (boost::bind (&Wrapper<T>::signal, w));
106 boost::mutex::scoped_lock lm (_signaller_mutex);
108 /* Clean up finished Wrappers */
109 std::list<WrapperBase*>::iterator i = _wrappers.begin ();
110 while (i != _wrappers.end ()) {
111 std::list<WrapperBase*>::iterator tmp = i;
113 if ((*i)->finished ()) {
120 /* Add the new one */
121 _wrappers.push_back (w);
125 /* Protect _wrappers */
126 boost::mutex _signaller_mutex;
127 std::list<WrapperBase*> _wrappers;