FINAL CUT
io_monitor.h
1 /***********************************************************************
2 * io_monitor.h - I/O monitoring object *
3 * *
4 * This file is part of the FINAL CUT widget toolkit *
5 * *
6 * Copyright 2023-2025 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  * ▕▔▔▔▔▔▔▔▔▔▔▔▏
32  * ▕ IoMonitor ▏
33  * ▕▁▁▁▁▁▁▁▁▁▁▁▏
34  */
35 
36 #ifndef IO_MONITOR_H
37 #define IO_MONITOR_H
38 
39 #include "final/eventloop/monitor.h"
40 #include "final/util/fstring.h"
41 
42 namespace finalcut
43 {
44 
45 //----------------------------------------------------------------------
46 // class IoMonitor
47 //----------------------------------------------------------------------
48 
49 class IoMonitor final : public Monitor
50 {
51  public:
52  // Disable default constructor
53  IoMonitor() = delete;
54 
55  explicit IoMonitor (EventLoop*);
56 
57  // Disable copy constructor
58  IoMonitor (const IoMonitor&) = delete;
59 
60  // Disable move constructor
61  IoMonitor (IoMonitor&&) = delete;
62 
63  // Destructor
64  ~IoMonitor() noexcept override;
65 
66  // Disable copy assignment operator (=)
67  auto operator = (const IoMonitor&) -> IoMonitor& = delete;
68 
69  // Disable move assignment operator (=) - Fixed syntax
70  auto operator = (IoMonitor&&) -> IoMonitor& = delete;
71 
72  // Accessor
73  auto getClassName() const -> FString override;
74 
75  // Method
76  template <typename T>
77  void init (int, short, handler_t, T&&);
78 
79  private:
80  void validate (int, short, const handler_t&) const;
81 };
82 
83 // IoMonitor inline functions
84 //----------------------------------------------------------------------
85 inline auto IoMonitor::getClassName() const -> FString
86 { return "IoMonitor"; }
87 
88 //----------------------------------------------------------------------
89 template <typename T>
90 inline void IoMonitor::init ( int file_descriptor, short ev
91  , handler_t hdl, T&& uc )
92 {
93  validate (file_descriptor, ev, hdl);
94 
95  try
96  {
97  setFileDescriptor (file_descriptor);
98  setEvents (ev);
99  setHandler (std::move(hdl));
100  setUserContext (std::forward<T>(uc));
101  }
102  catch (...)
103  {
104  setFileDescriptor (NO_FILE_DESCRIPTOR); // Clear file descriptor
105  setEvents (0); // Clear events
106  setHandler (handler_t{}); // Clear handler
107  clearUserContext(); // Clear user context
108  deactivate();
109  throw; // Re-throw the original exception
110  }
111 }
112 
113 //----------------------------------------------------------------------
114 inline void IoMonitor::validate ( int file_descriptor, short events
115  , const handler_t& hdl ) const
116 {
117  if ( isInitialized() )
118  throw monitor_error{"This instance has already been initialised."};
119 
120  if ( file_descriptor < 0 )
121  throw monitor_error{"Invalid file descriptor: must be >= 0 (got " +
122  std::to_string(file_descriptor) + ")"};
123  if ( events == 0 )
124  throw monitor_error{"Invalid events: must specify at least one event type."};
125 
126  if ( ! hdl )
127  throw monitor_error{"Handler cannot be null."};
128 }
129 
130 } // namespace finalcut
131 
132 #endif // IO_MONITOR_H
Definition: monitor.h:82
Definition: eventloop.h:49
Definition: class_template.cpp:25
Definition: monitor.h:53
Definition: io_monitor.h:49
Definition: fstring.h:82