-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringbuf.h
71 lines (58 loc) · 1.74 KB
/
ringbuf.h
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
62
63
64
65
66
67
68
69
70
71
// Ringbuf classes
// A ring buffer and associated classes that are mostly in the style
// of the C++ Standard Template Library
//
// This class has a provision to install a mutex. To do so, override the
// provided methods increment_size() and decrement_size(). These functions
// are the only places where a "thread unsafe" condition can occur.
#ifndef __RINGBUF_H_
#define __RINGBUF_H_
// Some exceptions for use with this class
class RingbufException
{
};
class RingbufEmptyException
: public RingbufException
{
};
class RingbufFullException
: public RingbufException
{
};
// Class Ringbuf
template<typename _T>
class Ringbuf
{
public:
Ringbuf (size_t capacity);
virtual ~Ringbuf ();
int ipushback (const _T&); // 0 for success, 1 for failure.
int ipop (_T&); // 0 for success, 1 for failure.
size_t size(void) const;
protected:
// Called when putting something into the ring buffer. To install eg a
// mutex, override this and include your mutex logic.
virtual void increment_size (void)
{ ++_size; }
// Called when taking something out of the ring buffer. To install eg a
// mutex, override this and include your mutex logic.
virtual void decrement_size (void)
{ --_size; }
private:
Ringbuf(); // No default constructor
Ringbuf(const Ringbuf<_T>&); // No copy constructor
size_t _size;
size_t _capacity;
_T* _push_next;
_T* _pop_next;
_T* _ring_start;
_T* _ring_end;
};
// Helper functions
template<typename _T>
Ringbuf<_T>& operator>> (Ringbuf<_T>&, _T&) throw (RingbufEmptyException);
template<typename _T>
Ringbuf<_T>& operator<< (Ringbuf<_T>&, const _T&) throw (RingbufFullException);
// Implementation
#include "ringbuf.tcc"
#endif // __RINGBUF_H_