VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/ds/nsDeque.h@ 62264

Last change on this file since 62264 was 62264, checked in by vboxsync, 8 years ago

xpcom: finally remove VBOX_GCC_Wno-delete-non-virtual-dtor as this warning is useful and disabling it would hide potential leaks

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/**
39 * MODULE NOTES:
40 *
41 * The Deque is a very small, very efficient container object
42 * than can hold elements of type void*, offering the following features:
43 * Its interface supports pushing and popping of elements.
44 * It can iterate (via an interator class) its elements.
45 * When full, it can efficiently resize dynamically.
46 *
47 *
48 * NOTE: The only bit of trickery here is that this deque is
49 * built upon a ring-buffer. Like all ring buffers, the first
50 * element may not be at index[0]. The mOrigin member determines
51 * where the first child is. This point is quietly hidden from
52 * customers of this class.
53 *
54 */
55
56#ifndef _NSDEQUE
57#define _NSDEQUE
58
59#include "nscore.h"
60
61/**
62 * The nsDequeFunctor class is used when you want to create
63 * callbacks between the deque and your generic code.
64 * Use these objects in a call to ForEach();
65 *
66 */
67
68class nsDequeFunctor{
69public:
70 virtual ~nsDequeFunctor() {}
71 virtual void* operator()(void* anObject)=0;
72};
73
74/******************************************************
75 * Here comes the nsDeque class itself...
76 ******************************************************/
77
78/**
79 * The deque (double-ended queue) class is a common container type,
80 * whose behavior mimics a line in your favorite checkout stand.
81 * Classic CS describes the common behavior of a queue as FIFO.
82 * A deque allows insertion and removal at both ends of
83 * the container.
84 *
85 * The deque stores pointers to items.
86 */
87
88class nsDequeIterator;
89
90class NS_COM nsDeque {
91 friend class nsDequeIterator;
92 public:
93 nsDeque(nsDequeFunctor* aDeallocator);
94 ~nsDeque();
95
96 /**
97 * Returns the number of elements currently stored in
98 * this deque.
99 *
100 * @return number of elements currently in the deque
101 */
102 inline PRInt32 GetSize() const {return mSize;}
103
104 /**
105 * Appends new member at the end of the deque.
106 *
107 * @param item to store in deque
108 * @return *this
109 */
110 nsDeque& Push(void* aItem);
111
112 /**
113 * Inserts new member at the front of the deque.
114 *
115 * @param item to store in deque
116 * @return *this
117 */
118 nsDeque& PushFront(void* aItem);
119
120 /**
121 * Remove and return the last item in the container.
122 *
123 * @return the item that was the last item in container
124 */
125 void* Pop();
126
127 /**
128 * Remove and return the first item in the container.
129 *
130 * @return the item that was first item in container
131 */
132 void* PopFront();
133
134 /**
135 * Retrieve the bottom item without removing it.
136 *
137 * @return the first item in container
138 */
139
140 void* Peek();
141 /**
142 * Return topmost item without removing it.
143 *
144 * @return the first item in container
145 */
146 void* PeekFront();
147
148 /**
149 * Retrieve the i'th member from the deque without removing it.
150 *
151 * @param index of desired item
152 * @return i'th element in list
153 */
154 void* ObjectAt(int aIndex) const;
155
156 /**
157 * Remove all items from container without destroying them.
158 *
159 * @return *this
160 */
161 nsDeque& Empty();
162
163 /**
164 * Remove and delete all items from container.
165 * Deletes are handled by the deallocator nsDequeFunctor
166 * which is specified at deque construction.
167 *
168 * @return *this
169 */
170 nsDeque& Erase();
171
172 /**
173 * Creates a new iterator, pointing to the first
174 * item in the deque.
175 *
176 * @return new dequeIterator
177 */
178 nsDequeIterator Begin() const;
179
180 /**
181 * Creates a new iterator, pointing to the last
182 * item in the deque.
183 *
184 * @return new dequeIterator
185 */
186 nsDequeIterator End() const;
187
188 void* Last() const;
189 /**
190 * Call this method when you want to iterate all the
191 * members of the container, passing a functor along
192 * to call your code.
193 *
194 * @param aFunctor object to call for each member
195 * @return *this
196 */
197 void ForEach(nsDequeFunctor& aFunctor) const;
198
199 /**
200 * Call this method when you want to iterate all the
201 * members of the container, calling the functor you
202 * passed with each member. This process will interrupt
203 * if your function returns non 0 to this method.
204 *
205 * @param aFunctor object to call for each member
206 * @return first nonzero result of aFunctor or 0.
207 */
208 const void* FirstThat(nsDequeFunctor& aFunctor) const;
209
210 void SetDeallocator(nsDequeFunctor* aDeallocator);
211
212protected:
213 PRInt32 mSize;
214 PRInt32 mCapacity;
215 PRInt32 mOrigin;
216 nsDequeFunctor* mDeallocator;
217 void* mBuffer[8];
218 void** mData;
219
220private:
221
222 /**
223 * Simple default constructor (PRIVATE)
224 */
225 nsDeque();
226
227 /**
228 * Copy constructor (PRIVATE)
229 *
230 * @param another deque
231 */
232 nsDeque(const nsDeque& other);
233
234 /**
235 * Deque assignment operator (PRIVATE)
236 *
237 * @param another deque
238 * @return *this
239 */
240 nsDeque& operator=(const nsDeque& anOther);
241
242 PRInt32 GrowCapacity();
243};
244
245/******************************************************
246 * Here comes the nsDequeIterator class...
247 ******************************************************/
248
249class nsDequeIterator {
250public:
251 /**
252 * DequeIterator is an object that knows how to iterate
253 * (forward and backward) through a Deque. Normally,
254 * you don't need to do this, but there are some special
255 * cases where it is pretty handy.
256 *
257 * One warning: the iterator is not bound to an item,
258 * it is bound to an index, so if you insert or remove
259 * from the beginning while using an iterator
260 * (which is not recommended) then the iterator will
261 * point to a different item. @see GetCurrent()
262 *
263 * Here you go.
264 *
265 * @param aQueue is the deque object to be iterated
266 * @param aIndex is the starting position for your iteration
267 */
268 nsDequeIterator(const nsDeque& aQueue, int aIndex=0);
269
270 /**
271 * Create a copy of a DequeIterator
272 *
273 * @param aCopy is another iterator to copy from
274 */
275 nsDequeIterator(const nsDequeIterator& aCopy);
276
277 /**
278 * Moves iterator to first element in the deque
279 * @return *this
280 */
281 nsDequeIterator& First();
282
283 /**
284 * Standard assignment operator for dequeiterator
285 * @param aCopy is another iterator to copy from
286 * @return *this
287 */
288 nsDequeIterator& operator=(const nsDequeIterator& aCopy);
289
290 /**
291 * preform ! operation against two iterators to test for equivalence
292 * (or lack thereof)!
293 *
294 * @param aIter is the object to be compared to
295 * @return TRUE if NOT equal.
296 */
297 PRBool operator!=(nsDequeIterator& aIter);
298
299 /**
300 * Compare two iterators for increasing order.
301 *
302 * @param aIter is the other iterator to be compared to
303 * @return TRUE if this object points to an element before
304 * the element pointed to by aIter.
305 * FALSE if this and aIter are not iterating over
306 * the same deque.
307 */
308 PRBool operator<(nsDequeIterator& aIter);
309
310 /**
311 * Compare two iterators for equivalence.
312 *
313 * @param aIter is the other iterator to be compared to
314 * @return TRUE if EQUAL
315 */
316 PRBool operator==(nsDequeIterator& aIter);
317
318 /**
319 * Compare two iterators for non strict decreasing order.
320 *
321 * @param aIter is the other iterator to be compared to
322 * @return TRUE if this object points to the same element, or
323 * an element after the element pointed to by aIter.
324 * FALSE if this and aIter are not iterating over
325 * the same deque.
326 */
327 PRBool operator>=(nsDequeIterator& aIter);
328
329 /**
330 * Pre-increment operator
331 * Iterator will advance one index towards the end.
332 *
333 * @return object_at(++index)
334 */
335 void* operator++();
336
337 /**
338 * Post-increment operator
339 * Iterator will advance one index towards the end.
340 *
341 * @param param is ignored
342 * @return object_at(mIndex++)
343 */
344 void* operator++(int);
345
346 /**
347 * Pre-decrement operator
348 * Iterator will advance one index towards the beginning.
349 *
350 * @return object_at(--index)
351 */
352 void* operator--();
353
354 /**
355 * Post-decrement operator
356 * Iterator will advance one index towards the beginning.
357 *
358 * @param param is ignored
359 * @return object_at(index--)
360 */
361 void* operator--(int);
362
363 /**
364 * Retrieve the the iterator's notion of current node.
365 *
366 * Note that the iterator floats, so you don't need to do:
367 * <code>++iter; aDeque.PopFront();</code>
368 * Unless you actually want your iterator to jump 2 positions
369 * relative to its origin.
370 *
371 * Picture: [1 2i 3 4]
372 * PopFront()
373 * Picture: [2 3i 4]
374 * Note that I still happily points to object at the second index.
375 *
376 * @return object at i'th index
377 */
378 void* GetCurrent();
379
380 /**
381 * Call this method when you want to iterate all the
382 * members of the container, passing a functor along
383 * to call your code.
384 *
385 * @param aFunctor object to call for each member
386 * @return *this
387 */
388 void ForEach(nsDequeFunctor& aFunctor) const;
389
390 /**
391 * Call this method when you want to iterate all the
392 * members of the container, calling the functor you
393 * passed with each member. This process will interrupt
394 * if your function returns non 0 to this method.
395 *
396 * @param aFunctor object to call for each member
397 * @return first nonzero result of aFunctor or 0.
398 */
399 const void* FirstThat(nsDequeFunctor& aFunctor) const;
400
401 protected:
402
403 PRInt32 mIndex;
404 const nsDeque& mDeque;
405};
406#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette