]> git.donarmstrong.com Git - bamtools.git/blob - src/api/internal/io/RollingBuffer_p.h
Updated file headers (filename, license, description, etc)
[bamtools.git] / src / api / internal / io / RollingBuffer_p.h
1 // ***************************************************************************
2 // RollingBuffer_p.h (c) 2011 Derek Barnett
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 10 November 2011 (DB)
6 // ---------------------------------------------------------------------------
7 // Provides a dynamic I/O FIFO byte queue, which removes bytes as they are
8 // read from the front of the buffer and grows to accept bytes being written
9 // to buffer end.
10 //
11 // implementation note: basically a 'smart' wrapper around 1..* ByteArrays
12 // ***************************************************************************
13
14 #ifndef ROLLINGBUFFER_P_H
15 #define ROLLINGBUFFER_P_H
16
17 //  -------------
18 //  W A R N I N G
19 //  -------------
20 //
21 // This file is not part of the BamTools API.  It exists purely as an
22 // implementation detail. This header file may change from version to version
23 // without notice, or even be removed.
24 //
25 // We mean it.
26
27 #include "api/api_global.h"
28 #include "api/internal/io/ByteArray_p.h"
29 #include <deque>
30 #include <string>
31
32 namespace BamTools {
33 namespace Internal {
34
35 class RollingBuffer {
36
37     // ctors & dtor
38     public:
39         RollingBuffer(size_t growth);
40         ~RollingBuffer(void);
41
42     // RollingBuffer interface
43     public:
44
45         // returns current buffer size
46         size_t BlockSize(void) const;
47         // checks buffer for new line
48         bool CanReadLine(void) const;
49         // frees @n bytes from end of buffer
50         void Chop(size_t n);
51         // clears entire buffer structure
52         void Clear(void);
53         // frees @n bytes from front of buffer
54         void Free(size_t n);
55         // checks buffer for @c
56         size_t IndexOf(char c) const;
57         // returns whether buffer contains data
58         bool IsEmpty(void) const;
59         // reads up to @maxLen bytes into @dest
60         // returns exactly how many bytes were read from buffer
61         size_t Read(char* dest, size_t max);
62         // reads until newline (or up to @maxLen bytes)
63         // returns exactly how many bytes were read from buffer
64         size_t ReadLine(char* dest, size_t max);
65
66         const char* ReadPointer(void) const;   // returns a C-fxn compatible char* to byte data
67         char* Reserve(size_t n);               // ensures that buffer contains space for @n incoming bytes, returns write-able char*
68         size_t Size(void) const;               // returns current number of bytes stored in buffer
69         void Write(const char* src, size_t n); // reserves space for @n bytes, then appends contents of @src to buffer
70
71     // data members
72     private:
73         size_t m_head;                // index into current data (next char)
74         size_t m_tail;                // index into last data position
75         size_t m_tailBufferIndex;     // m_data::size() - 1
76         size_t m_totalBufferSize;     // total buffer size
77         size_t m_bufferGrowth;        // new buffers are typically initialized with this size
78         std::deque<ByteArray> m_data; // basic 'buffer of buffers'
79 };
80
81 } // namespace Internal
82 } // namespace BamTools
83
84 #endif // ROLLINGBUFFER_P_H