-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringbuf.tcc
62 lines (54 loc) · 1.26 KB
/
ringbuf.tcc
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
60
61
// Implementation of Ringbuf
template<typename _T>
Ringbuf<_T>::Ringbuf (size_t capacity)
: _size(0), _capacity(capacity)
{
_ring_start = new _T[capacity];
_ring_end = _ring_start + capacity;
_push_next = _ring_start;
_pop_next = _ring_start;
}
template<typename _T>
Ringbuf<_T>::~Ringbuf()
{
delete[] _ring_start;
}
template<typename _T>
size_t Ringbuf<_T>::size (void) const
{
return _size;
}
template<typename _T>
int Ringbuf<_T>::ipushback (const _T& value)
{
if (_size == _capacity) return 1;
*_push_next = value;
if (++_push_next == _ring_end) _push_next = _ring_start;
increment_size();
return 0;
}
template<typename _T>
int Ringbuf<_T>::ipop (_T& value)
{
if (_size == 0) return 1;
value = *_pop_next;
if (++_pop_next == _ring_end) _pop_next = _ring_start;
decrement_size();
return 0;
}
template<typename _T>
Ringbuf<_T>& operator>> (Ringbuf<_T>& rbuf, _T& value)
throw (RingbufEmptyException)
{
if (rbuf.ipop (value) != 0)
throw RingbufEmptyException();
return rbuf;
}
template<typename _T>
Ringbuf<_T>& operator<< (Ringbuf<_T>& rbuf, const _T& value)
throw (RingbufFullException)
{
if (rbuf.ipushback (value) != 0)
throw RingbufFullException();
return rbuf;
}