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 )
34 // constructor for queues based on ring buffers
35 // create the first link
37 lastFree = new link<T>( initialvalue );
38 lastFilled = lastFree;
39 // make value point to itself
40 lastFilled->nextLink = lastFilled;
41 lastFilled->prevLink = lastFilled;
42 // now add the remainder of the elements
45 link<T> * newLink = new link<T>( initialvalue );
46 newLink->prevLink = lastFilled;
47 newLink->nextLink = lastFilled->nextLink;
48 lastFilled->nextLink->prevLink = newLink;
49 lastFilled->nextLink = newLink;
54 queueRingBuffer<T>::~queueRingBuffer()
56 // delete all memory associated with ring buffer
57 link<T> * p = lastFree;
60 // walk around the circle deleting nodes
61 while( p->nextLink != lastFree )
70 T& queueRingBuffer<T>::dequeue()
72 // remove element form front of queue
73 // advance last free position
74 lastFree = lastFree->nextLink;
76 // return value stored in last free position
77 return lastFree->value;
81 T& queueRingBuffer<T>::current()
83 // return value stored in current
84 return lastFree->nextLink->value;
88 void queueRingBuffer<T>::enqueue( const T &val )
90 // add new element to end of queue buffer
91 // first check for potential overflow
92 if( lastFilled->nextLink == lastFree )
94 // eDebug("increase size %d", count);
95 link<T> * newLink = new link<T>( val );
96 newLink->prevLink = lastFilled;
97 newLink->nextLink = lastFilled->nextLink;
98 lastFilled->nextLink->prevLink = newLink;
99 lastFilled->nextLink = newLink;
103 // simply advance the last filled pointer
104 lastFilled = lastFilled->nextLink;
105 lastFilled->value = val;