annotate libinterp/corefcn/event-queue.h @ 17744:d63878346099

maint: Update copyright notices for release.
author John W. Eaton <jwe@octave.org>
date Wed, 23 Oct 2013 22:09:27 -0400
parents 68fc671a9339
children 175b392e91fe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15396
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
1 /*
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
2
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 16892
diff changeset
3 Copyright (C) 2012-2013 John W. Eaton
15396
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
4
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
5 This file is part of Octave.
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
7 Octave is free software; you can redistribute it and/or modify it
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 option) any later version.
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
12 Octave is distributed in the hope that it will be useful, but WITHOUT
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
15 for more details.
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
16
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
18 along with Octave; see the file COPYING. If not, see
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>.
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
20
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
21 */
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
23 #if !defined (octave_event_queue_h)
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
24 #define octave_event_queue_h 1
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
26 #include <queue>
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
27 #include <memory>
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29 #include "action-container.h"
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31 class
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32 event_queue : public action_container
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 {
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34 public:
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36 event_queue (void) : fifo () { }
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38 // Destructor should not raise an exception, so all actions
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39 // registered should be exception-safe (but setting error_state is
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40 // allowed). If you're not sure, see event_queue_safe.
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42 ~event_queue (void) { run (); }
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
44 void add (elem *new_elem)
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45 {
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
46 fifo.push (new_elem);
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47 }
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
48
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
49 void run_first (void)
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
50 {
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
51 if (! empty ())
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52 {
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
53 // No leak on exception!
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
54 std::auto_ptr<elem> ptr (fifo.front ());
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
55 fifo.pop ();
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
56 ptr->run ();
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
57 }
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
58 }
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
59
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
60 void discard_first (void)
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
61 {
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
62 if (! empty ())
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
63 {
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
64 elem *ptr = fifo.front ();
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
65 fifo.pop ();
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
66 delete ptr;
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
67 }
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
68 }
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
69
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
70 size_t size (void) const { return fifo.size (); }
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
71
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
72 protected:
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
73
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
74 std::queue<elem *> fifo;
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
75
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
76 private:
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
77
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
78 // No copying!
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
79
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
80 event_queue (const event_queue&);
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
81
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
82 event_queue& operator = (const event_queue&);
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
83 };
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
84
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
85 // Like event_queue, but this one will guard against the
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
86 // possibility of seeing an exception (or interrupt) in the cleanup
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
87 // actions. Not that we can do much about it, but at least we won't
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
88 // crash.
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
89
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
90 class
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
91 event_queue_safe : public event_queue
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
92 {
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
93 private:
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
94
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
95 static void gripe_exception (void);
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
96
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
97 public:
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
98
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
99 event_queue_safe (void) : event_queue () { }
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
100
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
101 ~event_queue_safe (void)
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
102 {
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
103 while (! empty ())
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
104 {
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
105 try
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
106 {
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
107 run_first ();
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
108 }
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
109 catch (...) // Yes, the black hole. Remember we're in a dtor.
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
110 {
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
111 gripe_exception ();
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
112 }
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
113 }
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
114 }
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
115
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
116 private:
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
117
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
118 // No copying!
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
119
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
120 event_queue_safe (const event_queue_safe&);
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
121
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
122 event_queue_safe& operator = (const event_queue_safe&);
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
123 };
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
124
1054ab58cd58 abstract unwind_protect
John W. Eaton <jwe@octave.org>
parents:
diff changeset
125 #endif