FINAL CUT
signal_monitor.h
1 /***********************************************************************
2 * signal_monitor.h - Signal monitoring object *
3 * *
4 * This file is part of the FINAL CUT widget toolkit *
5 * *
6 * Copyright 2023 Andreas Noe *
7 * *
8 * FINAL CUT is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as *
10 * published by the Free Software Foundation; either version 3 of *
11 * the License, or (at your option) any later version. *
12 * *
13 * FINAL CUT is distributed in the hope that it will be useful, but *
14 * WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this program. If not, see *
20 * <http://www.gnu.org/licenses/>. *
21 ***********************************************************************/
22 
23 /* Inheritance diagram
24  * ═══════════════════
25  *
26  * ▕▔▔▔▔▔▔▔▔▔▏
27  * ▕ Monitor ▏
28  * ▕▁▁▁▁▁▁▁▁▁▏
29  * ▲
30  * │
31  * ▕▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▏1 1▕▔▔▔▔▔▔▔▔▔▔▏
32  * ▕ SignalMonitor ▏- - - - -▕ PipeData ▏
33  * ▕▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▏ ▕▁▁▁▁▁▁▁▁▁▁▏
34  */
35 
36 #ifndef SIGNAL_MONITOR_H
37 #define SIGNAL_MONITOR_H
38 
39 #include <map>
40 #include <memory>
41 
42 #include "final/eventloop/monitor.h"
43 #include "final/eventloop/pipedata.h"
44 #include "final/util/fstring.h"
45 
46 namespace finalcut
47 {
48 
49 //----------------------------------------------------------------------
50 // class SignalMonitor
51 //----------------------------------------------------------------------
52 
53 class SignalMonitor final : public Monitor
54 {
55  public:
56  explicit SignalMonitor(EventLoop*);
57 
58  // Disable default constructor
59  SignalMonitor() = delete;
60 
61  // Disable copy constructor
62  SignalMonitor(const SignalMonitor&) = delete;
63 
64  // Disable move constructor
65  SignalMonitor(const SignalMonitor&&) = delete;
66 
67  // Destructor
68  ~SignalMonitor() noexcept override;
69 
70  // Disable copy assignment operator (=)
71  auto operator = (const SignalMonitor&) -> SignalMonitor& = delete;
72 
73  // Disable move assignment operator (=)
74  auto operator = (SignalMonitor&&) noexcept -> SignalMonitor& = delete;
75 
76  // Accessor
77  auto getClassName() const -> FString override;
78 
79  // Methods
80  template <typename T>
81  void init (int, handler_t, T&&);
82  void trigger (short) override;
83 
84  private:
85  // class forward declaration
86  class SigactionImpl;
87 
88  // Methods
89  static void onSignal (int);
90  void init();
91  void handledAlarmSignal() const;
92  void ensureSignalIsUnmonitored() const;
93  void createPipe();
94  void installSignalHandler();
95  void enterMonitorInstanceInTable();
96  auto getSigactionImpl() const -> const SigactionImpl*;
97  auto getSigactionImpl() -> SigactionImpl*;
98 
99  // Data members
100  int signal_number{-1};
101  PipeData signal_pipe{NO_FILE_DESCRIPTOR, NO_FILE_DESCRIPTOR};
102  std::unique_ptr<SigactionImpl> impl;
103 };
104 
105 // SignalMonitor inline functions
106 //----------------------------------------------------------------------
107 inline auto SignalMonitor::getClassName() const -> FString
108 { return "SignalMonitor"; }
109 
110 //----------------------------------------------------------------------
111 template <typename T>
112 inline void SignalMonitor::init (int sn, handler_t hdl, T&& uc)
113 {
114  if ( isInitialized() )
115  throw monitor_error{"This instance has already been initialised."};
116 
117  signal_number = sn;
118  setHandler (std::move(hdl));
119  setUserContext (std::forward<T>(uc));
120  init();
121 }
122 
123 } // namespace finalcut
124 
125 #endif // SIGNAL_MONITOR_H
Definition: signal_monitor.h:53
Definition: monitor.h:81
Definition: eventloop.h:48
Definition: class_template.cpp:25
Definition: monitor.h:52
Definition: signal_monitor.cpp:66
Definition: fstring.h:79
Definition: pipedata.h:47