Implements a dynamically allocated circular buffer.
Can be used as a circular list (first in, first out) or as stack (last in, first out).
Public declaration:
class CircularBuffer{ public: CircularBuffer(){}; ~CircularBuffer(); boolean begin(byte items, byte itemSize); boolean addItem(void* item); void* getFirst(void* item); void* getLast(void* item); void clear(); byte itemsCount(); byte* checkFirst(); byte* checkLast(); };
CircularBuffer class can be used like a circular list (first in, first out) or as stack (last in, first out). The method addItem() always put new item at the end of buffer. To use like a circular list, retrieve items calling getFirst() method. To use as stack retrieve items calling getLast(). In both cases item is marked as free position on buffer.
Call begin() informing how many items will be stored at maximum in buffer and the size of each item.
Check begin() return to make sure that the buffer was successfully allocated.
Add itens calling addItem() informing item's reference. Item will be copied to buffer, so you dont need to keep the reference.
If buffer is full, addItem() returns false and nothing is done.
To retrive the first item added, call getFirst() informing destination reference. The first item will be copied to destination and reference returned.
If then buffer is empty, getFirst() returns NULL and nothing is done. Once retrieved, the item is marked as free position on buffer.
To retrive the last item added, call getLast().
Call clear() to empty buffer.
ItemsCount() returns current amount of items. The counter is increased by addItem() and decreased by getFirst() and getLast().
Call checkFirst() or checkLast() to get a reference to first/last item added.
Alloc memory to buffer. Call before any other method.
Add item at the end of buffer.
Remove and return the first item.
Remove and return the last item.
Clear the buffer.
Return current amount of items in buffer. The counter is increased by addItem() and decreased by getFirst() and getLast().
Return a reference to first item added. This method dont modify the buffer.
Return a reference to last item added. This method dont modify the buffer.