GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
event-queue.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2012-2025 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
39{
40public:
41
42 event_queue () : m_fifo () { }
43
44 OCTAVE_DISABLE_COPY_MOVE (event_queue)
45
46 // Destructor should not raise an exception, so all actions registered
47 // should be exception-safe. If you're not sure, see event_queue_safe.
48
49 ~event_queue () { run (); }
50
51 void run_first ()
52 {
53 if (! empty ())
54 {
55 // No leak on exception!
56 std::unique_ptr<elem> ptr (m_fifo.front ());
57 m_fifo.pop ();
58 ptr->run ();
59 }
60 }
61
63 {
64 if (! empty ())
65 {
66 elem *ptr = m_fifo.front ();
67 m_fifo.pop ();
68 delete ptr;
69 }
70 }
71
72 std::size_t size () const { return m_fifo.size (); }
73
74protected:
75
76 void add_action (elem *new_elem)
77 {
78 m_fifo.push (new_elem);
79 }
80
81 //--------
82
83 std::queue<elem *> m_fifo;
84};
85
86// Like event_queue, but this one will guard against the
87// possibility of seeing an exception (or interrupt) in the cleanup actions.
88// Not that we can do much about it, but at least we won't crash.
89
91{
92public:
93
95
96 OCTAVE_DISABLE_COPY_MOVE (event_queue_safe)
97
99 {
100 while (! empty ())
101 {
102 try
103 {
104 run_first ();
105 }
106 catch (...) // Yes, the black hole. Remember we're in a dtor.
107 {
108 warn_unhandled_exception ();
109 }
110 }
111 }
112
113private:
114
115 void warn_unhandled_exception () const;
116
117};
118
119OCTAVE_END_NAMESPACE(octave)
120
121#endif
void run_first()
Definition event-queue.h:51
void discard_first()
Definition event-queue.h:62
std::size_t size() const
Definition event-queue.h:72
std::queue< elem * > m_fifo
Definition event-queue.h:83
void add_action(elem *new_elem)
Definition event-queue.h:76
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn