2 * Copyright 2008-2013, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
5 #ifndef CIRCULAR_BUFFER_H
6 #define CIRCULAR_BUFFER_H
14 template<typename Type
>
15 class CircularBuffer
{
17 CircularBuffer(size_t size
)
30 status_t
InitCheck() const
32 return fBuffer
!= NULL
? B_OK
: B_NO_MEMORY
;
35 status_t
SetSize(size_t size
)
43 fBuffer
= (Type
*)malloc(fSize
* sizeof(Type
));
44 if (fBuffer
== NULL
) {
63 int32
CountItems() const
68 Type
* ItemAt(int32 index
) const
70 if (index
>= (int32
)fIn
|| index
< 0 || fBuffer
== NULL
)
73 return &fBuffer
[(fFirst
+ index
) % fSize
];
76 void AddItem(const Type
& item
)
80 index
= fFirst
+ fIn
++;
85 fBuffer
[index
% fSize
] = item
;
101 #endif // CIRCULAR_BUFFER_H