Panda3D
Loading...
Searching...
No Matches
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 */
21template<class Thing, int max_size>
23CircBuffer() {
24 _in = _out = 0;
25}
26
27/**
28 *
29 */
30template<class Thing, int max_size>
33}
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 */
40template<class Thing, int max_size>
42size() const {
43 int diff = _in - _out;
44 return (diff >= 0) ? diff : max_size + 1 + diff;
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 */
52template<class Thing, int max_size>
54empty() 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 */
64template<class Thing, int max_size>
66full() 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 */
76template<class Thing, int max_size>
78front() 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 */
88template<class Thing, int max_size>
90front() {
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 */
100template<class Thing, int max_size>
102operator[] (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 */
112template<class Thing, int max_size>
114operator[] (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 */
123template<class Thing, int max_size>
125pop_front() {
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 */
145template<class Thing, int max_size>
147back() 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 */
157template<class Thing, int max_size>
159back() {
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 */
167template<class Thing, int max_size>
169push_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 */
182template<class Thing, int max_size>
184clear() {
185 _in = _out = 0;
186}
This class implements a queue of some type via a circular buffer.
Definition circBuffer.h:27
void clear()
Removes all items from the queue.
Definition circBuffer.I:184
const Thing & back() const
Returns a reference to the last item in the queue.
Definition circBuffer.I:147
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
bool empty() const
Returns true if the buffer is empty.
Definition circBuffer.I:54
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
void pop_front()
Removes the first item from the buffer.
Definition circBuffer.I:125
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.