Panda3D
 All Classes Functions Variables Enumerations
circBuffer.h
1 // Filename: circBuffer.h
2 // Created by: drose (08Feb99)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef CIRCBUFFER_H
16 #define CIRCBUFFER_H
17 
18 #include "pandabase.h"
19 
20 ////////////////////////////////////////////////////////////////////
21 // Class : CircBuffer
22 // Description : This class implements a queue of some type via a
23 // circular buffer. The circular buffer has the
24 // advantage that no synchronization is required when
25 // one process adds to the queue while another process
26 // extracts. It works for any kind of Thing that has a
27 // valid assignment operator and copy constructor
28 // defined.
29 ////////////////////////////////////////////////////////////////////
30 template<class Thing, int max_size>
31 class CircBuffer {
32 public:
33  INLINE CircBuffer();
34  INLINE ~CircBuffer();
35 
36  // Methods that are safe to call without synchronization primitives
37  // from either thread.
38  INLINE int size() const;
39 
40  // Methods that are safe to call without synchronization primitives
41  // only from the reader thread.
42  INLINE bool empty() const;
43 
44  INLINE const Thing &front() const;
45  INLINE Thing &front();
46  INLINE void pop_front();
47 
48  INLINE const Thing &operator[] (int n) const;
49  INLINE Thing &operator[] (int n);
50 
51  // Methods that are safe to call without synchronization primitives
52  // only from the writer thread.
53  INLINE bool full() const;
54 
55  INLINE const Thing &back() const;
56  INLINE Thing &back();
57  INLINE void push_back(const Thing &t);
58 
59  INLINE void clear();
60 
61 private:
62  Thing _array[max_size+1];
63  int _in, _out;
64 };
65 
66 #include "circBuffer.I"
67 
68 #endif
void push_back(const Thing &t)
Adds an item to the end of the buffer.
Definition: circBuffer.I:207
void clear()
Removes all items from the queue.
Definition: circBuffer.I:224
bool full() const
Returns true if the buffer is full; if this is true, push_back() will fail.
Definition: circBuffer.I:81
void pop_front()
Removes the first item from the buffer.
Definition: circBuffer.I:152
const Thing & front() const
Returns a reference to the first item in the queue.
Definition: circBuffer.I:96
const Thing & operator[](int n) const
Returns the nth element in the buffer.
Definition: circBuffer.I:125
const Thing & back() const
Returns a reference to the last item in the queue.
Definition: circBuffer.I:178
This class implements a queue of some type via a circular buffer.
Definition: circBuffer.h:31
bool empty() const
Returns true if the buffer is empty.
Definition: circBuffer.I:66
int size() const
Returns the number of items currently in the buffer.
Definition: circBuffer.I:51