GNU Octave  4.4.1
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-2018 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if ! defined (octave_event_queue_h)
24 #define octave_event_queue_h 1
25 
26 #include "octave-config.h"
27 
28 #include <queue>
29 #include <memory>
30 
31 #include "action-container.h"
32 
33 namespace octave
34 {
35  class
37  {
38  public:
39 
40  event_queue (void) : fifo () { }
41 
42  // No copying!
43 
44  event_queue (const event_queue&) = delete;
45 
46  event_queue& operator = (const event_queue&) = delete;
47 
48  // Destructor should not raise an exception, so all actions
49  // registered should be exception-safe. If you're not sure, see
50  // event_queue_safe.
51 
52  ~event_queue (void) { run (); }
53 
54  void add (elem *new_elem)
55  {
56  fifo.push (new_elem);
57  }
58 
59  void run_first (void)
60  {
61  if (! empty ())
62  {
63  // No leak on exception!
64  std::unique_ptr<elem> ptr (fifo.front ());
65  fifo.pop ();
66  ptr->run ();
67  }
68  }
69 
70  void discard_first (void)
71  {
72  if (! empty ())
73  {
74  elem *ptr = fifo.front ();
75  fifo.pop ();
76  delete ptr;
77  }
78  }
79 
80  size_t size (void) const { return fifo.size (); }
81 
82  protected:
83 
84  std::queue<elem *> 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  private:
95 
96  void warn_unhandled_exception (void) const;
97 
98  public:
99 
101 
102  // No copying!
103 
104  event_queue_safe (const event_queue_safe&) = delete;
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 }
124 
125 #if defined (OCTAVE_USE_DEPRECATED_FUNCTIONS)
126 
127 OCTAVE_DEPRECATED (4.4, "use 'octave::event_queue' instead")
128 typedef octave::event_queue event_queue;
129 
130 OCTAVE_DEPRECATED (4.4, "use 'octave::event_queue_safe' instead")
131 typedef octave::event_queue_safe event_queue_safe;
132 
133 #endif
134 
135 #endif
size_t size(void) const
Definition: event-queue.h:80
void run_first(void)
Definition: event-queue.h:59
create a structure array and initialize its values The dimensions of each cell array of values must match Singleton cells and non cell values are repeated so that they fill the entire array If the cells are empty
Definition: ov-struct.cc:1736
static int elem
Definition: __contourc__.cc:47
void discard_first(void)
Definition: event-queue.h:70
std::queue< elem * > fifo
Definition: event-queue.h:84
void add(elem *new_elem)
Definition: event-queue.h:54