GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
event-queue.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2012-2017 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 <http://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 class
35 {
36 public:
37 
38  event_queue (void) : fifo () { }
39 
40  // Destructor should not raise an exception, so all actions
41  // registered should be exception-safe. If you're not sure, see
42  // event_queue_safe.
43 
44  ~event_queue (void) { run (); }
45 
46  void add (elem *new_elem)
47  {
48  fifo.push (new_elem);
49  }
50 
51  void run_first (void)
52  {
53  if (! empty ())
54  {
55  // No leak on exception!
56  std::unique_ptr<elem> ptr (fifo.front ());
57  fifo.pop ();
58  ptr->run ();
59  }
60  }
61 
62  void discard_first (void)
63  {
64  if (! empty ())
65  {
66  elem *ptr = fifo.front ();
67  fifo.pop ();
68  delete ptr;
69  }
70  }
71 
72  size_t size (void) const { return fifo.size (); }
73 
74 protected:
75 
76  std::queue<elem *> fifo;
77 
78 private:
79 
80  // No copying!
81 
82  event_queue (const event_queue&);
83 
84  event_queue& operator = (const event_queue&);
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 
103  {
104  while (! empty ())
105  {
106  try
107  {
108  run_first ();
109  }
110  catch (...) // Yes, the black hole. Remember we're in a dtor.
111  {
112  warn_unhandled_exception ();
113  }
114  }
115  }
116 
117 private:
118 
119  // No copying!
120 
122 
123  event_queue_safe& operator = (const event_queue_safe&);
124 };
125 
126 #endif
~event_queue_safe(void)
Definition: event-queue.h:102
event_queue(void)
Definition: event-queue.h:38
event_queue_safe(void)
Definition: event-queue.h:100
std::queue< elem * > fifo
Definition: event-queue.h:76
size_t size(void) const
Definition: event-queue.h:72
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:1688
static int elem
Definition: __contourc__.cc:50
void run_first(void)
Definition: event-queue.h:51
void discard_first(void)
Definition: event-queue.h:62
~event_queue(void)
Definition: event-queue.h:44
void add(elem *new_elem)
Definition: event-queue.h:46