jnk0le / Ring-Buffer

simple C++11 ring buffer implementation, allocated and evaluated at compile time
MIT License
380 stars 64 forks source link

Add template parameter to make it a overwriting ring buffer #9

Open jrahlf opened 3 years ago

jrahlf commented 3 years ago

Hi,

how about adding a template parameter which makes it an overwriting ring buffer? I.e. when buffer is full, calling insert() replaces the oldest value with the new value. Then the tail also has to be updated, which means the ring buffer cannot be lock free anymore.

I really like your implementation and this addition would make it even more generalized.

jnk0le commented 3 years ago

Overwiriting insert is wanted function especially in atomic context. Probably as separate function instead of overloading. I'll definitely add it in mpmc, but non atomic overloads of insert is going to be a lot of burden now.

It is still possible to inherit from the spsc class and add followinfg function

//non atomic, returns true if overwritten
bool insertOverwrite(T data)
{
    if(!insert(data)) {
        remove();
        insert(data);
        return true;
    }
    return false;
}

not the most efficient, but should work.