GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
event-queue.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2012-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_event_queue_h)
27 #define octave_event_queue_h 1
28 
29 #include "octave-config.h"
30 
31 #include <queue>
32 #include <memory>
33 
34 #include "action-container.h"
35 
37 
38 class
40 {
41 public:
42 
43  event_queue () : m_fifo () { }
44 
45  OCTAVE_DISABLE_COPY_MOVE (event_queue)
46 
47  // Destructor should not raise an exception, so all actions registered
48  // should be exception-safe. If you're not sure, see event_queue_safe.
49 
50  ~event_queue () { run (); }
51 
52  void run_first ()
53  {
54  if (! empty ())
55  {
56  // No leak on exception!
57  std::unique_ptr<elem> ptr (m_fifo.front ());
58  m_fifo.pop ();
59  ptr->run ();
60  }
61  }
62 
63  void discard_first ()
64  {
65  if (! empty ())
66  {
67  elem *ptr = m_fifo.front ();
68  m_fifo.pop ();
69  delete ptr;
70  }
71  }
72 
73  std::size_t size () const { return m_fifo.size (); }
74 
75 protected:
76 
77  void add_action (elem *new_elem)
78  {
79  m_fifo.push (new_elem);
80  }
81 
82  //--------
83 
84  std::queue<elem *> m_fifo;
85 };
86 
87 // Like event_queue, but this one will guard against the
88 // possibility of seeing an exception (or interrupt) in the cleanup actions.
89 // Not that we can do much about it, but at least we won't crash.
90 
91 class
93 {
94 public:
95 
97 
98  OCTAVE_DISABLE_COPY_MOVE (event_queue_safe)
99 
101  {
102  while (! empty ())
103  {
104  try
105  {
106  run_first ();
107  }
108  catch (...) // Yes, the black hole. Remember we're in a dtor.
109  {
110  warn_unhandled_exception ();
111  }
112  }
113  }
114 
115 private:
116 
117  void warn_unhandled_exception () const;
118 
119 };
120 
121 OCTAVE_END_NAMESPACE(octave)
122 
123 #endif
void run_first()
Definition: event-queue.h:52
void discard_first()
Definition: event-queue.h:63
std::size_t size() const
Definition: event-queue.h:73
std::queue< elem * > m_fifo
Definition: event-queue.h:84
void add_action(elem *new_elem)
Definition: event-queue.h:77
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn