GNU Octave  3.8.0
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-2013 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 <queue>
27 #include <memory>
28 
29 #include "action-container.h"
30 
31 class
33 {
34 public:
35 
36  event_queue (void) : fifo () { }
37 
38  // Destructor should not raise an exception, so all actions
39  // registered should be exception-safe (but setting error_state is
40  // allowed). If you're not sure, see event_queue_safe.
41 
42  ~event_queue (void) { run (); }
43 
44  void add (elem *new_elem)
45  {
46  fifo.push (new_elem);
47  }
48 
49  void run_first (void)
50  {
51  if (! empty ())
52  {
53  // No leak on exception!
54  std::auto_ptr<elem> ptr (fifo.front ());
55  fifo.pop ();
56  ptr->run ();
57  }
58  }
59 
60  void discard_first (void)
61  {
62  if (! empty ())
63  {
64  elem *ptr = fifo.front ();
65  fifo.pop ();
66  delete ptr;
67  }
68  }
69 
70  size_t size (void) const { return fifo.size (); }
71 
72 protected:
73 
74  std::queue<elem *> fifo;
75 
76 private:
77 
78  // No copying!
79 
80  event_queue (const event_queue&);
81 
83 };
84 
85 // Like event_queue, but this one will guard against the
86 // possibility of seeing an exception (or interrupt) in the cleanup
87 // actions. Not that we can do much about it, but at least we won't
88 // crash.
89 
90 class
92 {
93 private:
94 
95  static void gripe_exception (void);
96 
97 public:
98 
99  event_queue_safe (void) : event_queue () { }
100 
102  {
103  while (! empty ())
104  {
105  try
106  {
107  run_first ();
108  }
109  catch (...) // Yes, the black hole. Remember we're in a dtor.
110  {
111  gripe_exception ();
112  }
113  }
114  }
115 
116 private:
117 
118  // No copying!
119 
121 
123 };
124 
125 #endif