Panda3D
circBuffer.h
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.h
10  * @author drose
11  * @date 1999-02-08
12  */
13 
14 #ifndef CIRCBUFFER_H
15 #define CIRCBUFFER_H
16 
17 #include "pandabase.h"
18 
19 /**
20  * This class implements a queue of some type via a circular buffer. The
21  * circular buffer has the advantage that no synchronization is required when
22  * one process adds to the queue while another process extracts. It works for
23  * any kind of Thing that has a valid assignment operator and copy constructor
24  * defined.
25  */
26 template<class Thing, int max_size>
27 class CircBuffer {
28 public:
29  INLINE CircBuffer();
30  INLINE ~CircBuffer();
31 
32  // Methods that are safe to call without synchronization primitives from
33  // either thread.
34  INLINE int size() const;
35 
36  // Methods that are safe to call without synchronization primitives only
37  // from the reader thread.
38  INLINE bool empty() const;
39 
40  INLINE const Thing &front() const;
41  INLINE Thing &front();
42  INLINE void pop_front();
43 
44  INLINE const Thing &operator[] (int n) const;
45  INLINE Thing &operator[] (int n);
46 
47  // Methods that are safe to call without synchronization primitives only
48  // from the writer thread.
49  INLINE bool full() const;
50 
51  INLINE const Thing &back() const;
52  INLINE Thing &back();
53  INLINE void push_back(const Thing &t);
54 
55  INLINE void clear();
56 
57 private:
58  Thing _array[max_size+1];
59  int _in, _out;
60 };
61 
62 #include "circBuffer.I"
63 
64 #endif
void push_back(const Thing &t)
Adds an item to the end of the buffer.
Definition: circBuffer.I:169
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.
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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