FINAL CUT
eventloop.h
1 /***********************************************************************
2 * eventloop.h - Implements the event loop *
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 /* Standalone class
24  * ════════════════
25  *
26  * ▕▔▔▔▔▔▔▔▔▔▔▔▏
27  * ▕ EventLoop ▏
28  * ▕▁▁▁▁▁▁▁▁▁▁▁▏
29  */
30 
31 #ifndef EVENTLOOP_H
32 #define EVENTLOOP_H
33 
34 #include <list>
35 #include <poll.h>
36 #include <array>
37 
38 #include "final/eventloop/monitor.h"
39 #include "final/util/fstring.h"
40 
41 namespace finalcut
42 {
43 
44 //----------------------------------------------------------------------
45 // class EventLoop
46 //----------------------------------------------------------------------
47 
48 class EventLoop
49 {
50  public:
51  // Constructor
52  EventLoop() = default;
53 
54  // Accessor
55  auto getClassName() const -> FString;
56 
57  // Methods
58  auto run() -> int;
59  void leave();
60 
61  private:
62  // Constants
63  static constexpr nfds_t MAX_MONITORS{50};
64  static constexpr int WAIT_INDEFINITELY{-1};
65 
66  // Methods
67  void nonPollWaiting() const;
68  auto processNextEvents() -> bool;
69  void dispatcher (int, nfds_t);
70  void addMonitor (Monitor*);
71  void removeMonitor (Monitor*);
72 
73  // Data members
74  bool running{false};
75  bool monitors_changed{false};
76  std::list<Monitor*> monitors{};
77  std::array<struct pollfd, MAX_MONITORS> fds{};
78  std::array<Monitor*, MAX_MONITORS> lookup_table{};
79 
80  // Friend classes
81  friend class Monitor;
82 };
83 
84 // EventLoop inline functions
85 //----------------------------------------------------------------------
86 inline auto EventLoop::getClassName() const -> FString
87 { return "EventLoop"; }
88 
89 //----------------------------------------------------------------------
90 inline void EventLoop::leave()
91 { running = false; }
92 
93 } // namespace finalcut
94 
95 #endif // EVENTLOOP_H
Definition: monitor.h:81
Definition: eventloop.h:48
Definition: class_template.cpp:25
Definition: fstring.h:79