GNU Octave 7.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-2022 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
36namespace octave
37{
38 class
40 {
41 public:
42
43 event_queue (void) : m_fifo () { }
44
45 // No copying!
46
47 event_queue (const event_queue&) = delete;
48
49 event_queue& operator = (const event_queue&) = delete;
50
51 // Destructor should not raise an exception, so all actions registered
52 // should be exception-safe. If you're not sure, see event_queue_safe.
53
54 ~event_queue (void) { run (); }
55
56 void run_first (void)
57 {
58 if (! empty ())
59 {
60 // No leak on exception!
61 std::unique_ptr<elem> ptr (m_fifo.front ());
62 m_fifo.pop ();
63 ptr->run ();
64 }
65 }
66
67 void discard_first (void)
68 {
69 if (! empty ())
70 {
71 elem *ptr = m_fifo.front ();
72 m_fifo.pop ();
73 delete ptr;
74 }
75 }
76
77 std::size_t size (void) const { return m_fifo.size (); }
78
79 protected:
80
81 void add_action (elem *new_elem)
82 {
83 m_fifo.push (new_elem);
84 }
85
86 //--------
87
88 std::queue<elem *> m_fifo;
89 };
90
91 // Like event_queue, but this one will guard against the
92 // possibility of seeing an exception (or interrupt) in the cleanup actions.
93 // Not that we can do much about it, but at least we won't crash.
94
95 class
97 {
98 public:
99
101
102 // No copying!
103
105
106 event_queue_safe& operator = (const event_queue_safe&) = delete;
107
109 {
110 while (! empty ())
111 {
112 try
113 {
114 run_first ();
115 }
116 catch (...) // Yes, the black hole. Remember we're in a dtor.
117 {
118 warn_unhandled_exception ();
119 }
120 }
121 }
122
123 private:
124
125 void warn_unhandled_exception (void) const;
126
127 };
128}
129
130#endif
event_queue_safe(const event_queue_safe &)=delete
std::queue< elem * > m_fifo
Definition: event-queue.h:88
std::size_t size(void) const
Definition: event-queue.h:77
event_queue(const event_queue &)=delete
void discard_first(void)
Definition: event-queue.h:67
void add_action(elem *new_elem)
Definition: event-queue.h:81
void run_first(void)
Definition: event-queue.h:56