aboutsummaryrefslogtreecommitdiff
path: root/libs/kstd
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-09-30 15:35:29 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-09-30 15:35:29 +0200
commitd8b4ae2ddffa7ff9613d7a6416f3a405d81dad43 (patch)
treee58904b57b379ee790c9eb417eeb67323ee23718 /libs/kstd
parent1c9f678aa0496721317698c2c6aef559af0495c9 (diff)
downloadkernel-d8b4ae2ddffa7ff9613d7a6416f3a405d81dad43.tar.xz
kernel-d8b4ae2ddffa7ff9613d7a6416f3a405d81dad43.zip
kstd: ring_buffer: implement empty()
Diffstat (limited to 'libs/kstd')
-rw-r--r--libs/kstd/kstd/ring_buffer.hpp13
-rw-r--r--libs/kstd/kstd/ring_buffer.tests.cpp5
2 files changed, 18 insertions, 0 deletions
diff --git a/libs/kstd/kstd/ring_buffer.hpp b/libs/kstd/kstd/ring_buffer.hpp
index 2949988e..a25e37b7 100644
--- a/libs/kstd/kstd/ring_buffer.hpp
+++ b/libs/kstd/kstd/ring_buffer.hpp
@@ -26,6 +26,14 @@ namespace kstd
return Capacity;
}
+ //! Check if this ring buffer is empty.
+ //!
+ //! @return @p true iff. this ring buffer is empty, @p false otherwise.
+ [[nodiscard]] constexpr auto empty() const noexcept -> bool
+ {
+ return m_head == m_tail;
+ }
+
//! Get the maximum number of elements this ring buffer can hold.
//!
//! Due to ring_buffer being a fixed-size container, this function will return the same value as capacity. This
@@ -39,6 +47,11 @@ namespace kstd
//! @}
+ private:
+ //! The index of the element that was most recently added to this ring buffer.
+ size_type m_head{};
+ //! The index of the element that was least recently added to this ring buffer.
+ size_type m_tail{};
};
} // namespace kstd
diff --git a/libs/kstd/kstd/ring_buffer.tests.cpp b/libs/kstd/kstd/ring_buffer.tests.cpp
index f693b0a8..37243014 100644
--- a/libs/kstd/kstd/ring_buffer.tests.cpp
+++ b/libs/kstd/kstd/ring_buffer.tests.cpp
@@ -15,6 +15,11 @@ SCENARIO("Ring Buffer initialization and construction", "[ring_buffer]")
REQUIRE(buffer.capacity() == 5);
}
+ THEN("the buffer is empty")
+ {
+ REQUIRE(buffer.empty());
+ }
+
THEN("the maximum size is equal to the capacity")
{
REQUIRE(buffer.max_size() == buffer.capacity());