From 21adf399c074069ef8eef22a68a238238ec17e9c Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sat, 19 Aug 2023 11:32:49 +0100 Subject: [PATCH] Ring buffer: Add a std::initializer_list constructor --- src/core/ring_buffer.hpp | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/core/ring_buffer.hpp b/src/core/ring_buffer.hpp index 241b1a4dc5..8e9265e829 100644 --- a/src/core/ring_buffer.hpp +++ b/src/core/ring_buffer.hpp @@ -201,19 +201,25 @@ public: ring_buffer() = default; + template + void construct_from(const U &other) + { + uint32 cap = round_up_size((uint32)other.size()); + this->data.reset(MallocT(cap * sizeof(T))); + this->mask = cap - 1; + this->head = 0; + this->count = (uint32)other.size(); + byte *ptr = this->data.get(); + for (const T &item : other) { + new (ptr) T(item); + ptr += sizeof(T); + } + } + ring_buffer(const ring_buffer &other) { if (!other.empty()) { - uint32 cap = round_up_size(other.count); - this->data.reset(MallocT(cap * sizeof(T))); - this->mask = cap - 1; - this->head = 0; - this->count = other.size(); - byte *ptr = this->data.get(); - for (const T &item : other) { - new (ptr) T(item); - ptr += sizeof(T); - } + this->construct_from(other); } } @@ -225,6 +231,13 @@ public: std::swap(this->mask, other.mask); } + ring_buffer(std::initializer_list init) + { + if (init.size() > 0) { + this->construct_from(init); + } + } + ring_buffer& operator =(const ring_buffer &other) { if (&other != this) {