00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Andrew Clinton 00008 * Side Effects Software Inc 00009 * 477 Richmond Street West 00010 * Toronto, Ontario 00011 * Canada M5V 3E7 00012 * 416-504-9876 00013 * 00014 * NAME: UT_RingBuffer.h ( UT Library, C++) 00015 * 00016 * COMMENTS: 00017 */ 00018 00019 #ifndef __UT_RingBuffer__ 00020 #define __UT_RingBuffer__ 00021 00022 #include "UT_API.h" 00023 #include "UT_RingBufferRaw.h" 00024 00025 /// A UT_RingBuffer is an indexable queue. The buffer uses indices 00026 /// that represent the insertion order of items in the buffer. The 00027 /// buffer may contain "holes", which have no associated data but 00028 /// still have an order number. 00029 template <class utPtr> 00030 class UT_RingBuffer { 00031 public: 00032 UT_RingBuffer() {} 00033 UT_RingBuffer(int capacity) : myBuf(capacity) {} 00034 ~UT_RingBuffer() {} 00035 00036 /// Null-padded insertion. If the id of the incoming object is 00037 /// greater than the next order number that would be assigned 00038 /// by push(), holes are created in the buffer. 00039 void insert(int id, utPtr data) { myBuf.insert(id, (void *)data); } 00040 void remove(int id) { myBuf.remove(id); } 00041 00042 /// Pop the first element off the head of the list, moving the first entry 00043 /// to the next element. 00044 utPtr popFirst() 00045 { 00046 utPtr data = front(); 00047 pop(); 00048 return data; 00049 } 00050 00051 /// Insert the data into the buffer, and return the order number that 00052 /// it is assigned. 00053 int push(utPtr data) { return myBuf.push((void *)data); } 00054 00055 /// Pop a single element from the start of the buffer 00056 void pop() { myBuf.pop(); } 00057 00058 /// Pop the next "number" elements from the buffer 00059 void pop(int number) { myBuf.pop(number); } 00060 00061 int entries() const { return myBuf.entries(); } 00062 int peakUsage() const { return myBuf.peakUsage(); } 00063 00064 /// operator [] is the bounds checked version of the operator. The 00065 /// id is the order number of the object being indexed. 00066 utPtr operator[](int id) const { return (utPtr)myBuf[id]; } 00067 utPtr operator()(int id) const { return (utPtr)myBuf(id); } 00068 00069 /// In order to iterate over the contents of the ring buffer 00070 /// for (i = rbuf.frontId(); i <= rbuf.backId(); i++) 00071 /// element = rbuf(i); 00072 /// The backId is *inclusive*!!! There shouldn't be holes in the array. 00073 00074 utPtr front() const { return (utPtr)myBuf.front(); } 00075 int frontId() const { return myBuf.frontId(); } 00076 utPtr back() const { return (utPtr)myBuf.back(); } 00077 int backId() const { return myBuf.backId(); } 00078 00079 void display() const { myBuf.display(); } 00080 00081 private: 00082 UT_RingBufferRaw myBuf; 00083 }; 00084 00085 #endif 00086
1.5.9