1 module hunt.imf.EvBuffer;
2 
3 import std.array;
4 import std.stdio;
5 
6 class  EvBuffer(T) {
7     this( ulong  sz = 0){
8         _buffer = new T [sz];
9         _buf_sz = 0;
10     }
11 
12 public:
13 
14     void mergeBuffer ( ref T [] buf)
15     {
16         if (buf != null)
17         {
18             this._buffer ~= buf;
19             _buf_sz += buf.length;
20         }
21     }
22 
23     bool copyOutFromHead (ref T [] buf , ref const ulong len)
24     {
25         if (_buf_sz >= len && buf != null)
26         {
27             buf[0 .. len] = _buffer [0 .. len];
28             return true;
29         } else
30         {
31             return false;
32         }
33     }
34 
35     bool drainBufferFromHead (ref const ulong len)
36     {
37         if (_buf_sz < len)
38         {
39             return false;
40         } else {
41             _buffer = _buffer[len .. $];
42             _buf_sz -= len;
43             return true;
44         }
45     }
46 
47     bool removeBufferFromHead (ref T [] buf , ref const ulong len)
48     {
49         if (_buf_sz < len)
50         {
51             return false;
52         } else {
53             buf[0 .. len] = _buffer [0 .. len];
54             _buffer = _buffer[len .. $];
55             _buf_sz -= len;
56             return true;
57         }
58     }
59 
60     void reset(){
61         _buffer = new T [0];
62         _buf_sz = 0;
63     }
64 
65     ulong getBufferLength () { return this._buf_sz ;}
66 
67     T [] getBuffer(){return _buffer;}
68 
69     void print () {
70         writeln("%s",this._buffer);
71     }
72 
73 private:
74     T []        _buffer;
75     ulong       _buf_sz;
76 }