00001 //=========================================================================== 00002 //Copyright (C) 2003, 2004 Zentaro Kavanagh 00003 // 00004 //Redistribution and use in source and binary forms, with or without 00005 //modification, are permitted provided that the following conditions 00006 //are met: 00007 // 00008 //- Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // 00011 //- Redistributions in binary form must reproduce the above copyright 00012 // notice, this list of conditions and the following disclaimer in the 00013 // documentation and/or other materials provided with the distribution. 00014 // 00015 //- Neither the name of Zentaro Kavanagh nor the names of contributors 00016 // may be used to endorse or promote products derived from this software 00017 // without specific prior written permission. 00018 // 00019 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00020 //``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00021 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00022 //PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ORGANISATION OR 00023 //CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00024 //EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00025 //PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00026 //PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00027 //LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00028 //NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 //=========================================================================== 00031 #pragma once 00032 #include <libOOOgg/dllstuff.h> 00033 #include <libOOOgg/IFIFOBuffer.h> 00034 00035 00036 //Empty Buffer 00037 //============== 00038 // 00039 // <--------------- Buffer Size --------------------> 00040 // 00041 // 0123456789 123456789 123456789 123456789 123456789* 00042 // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 00043 // R 00044 // W 00045 // 00046 // 00047 // 0123456789 123456789 123456789 123456789 123456789* 00048 // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 00049 // R 00050 // W 00051 // 00052 //When R = W Buffer is empty 00053 // 00054 // when R = W: available bytes = 0 00055 // when R = W: space left = buffer size 00057 00058 //Full Buffer 00059 //=========== 00060 // 00061 // 00062 // 00063 // <--------------- Buffer Size --------------------> 00064 // 00065 // 0123456789 123456789 123456789 123456789 123456789* 00066 // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 00067 // R 00068 // W 00069 // 00070 // 00071 // <--------------- Buffer Size --------------------> 00072 // 00073 // 0123456789 123456789 123456789 123456789 123456789* 00074 // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 00075 // R 00076 // W 00077 // 00078 //Buffer is full when R = (W + 1) MOD (bufferSize + 1) 00079 // 00080 // when R = (W + 1) MOD (bufferSize + 1): available bytes = buffer size 00081 // when R = (W + 1) MOD (bufferSize + 1): space left = 0 00082 // 00083 // 00084 // 00085 // 00087 00088 //Partial Buffers 00089 //=============== 00090 // 00091 //Case 1 00092 //====== 00093 // 00094 // 00095 // <--------------- Buffer Size --------------------> 00096 // 00097 // 0123456789 123456789 123456789 123456789 123456789* 00098 // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 00099 // R 00100 // W 00101 // 00102 // 00103 // when W > R: available bytes = W - R 00104 // when W > R: space left = buffer size - available bytes = buffer size + R - W 00105 // 00106 // 00107 //Case 2 00108 //====== 00109 // 00110 // 00111 // <--------------- Buffer Size --------------------> 00112 // 1 2 3 4 00113 // 0123456789 123456789 123456789 123456789 123456789* 00114 // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 00115 // R 00116 // W 00117 // 00118 // when R > W: available bytes = buffer size + 1 - R + W 00119 // when R > W: space left = buffer size - available bytes = R - W - 1 00120 // 00121 // 00122 // 00123 // 00124 // 00125 00126 class LIBOOOGG_API CircularBuffer 00127 : public IFIFOBuffer 00128 { 00129 public: 00131 CircularBuffer(unsigned long inBufferSize); 00132 virtual ~CircularBuffer(void); 00133 00135 virtual unsigned long read(unsigned char* outData, unsigned long inBytesToRead); 00136 00138 virtual unsigned long write(const unsigned char* inData, unsigned long inBytesToWrite); 00139 00141 virtual unsigned long numBytesAvail(); 00142 00144 virtual unsigned long spaceLeft(); 00145 00147 virtual void reset(); 00148 00149 protected: 00150 unsigned long mBufferSize; 00151 unsigned long mReadPtr; 00152 unsigned long mWritePtr; 00153 00154 void bufASSERT(bool inBool) { if (!inBool) throw 0; }; 00155 unsigned char* mBuffer; 00156 00157 private: 00158 CircularBuffer& operator=(const CircularBuffer& other); /* Don't assign me */ 00159 CircularBuffer(const CircularBuffer& other); /* Don't copy me */ 00160 };