1 #ifndef QueueRingBufferH
2 #define QueueRingBufferH
23 queueRingBuffer( unsigned int max );
25 int size() { return count; }
26 T& queueRingBuffer::dequeue();
27 T& queueRingBuffer::current();
28 void queueRingBuffer::enqueue( const T &val );
32 queueRingBuffer<T>::queueRingBuffer( unsigned int max )
35 // constructor for queues based on ring buffers
36 // create the first link
38 lastFree = new link<T>( initialvalue );
39 lastFilled = lastFree;
40 // make value point to itself
41 lastFilled->nextLink = lastFilled;
42 lastFilled->prevLink = lastFilled;
43 // now add the remainder of the elements
46 link<T> * newLink = new link<T>( initialvalue );
47 newLink->prevLink = lastFilled;
48 newLink->nextLink = lastFilled->nextLink;
49 lastFilled->nextLink->prevLink = newLink;
50 lastFilled->nextLink = newLink;
55 queueRingBuffer<T>::~queueRingBuffer()
57 // delete all memory associated with ring buffer
58 link<T> * p = lastFree;
61 // walk around the circle deleting nodes
62 while( p->nextLink != lastFree )
71 T& queueRingBuffer<T>::dequeue()
73 // remove element form front of queue
74 // advance last free position
75 lastFree = lastFree->nextLink;
77 // return value stored in last free position
78 return lastFree->value;
82 T& queueRingBuffer<T>::current()
84 // return value stored in current
85 return lastFree->nextLink->value;
89 void queueRingBuffer<T>::enqueue( const T &val )
91 // add new element to end of queue buffer
92 // first check for potential overflow
93 if( lastFilled->nextLink == lastFree )
95 // eDebug("increase size %d", count);
96 link<T> * newLink = new link<T>( val );
97 newLink->prevLink = lastFilled;
98 newLink->nextLink = lastFilled->nextLink;
99 lastFilled->nextLink->prevLink = newLink;
100 lastFilled->nextLink = newLink;
104 // simply advance the last filled pointer
105 lastFilled = lastFilled->nextLink;
106 lastFilled->value = val;