File: CircleBuffer.h

package info (click to toggle)
libjmac-java 1.74-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,780 kB
  • sloc: java: 9,279; cpp: 4,375; xml: 369; makefile: 31; sh: 12
file content (59 lines) | stat: -rw-r--r-- 1,378 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef APE_CIRCLEBUFFER_H
#define APE_CIRCLEBUFFER_H

class CCircleBuffer  
{
public:

    // construction / destruction
    CCircleBuffer();
    virtual ~CCircleBuffer();

    // create the buffer
    void CreateBuffer(int nBytes, int nMaxDirectWriteBytes);

    // query
    int MaxAdd();
    int MaxGet();

    // direct writing
    inline unsigned char * CCircleBuffer::GetDirectWritePointer()
    {
        // return a pointer to the tail -- note that it will always be safe to write
        // at least m_nMaxDirectWriteBytes since we use an end cap region
        return &m_pBuffer[m_nTail];
    }

    inline void CCircleBuffer::UpdateAfterDirectWrite(int nBytes)
    {
        // update the tail
        m_nTail += nBytes;

        // if the tail enters the "end cap" area, set the end cap and loop around
        if (m_nTail >= (m_nTotal - m_nMaxDirectWriteBytes))
        {
            m_nEndCap = m_nTail;
            m_nTail = 0;
        }
    }

    // get data
    int Get(unsigned char * pBuffer, int nBytes);

    // remove / empty
    void Empty();
    int RemoveHead(int nBytes);
    int RemoveTail(int nBytes);

private:

    int m_nTotal;
    int m_nMaxDirectWriteBytes;
    int m_nEndCap;
    int m_nHead;
    int m_nTail;
    unsigned char * m_pBuffer;
};


#endif // #ifndef APE_CIRCLEBUFFER_H