aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libs/kstd/kstd/ring_buffer.hpp9
-rw-r--r--libs/kstd/kstd/ring_buffer.tests.cpp16
2 files changed, 25 insertions, 0 deletions
diff --git a/libs/kstd/kstd/ring_buffer.hpp b/libs/kstd/kstd/ring_buffer.hpp
index 2b97e0a9..d2f0f958 100644
--- a/libs/kstd/kstd/ring_buffer.hpp
+++ b/libs/kstd/kstd/ring_buffer.hpp
@@ -184,6 +184,15 @@ namespace kstd
[&](auto const i) { m_storage.construct(i, other[i]); });
}
+ //! Construct a ring buffer by moving from an existing one.
+ constexpr ring_buffer(ring_buffer && other)
+ : m_size{other.m_size}
+ , m_read_index{}
+ {
+ std::ranges::for_each(std::views::iota(0uz, other.size()),
+ [&](auto const i) { m_storage.construct(i, std::move(other[i])); });
+ }
+
//! Destroy this ring buffer.
constexpr ~ring_buffer() noexcept
{
diff --git a/libs/kstd/kstd/ring_buffer.tests.cpp b/libs/kstd/kstd/ring_buffer.tests.cpp
index 4e34249b..537ed27e 100644
--- a/libs/kstd/kstd/ring_buffer.tests.cpp
+++ b/libs/kstd/kstd/ring_buffer.tests.cpp
@@ -329,6 +329,22 @@ SCENARIO("Ring Buffer initialization and construction", "[kstd][ring_buffer]")
REQUIRE_FALSE(std::ranges::equal(buffer, copy, std::equal_to{}, address_of, address_of));
}
}
+
+ WHEN("moving from it")
+ {
+ auto copy = buffer;
+ auto moved = std::move(buffer);
+
+ THEN("the new vector has size 3")
+ {
+ REQUIRE(moved.size() == 3);
+ }
+
+ THEN("the new buffer contains the moved values")
+ {
+ REQUIRE(std::ranges::equal(moved, copy));
+ }
+ }
}
}