Skip to content

Commit

Permalink
Define proper copy semantics for RingBuffer.
Browse files Browse the repository at this point in the history
Previously did not behave well when copies were made: A RingBuffer copy that got destroyed would leave the other copies with an buffer full of destroyed elements.
  • Loading branch information
s-ludwig committed Feb 13, 2024
1 parent 56d23e3 commit ae1751b
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions source/vibe/container/ringbuffer.d
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ module vibe.container.ringbuffer;
Both, FIFO and LIFO operation modes are supported, using `removeFront` and
`removeBack`.
This struct has value semantics - copying the a `RingBuffer` value will copy
the whole buffer. Copy-on-write optimization may be implemented in the future,
but generally copying is discouraged.
*/
struct RingBuffer(T, size_t N = 0, bool INITIALIZE = true) {
import std.traits : hasElaborateDestructor, isCopyable;
Expand All @@ -43,6 +47,12 @@ struct RingBuffer(T, size_t N = 0, bool INITIALIZE = true) {
/// Constructs a new rung buffer with given capacity (only if `N == 0`).
this(size_t capacity) { m_buffer = new T[capacity]; }

this(this)
{
if (m_buffer.length)
m_buffer = m_buffer.dup;
}

~this()
{
if (m_buffer.length > 0) {
Expand Down

0 comments on commit ae1751b

Please sign in to comment.