1 #pragma once 2 3 namespace APE 4 { 5 6 class CCircleBuffer 7 { 8 public: 9 // construction / destruction 10 CCircleBuffer(); 11 virtual ~CCircleBuffer(); 12 13 // create the buffer 14 void CreateBuffer(intn nBytes, intn nMaxDirectWriteBytes); 15 16 // query 17 intn MaxAdd(); 18 intn MaxGet(); 19 20 // direct writing GetDirectWritePointer()21 __forceinline unsigned char * GetDirectWritePointer() 22 { 23 // return a pointer to the tail -- note that it will always be safe to write 24 // at least m_nMaxDirectWriteBytes since we use an end cap region 25 return &m_pBuffer[m_nTail]; 26 } 27 UpdateAfterDirectWrite(intn nBytes)28 __forceinline void UpdateAfterDirectWrite(intn nBytes) 29 { 30 // update the tail 31 m_nTail += nBytes; 32 33 // if the tail enters the "end cap" area, set the end cap and loop around 34 if (m_nTail >= (m_nTotal - m_nMaxDirectWriteBytes)) 35 { 36 m_nEndCap = m_nTail; 37 m_nTail = 0; 38 } 39 } 40 41 // get data 42 intn Get(unsigned char * pBuffer, intn nBytes); 43 44 // remove / empty 45 void Empty(); 46 intn RemoveHead(intn nBytes); 47 intn RemoveTail(intn nBytes); 48 49 private: 50 intn m_nTotal; 51 intn m_nMaxDirectWriteBytes; 52 intn m_nEndCap; 53 intn m_nHead; 54 intn m_nTail; 55 unsigned char * m_pBuffer; 56 }; 57 58 } 59