Menu

CircularBuffer

Perrotti

CircularBuffer Class

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();
};

How to use

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.


boolean begin(byte items, byte itemSize);

Alloc memory to buffer. Call before any other method.

  • items: Maximum amount of items to be stored.
  • itemSize: Size in bytes of each item.
  • return:
    • true: No errors;
    • false: Insufficient memory.

boolean addItem(void* item);

Add item at the end of buffer.

  • item: reference to item. Item will be copied to buffer.
  • return:
    • true if item was added;
    • false if buffer is full.

byte * getFirst (byte * item);

Remove and return the first item.

  • item: reference to destination memory block to contain an item. The item will be copied to it.
  • return: reference to destination block

byte * getLast (byte * item);

Remove and return the last item.

  • item: reference to destination memory block to contain an item. The item will be copied to it.
  • return: reference to destination block

void clear();

Clear the buffer.


byte itemsCount();

Return current amount of items in buffer. The counter is increased by addItem() and decreased by getFirst() and getLast().

  • return: current amount of items in buffer.

byte* checkFirst();

Return a reference to first item added. This method dont modify the buffer.

  • return: reference to first item added.

byte* checkLast();

Return a reference to last item added. This method dont modify the buffer.

  • return: reference to last item added.

Related

Wiki: Home

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.