From 8a01eaa7d27216b91178212412ed1f483a2af2d7 Mon Sep 17 00:00:00 2001 From: John Lindgren Date: Thu, 30 Nov 2023 18:59:08 -0500 Subject: [PATCH] ringbuf: Add fill_with() --- src/libaudcore/ringbuf.h | 9 +++++++++ src/libaudcore/tests/test.cc | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/libaudcore/ringbuf.h b/src/libaudcore/ringbuf.h index 7908670d8..b470f9047 100644 --- a/src/libaudcore/ringbuf.h +++ b/src/libaudcore/ringbuf.h @@ -169,6 +169,15 @@ class RingBuf : private RingBufBase return val; } + template + void fill_with(Args &&... args) + { + discard(); + add(raw(size())); + for (int i = 0; i < len(); i++) + aud::construct::make(at(raw(i)), std::forward(args)...); + } + private: static constexpr int raw(int len) { return len * sizeof(T); } static constexpr int cooked(int len) { return len / sizeof(T); } diff --git a/src/libaudcore/tests/test.cc b/src/libaudcore/tests/test.cc index 884e18238..dfdcd2cc0 100644 --- a/src/libaudcore/tests/test.cc +++ b/src/libaudcore/tests/test.cc @@ -479,6 +479,16 @@ static void test_ringbuf() ring.discard(5); assert(ring.len() == 5); + ring.fill_with("fill"); + + assert(ring.size() == 10); + assert(ring.len() == 10); + assert(ring.linear() == 10); + assert(ring.space() == 0); + + for (int i = 0; i < 10; i++) + assert(ring[i] == String("fill")); + ring.discard(); assert(ring.len() == 0);