FINAL CUT
monitor.h
1 /***********************************************************************
2 * monitor.h - Monitoring object base class *
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 /* Base class
24  * ══════════
25  *
26  * ▕▔▔▔▔▔▔▔▔▔▏
27  * ▕ Monitor ▏
28  * ▕▁▁▁▁▁▁▁▁▁▏
29  */
30 
31 #ifndef MONITOR_H
32 #define MONITOR_H
33 
34 #include <functional>
35 #include <stdexcept>
36 #include <string>
37 
38 #include "final/util/fdata.h"
39 #include "final/util/fstring.h"
40 
41 namespace finalcut
42 {
43 
44 // class forward declaration
45 class EventLoop;
46 class Monitor;
47 
48 //----------------------------------------------------------------------
49 // class monitor_error
50 //----------------------------------------------------------------------
51 
52 class monitor_error : public std::runtime_error
53 {
54  public:
55  using std::runtime_error::runtime_error;
56 
57  // Copy constructor
58  monitor_error (const monitor_error&) = default;
59 
60  // Move constructor
61  monitor_error (monitor_error&&) noexcept = default;
62 
63  // Copy assignment operator (=)
64  auto operator = (const monitor_error&) -> monitor_error& = default;
65 
66  // Move assignment operator (=)
67  auto operator = (monitor_error&&) noexcept -> monitor_error& = default;
68 
69  // Destructor
70  ~monitor_error() override;
71 };
72 
73 // Using-declaration
74 using handler_t = std::function<void(Monitor*, short)>;
75 
76 
77 //----------------------------------------------------------------------
78 // class Monitor
79 //----------------------------------------------------------------------
80 
81 class Monitor
82 {
83  public:
84  // Constructor
85  explicit Monitor (EventLoop*);
86  Monitor() = delete;
87 
88  // Disable copy constructor
89  Monitor (const Monitor&) = delete;
90 
91  // Disable move constructor
92  Monitor (Monitor&&) noexcept = delete;
93 
94  // Destructor
95  virtual ~Monitor();
96 
97  // Disable copy assignment operator (=)
98  auto operator = (const Monitor&) -> Monitor& = delete;
99 
100  // Disable move assignment operator (=)
101  auto operator = (Monitor&&) noexcept -> Monitor& = delete;
102 
103  // Accessors
104  virtual auto getClassName() const -> FString;
105  auto getEvents() const -> short;
106  auto getFileDescriptor() const -> int;
107  template <typename T>
108  auto getUserContext() const -> clean_fdata_t<T>&;
109 
110  // Inquiry
111  auto isActive() const -> bool;
112 
113  // Methods
114  virtual void resume();
115  virtual void suspend();
116 
117  protected:
118  // Constants
119  static constexpr int NO_FILE_DESCRIPTOR{-1};
120 
121  // Mutators
122  void setFileDescriptor (int);
123  void setEvents (short);
124  void setHandler (handler_t&&);
125  template <typename T>
126  void setUserContext (T&&);
127  void setInitialized();
128 
129  // Inquiry
130  auto isInitialized() const -> bool;
131 
132  // Methods
133  virtual void trigger (short);
134 
135  private:
136  // Using-declaration
137  using FDataAccessPtr = std::shared_ptr<FDataAccess>;
138 
139  // Data member
140  bool active{false};
141  EventLoop* eventloop{};
142  int fd{NO_FILE_DESCRIPTOR};
143  short events{0};
144  handler_t handler{};
145  FDataAccessPtr user_context{nullptr};
146  bool monitor_initialized{false};
147 
148  // Friend classes
149  friend class EventLoop;
150 };
151 
152 // Monitor inline functions
153 //----------------------------------------------------------------------
154 inline auto Monitor::getClassName() const -> FString
155 { return "Monitor"; }
156 
157 //----------------------------------------------------------------------
158 inline auto Monitor::getEvents() const -> short
159 { return events; }
160 
161 //----------------------------------------------------------------------
162 inline auto Monitor::getFileDescriptor() const -> int
163 { return fd; }
164 
165 //----------------------------------------------------------------------
166 template <typename T>
167 auto Monitor::getUserContext() const -> clean_fdata_t<T>&
168 {
169  static T empty_lvalue = T{};
170  return user_context
171  ? static_cast<FData<clean_fdata_t<T>>&>(*user_context).get()
172  : empty_lvalue;
173 }
174 
175 //----------------------------------------------------------------------
176 inline auto Monitor::isActive() const -> bool
177 { return active; }
178 
179 //----------------------------------------------------------------------
180 inline void Monitor::resume()
181 { active = true; }
182 
183 //----------------------------------------------------------------------
184 inline void Monitor::suspend()
185 { active = false; }
186 
187 //----------------------------------------------------------------------
188 inline void Monitor::trigger (short return_events)
189 {
190  if ( handler )
191  handler (this, return_events);
192 }
193 
194 //----------------------------------------------------------------------
195 inline void Monitor::setFileDescriptor (int file_descriptor)
196 { fd = file_descriptor; }
197 
198 //----------------------------------------------------------------------
199 inline void Monitor::setEvents (short ev)
200 { events = ev; }
201 
202 //----------------------------------------------------------------------
203 inline void Monitor::setHandler (handler_t&& hdl)
204 { handler = std::move(hdl); }
205 
206 //----------------------------------------------------------------------
207 template <typename T>
208 inline void Monitor::setUserContext (T&& uc)
209 {
210  user_context.reset(makeFData(std::forward<T>(uc)));
211 }
212 
213 //----------------------------------------------------------------------
214 inline void Monitor::setInitialized()
215 { monitor_initialized = true; }
216 
217 //----------------------------------------------------------------------
218 inline auto Monitor::isInitialized() const -> bool
219 { return monitor_initialized; }
220 
221 } // namespace finalcut
222 
223 #endif // MONITOR_H
Definition: monitor.h:81
Definition: eventloop.h:48
Definition: class_template.cpp:25
Definition: monitor.h:52
Definition: fstring.h:79
Definition: fdata.h:51