Mercurial > octave
annotate libinterp/corefcn/event-queue.h @ 18794:03c2671493f9
maint: Periodic merge of gui-release to default.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 02 Jun 2014 11:17:59 -0700 |
parents | 175b392e91fe |
children | 4197fc428c7d |
rev | line source |
---|---|
15396 | 1 /* |
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 | 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 | |
32 event_queue : public action_container | |
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 | |
82 event_queue& operator = (const event_queue&); | |
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 | |
91 event_queue_safe : public event_queue | |
92 { | |
93 private: | |
94 | |
95 static void gripe_exception (void); | |
96 | |
97 public: | |
98 | |
99 event_queue_safe (void) : event_queue () { } | |
100 | |
101 ~event_queue_safe (void) | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
102 { |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
103 while (! empty ()) |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
104 { |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
105 try |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
106 { |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
107 run_first (); |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
108 } |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
109 catch (...) // Yes, the black hole. Remember we're in a dtor. |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
110 { |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
111 gripe_exception (); |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
112 } |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
113 } |
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
114 } |
15396 | 115 |
116 private: | |
117 | |
118 // No copying! | |
119 | |
120 event_queue_safe (const event_queue_safe&); | |
121 | |
122 event_queue_safe& operator = (const event_queue_safe&); | |
123 }; | |
124 | |
125 #endif |