1 | /* |
---|
2 | * Copyright (c) 2005, Eric Crahen |
---|
3 | * |
---|
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
5 | * of this software and associated documentation files (the "Software"), to deal |
---|
6 | * in the Software without restriction, including without limitation the rights |
---|
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
8 | * copies of the Software, and to permit persons to whom the Software is furnished |
---|
9 | * to do so, subject to the following conditions: |
---|
10 | * |
---|
11 | * The above copyright notice and this permission notice shall be included in all |
---|
12 | * copies or substantial portions of the Software. |
---|
13 | * |
---|
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
---|
18 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
---|
19 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | #ifndef __ZTQUEUE_H__ |
---|
24 | #define __ZTQUEUE_H__ |
---|
25 | |
---|
26 | #include "zthread/Cancelable.h" |
---|
27 | #include "zthread/NonCopyable.h" |
---|
28 | |
---|
29 | namespace ZThread { |
---|
30 | |
---|
31 | /** |
---|
32 | * @class Queue |
---|
33 | * @author Eric Crahen <http://www.code-foo.com> |
---|
34 | * @date <2003-07-16T11:32:42-0400> |
---|
35 | * @version 2.3.0 |
---|
36 | * |
---|
37 | * A Queue defines an interface for a value-oriented collection objects (similar to |
---|
38 | * STL collections). |
---|
39 | */ |
---|
40 | template <typename T> |
---|
41 | class Queue : public Cancelable, private NonCopyable { |
---|
42 | public: |
---|
43 | |
---|
44 | //! Destroy a Queue |
---|
45 | virtual ~Queue() { } |
---|
46 | |
---|
47 | /** |
---|
48 | * Add an object to this Queue. |
---|
49 | * |
---|
50 | * @param item value to be added to the Queue |
---|
51 | * |
---|
52 | * @exception Cancellation_Exception thrown if this Queue has been canceled. |
---|
53 | * |
---|
54 | * @pre The Queue should not have been canceled prior to the invocation of this function. |
---|
55 | * @post If no exception is thrown, a copy of <i>item</i> will have been added to the Queue. |
---|
56 | */ |
---|
57 | virtual void add(const T& item) = 0; |
---|
58 | |
---|
59 | /** |
---|
60 | * Add an object to this Queue. |
---|
61 | * |
---|
62 | * @param item value to be added to the Queue |
---|
63 | * @param timeout maximum amount of time (milliseconds) this method may block |
---|
64 | * the calling thread. |
---|
65 | * |
---|
66 | * @return |
---|
67 | * - <em>true</em> if a copy of <i>item</i> can be added before <i>timeout</i> |
---|
68 | * milliseconds elapse. |
---|
69 | * - <em>false</em> otherwise. |
---|
70 | * |
---|
71 | * @exception Cancellation_Exception thrown if this Queue has been canceled. |
---|
72 | * |
---|
73 | * @pre The Queue should not have been canceled prior to the invocation of this function. |
---|
74 | * @post If this function returns true a copy of <i>item</i> will have been added to the Queue. |
---|
75 | */ |
---|
76 | virtual bool add(const T& item, unsigned long timeout) = 0; |
---|
77 | |
---|
78 | /** |
---|
79 | * Retrieve and remove a value from this Queue. |
---|
80 | * |
---|
81 | * @return <em>T</em> next available value |
---|
82 | * |
---|
83 | * @exception Cancellation_Exception thrown if this Queue has been canceled. |
---|
84 | * |
---|
85 | * @pre The Queue should not have been canceled prior to the invocation of this function. |
---|
86 | * @post The value returned will have been removed from the Queue. |
---|
87 | */ |
---|
88 | virtual T next() = 0; |
---|
89 | |
---|
90 | /** |
---|
91 | * Retrieve and remove a value from this Queue. |
---|
92 | * |
---|
93 | * @param timeout maximum amount of time (milliseconds) this method may block |
---|
94 | * the calling thread. |
---|
95 | * |
---|
96 | * @return <em>T</em> next available value |
---|
97 | * |
---|
98 | * @exception Cancellation_Exception thrown if this Queue has been canceled. |
---|
99 | * @exception Timeout_Exception thrown if the timeout expires before a value |
---|
100 | * can be retrieved. |
---|
101 | * |
---|
102 | * @pre The Queue should not have been canceled prior to the invocation of this function. |
---|
103 | * @post The value returned will have been removed from the Queue. |
---|
104 | */ |
---|
105 | virtual T next(unsigned long timeout) = 0; |
---|
106 | |
---|
107 | /** |
---|
108 | * Canceling a Queue disables it, disallowing further additions. Values already |
---|
109 | * present in the Queue can still be retrieved and are still available through |
---|
110 | * the next() methods. |
---|
111 | * |
---|
112 | * Canceling a Queue more than once has no effect. |
---|
113 | * |
---|
114 | * @post The next() methods will continue to return objects until |
---|
115 | * the Queue has been emptied. |
---|
116 | * @post Once emptied, the next() methods will throw a Cancellation_Exception. |
---|
117 | * @post The add() methods will throw a Cancellation_Exceptions from this point on. |
---|
118 | */ |
---|
119 | virtual void cancel() = 0; |
---|
120 | |
---|
121 | /** |
---|
122 | * Count the values present in this Queue. |
---|
123 | * |
---|
124 | * @return <em>size_t</em> number of elements available in the Queue. |
---|
125 | */ |
---|
126 | virtual size_t size() = 0; |
---|
127 | |
---|
128 | /** |
---|
129 | * Count the values present in this Queue. |
---|
130 | * |
---|
131 | * @param timeout maximum amount of time (milliseconds) this method may block |
---|
132 | * the calling thread. |
---|
133 | * |
---|
134 | * @return <em>size_t</em> number of elements available in the Queue. |
---|
135 | * |
---|
136 | * @exception Timeout_Exception thrown if <i>timeout</i> milliseconds |
---|
137 | * expire before a value becomes available |
---|
138 | */ |
---|
139 | virtual size_t size(unsigned long timeout) = 0; |
---|
140 | |
---|
141 | /** |
---|
142 | * Test whether any values are available in this Queue. |
---|
143 | * |
---|
144 | * @return |
---|
145 | * - <em>true</em> if there are no values available. |
---|
146 | * - <em>false</em> if there <i>are</i> values available. |
---|
147 | */ |
---|
148 | virtual bool empty() { |
---|
149 | |
---|
150 | try { |
---|
151 | |
---|
152 | return size() == 0; |
---|
153 | |
---|
154 | } catch(Cancellation_Exception&) { } |
---|
155 | |
---|
156 | return true; |
---|
157 | |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * Test whether any values are available in this Queue. |
---|
162 | * |
---|
163 | * @param timeout maximum amount of time (milliseconds) this method may block |
---|
164 | * the calling thread. |
---|
165 | * |
---|
166 | * @return |
---|
167 | * - <em>true</em> if there are no values available. |
---|
168 | * - <em>false</em> if there <i>are</i> values available. |
---|
169 | * |
---|
170 | * @exception Timeout_Exception thrown if <i>timeout</i> milliseconds |
---|
171 | * expire before a value becomes available |
---|
172 | */ |
---|
173 | virtual bool empty(unsigned long timeout) { |
---|
174 | |
---|
175 | try { |
---|
176 | |
---|
177 | return size(timeout) == 0; |
---|
178 | |
---|
179 | } catch(Cancellation_Exception&) { } |
---|
180 | |
---|
181 | return true; |
---|
182 | |
---|
183 | } |
---|
184 | |
---|
185 | }; /* Queue */ |
---|
186 | |
---|
187 | } // namespace ZThread |
---|
188 | |
---|
189 | #endif // __ZTQUEUE_H__ |
---|