Some noncopyable removal.
[dcpomatic.git] / src / lib / signal_manager.h
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #ifndef DCPOMATIC_SIGNAL_MANAGER_H
23 #define DCPOMATIC_SIGNAL_MANAGER_H
24
25
26 #include "exception_store.h"
27 #include <boost/asio.hpp>
28 #include <boost/thread.hpp>
29 #include <boost/noncopyable.hpp>
30
31
32 class Signaller;
33
34
35 /** A class to allow signals to be emitted from non-UI threads and handled
36  *  by a UI thread.
37  */
38 class SignalManager : public ExceptionStore
39 {
40 public:
41         /** Create a SignalManager.  Must be called from the UI thread */
42         SignalManager ()
43                 : _work (_service)
44         {
45                 _ui_thread = boost::this_thread::get_id ();
46         }
47
48         virtual ~SignalManager () {}
49
50         SignalManager (Signaller const&) = delete;
51         SignalManager& operator= (Signaller const&) = delete;
52
53         /* Do something next time the UI is idle */
54         template <typename T>
55         void when_idle (T f) {
56                 _service.post (f);
57         }
58
59         /** Call this in the UI when it is idle.
60          *  @return Number of idle handlers that were executed.
61          */
62         size_t ui_idle () {
63                 /* This executes one of the functors that has been post()ed to _service */
64                 return _service.poll_one ();
65         }
66
67         /** This should wake the UI and make it call ui_idle() */
68         virtual void wake_ui () {
69                 /* This is only a sensible implementation when there is no GUI */
70                 ui_idle ();
71         }
72
73 private:
74         /** Emit a signal from any thread whose handlers will be called in the UI
75          *  thread.  Use something like:
76          *
77          *  ui_signaller->emit (boost::bind (boost::ref (SomeSignal), parameter));
78          */
79         template <typename T>
80         void emit (T f) {
81                 if (boost::this_thread::get_id() == _ui_thread) {
82                         /* already in the UI thread */
83                         try {
84                                 f ();
85                         } catch (...) {
86                                 store_current ();
87                         }
88                 } else {
89                         /* non-UI thread; post to the service and wake up the UI */
90                         _service.post (f);
91                         wake_ui ();
92                 }
93         }
94
95         friend class Signaller;
96
97         /** A io_service which is used as the conduit for messages */
98         boost::asio::io_service _service;
99         /** Object required to keep io_service from stopping when it has nothing to do */
100         boost::asio::io_service::work _work;
101         /** The UI thread's ID */
102         boost::thread::id _ui_thread;
103 };
104
105
106 extern SignalManager* signal_manager;
107
108
109 #endif