Panda3D
circBuffer.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file circBuffer.I
10  * @author drose
11  * @date 1999-02-08
12  */
13 
14 #include "pandabase.h"
15 #include "config_express.h"
16 #include "pnotify.h"
17 
18 /**
19  *
20  */
21 template<class Thing, int max_size>
23 CircBuffer() {
24  _in = _out = 0;
25 }
26 
27 /**
28  *
29  */
30 template<class Thing, int max_size>
32 ~CircBuffer() {
33 }
34 
35 /**
36  * Returns the number of items currently in the buffer. This can safely be
37  * called without synchronization from either the reader or the writer thread,
38  * but the size may of course vary without warning after the call.
39  */
40 template<class Thing, int max_size>
42 size() const {
43  int diff = _in - _out;
44  return (diff >= 0) ? diff : max_size + 1 + diff;
45 }
46 
47 /**
48  * Returns true if the buffer is empty. It is safe to call this without
49  * synchronization primitives from either the reader or the writer thread, but
50  * the result may vary without warning after the call.
51  */
52 template<class Thing, int max_size>
54 empty() const {
55  return _in == _out;
56 }
57 
58 /**
59  * Returns true if the buffer is full; if this is true, push_back() will fail.
60  * It is safe to call this without synchronization primitives from either the
61  * reader or the writer thread, but the result may vary without warning after
62  * the call.
63  */
64 template<class Thing, int max_size>
66 full() const {
67  // return _in == _out-1 || (_in==max_size && _out==0);
68  return ((_in + 1) % (max_size + 1)) == _out;
69 }
70 
71 /**
72  * Returns a reference to the first item in the queue. It is invalid to call
73  * this if empty() is true. It is safe to call this without synchronization
74  * only from the reading thread: the thread that calls pop_front().
75  */
76 template<class Thing, int max_size>
77 INLINE const Thing &CircBuffer<Thing, max_size>::
78 front() const {
79  nassertr(!empty(), _array[0]);
80  return _array[_out];
81 }
82 
83 /**
84  * Returns a reference to the first item in the queue. It is invalid to call
85  * this if empty() is true. It is safe to call this without synchronization
86  * only from the reading thread: the thread that calls pop_front().
87  */
88 template<class Thing, int max_size>
90 front() {
91  nassertr(!empty(), _array[0]);
92  return _array[_out];
93 }
94 
95 /**
96  * Returns the nth element in the buffer. It is safe to call this without
97  * synchronization only from the reading thread: the thread that calls
98  * pop_front().
99  */
100 template<class Thing, int max_size>
101 INLINE const Thing &CircBuffer<Thing, max_size>::
102 operator[] (int n) const {
103  nassertr(!empty(), _array[0]);
104  return _array[(_out + n) % (max_size + 1)];
105 }
106 
107 /**
108  * Returns the nth element in the buffer. It is safe to call this without
109  * synchronization only from the reading thread: the thread that calls
110  * pop_front().
111  */
112 template<class Thing, int max_size>
113 INLINE Thing &CircBuffer<Thing, max_size>::
114 operator[] (int n) {
115  nassertr(!empty(), _array[0]);
116  return _array[(_out + n) % (max_size + 1)];
117 }
118 
119 
120 /**
121  * Removes the first item from the buffer.
122  */
123 template<class Thing, int max_size>
126  nassertv(!empty());
127 
128  // We need to clear out the old element to force its destructor to be
129  // called; it might be important. This will generate yet another UMR
130  // warning in Purify if the default constructor doesn't fully initialize the
131  // class.
132  _array[_out] = Thing();
133 
134  _out = (_out+1)%(max_size+1);
135 }
136 
137 
138 
139 
140 /**
141  * Returns a reference to the last item in the queue. It is invalid to call
142  * this if empty() is true. It is safe to call this without synchronization
143  * primitives only from the writing thread: the thread that calls push_back().
144  */
145 template<class Thing, int max_size>
146 INLINE const Thing &CircBuffer<Thing, max_size>::
147 back() const {
148  nassertr(!empty(), _array[0]);
149  return _array[(_in + max_size) % (max_size + 1)];
150 }
151 
152 /**
153  * Returns a reference to the last item in the queue. It is invalid to call
154  * this if empty() is true. It is safe to call this without synchronization
155  * primitives only from the writing thread: the thread that calls push_back().
156  */
157 template<class Thing, int max_size>
158 INLINE Thing &CircBuffer<Thing, max_size>::
159 back() {
160  nassertr(!empty(), _array[0]);
161  return _array[(_in + max_size) % (max_size + 1)];
162 }
163 
164 /**
165  * Adds an item to the end of the buffer. This may fail if full() is true.
166  */
167 template<class Thing, int max_size>
169 push_back(const Thing &t) {
170  if (full()) {
171  express_cat.error()
172  << "Circular buffer is full; cannot add requests.\n";
173  } else {
174  _array[_in] = t;
175  _in = (_in+1)%(max_size+1);
176  }
177 }
178 
179 /**
180  * Removes all items from the queue.
181  */
182 template<class Thing, int max_size>
184 clear() {
185  _in = _out = 0;
186 }
void push_back(const Thing &t)
Adds an item to the end of the buffer.
Definition: circBuffer.I:169
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool empty() const
Returns true if the buffer is empty.
Definition: circBuffer.I:54
const Thing & back() const
Returns a reference to the last item in the queue.
Definition: circBuffer.I:147
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
int size() const
Returns the number of items currently in the buffer.
Definition: circBuffer.I:42
bool full() const
Returns true if the buffer is full; if this is true, push_back() will fail.
Definition: circBuffer.I:66
void clear()
Removes all items from the queue.
Definition: circBuffer.I:184
void pop_front()
Removes the first item from the buffer.
Definition: circBuffer.I:125
This class implements a queue of some type via a circular buffer.
Definition: circBuffer.h:27
const Thing & operator[](int n) const
Returns the nth element in the buffer.
Definition: circBuffer.I:102
const Thing & front() const
Returns a reference to the first item in the queue.
Definition: circBuffer.I:78