aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-09-30 17:58:21 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-09-30 18:00:04 +0200
commitfe342f77c1183ee7ef14f909005351dcdef3f162 (patch)
treeab490f84023c789341498e92d3879c71e7d4b057
parentbf92802e0252315e9753a2817d22fd885b40111f (diff)
downloadkernel-fe342f77c1183ee7ef14f909005351dcdef3f162.tar.xz
kernel-fe342f77c1183ee7ef14f909005351dcdef3f162.zip
kstd: ring_buffer: implement operator[]
-rw-r--r--libs/kstd/kstd/ring_buffer.hpp28
-rw-r--r--libs/kstd/kstd/ring_buffer.tests.cpp22
2 files changed, 45 insertions, 5 deletions
diff --git a/libs/kstd/kstd/ring_buffer.hpp b/libs/kstd/kstd/ring_buffer.hpp
index d8873b2c..b40e167b 100644
--- a/libs/kstd/kstd/ring_buffer.hpp
+++ b/libs/kstd/kstd/ring_buffer.hpp
@@ -51,7 +51,9 @@ namespace kstd
//! @name Element Access
//! @{
- //! Get the element at the specified index.
+ //! Get the element at the specified position.
+ //!
+ //! @warning This function will panic if the position is not valid for this ring buffer.
//!
//! @param position The zero-based index of the element to get.
//! @return A reference to the element at the given position.
@@ -63,6 +65,8 @@ namespace kstd
//! Get the element at the specified index.
//!
+ //! @warning This function will panic if the position is not valid for this ring buffer.
+ //!
//! @param position The zero-based index of the element to get.
//! @return A reference to the element at the given position.
[[nodiscard]] constexpr auto at(size_type position) const -> const_reference
@@ -71,6 +75,28 @@ namespace kstd
return *element_at(position);
}
+ //! Get the element at the specified position.
+ //!
+ //! @warning This function will invoke undefined behavior if the position is not valid for this ring buffer.
+ //!
+ //! @param position The zero-based index of the element to get.
+ //! @return A reference to the element at the given position.
+ [[nodiscard]] constexpr auto operator[](size_type position) noexcept -> reference
+ {
+ return *element_at(position);
+ }
+
+ //! Get the element at the specified position.
+ //!
+ //! @warning This function will invoke undefined behavior if the position is not valid for this ring buffer.
+ //!
+ //! @param position The zero-based index of the element to get.
+ //! @return A reference to the element at the given position.
+ [[nodiscard]] constexpr auto operator[](size_type position) const noexcept -> const_reference
+ {
+ return *element_at(position);
+ }
+
//! @}
//! @name Capacity
diff --git a/libs/kstd/kstd/ring_buffer.tests.cpp b/libs/kstd/kstd/ring_buffer.tests.cpp
index d2515289..bf1ae2a0 100644
--- a/libs/kstd/kstd/ring_buffer.tests.cpp
+++ b/libs/kstd/kstd/ring_buffer.tests.cpp
@@ -90,35 +90,49 @@ SCENARIO("Ring Buffer element access", "[kstd][ring_buffer]")
{
auto buffer = kstd::ring_buffer<char, 5>{3, 'a'};
- THEN("accessing the first 3 element return 'a'")
+ THEN("accessing the first 3 elements using at() returns 'a'")
{
REQUIRE(buffer.at(0) == 'a');
REQUIRE(buffer.at(1) == 'a');
REQUIRE(buffer.at(2) == 'a');
}
- THEN("accessing the fourth element panics")
+ THEN("accessing the fourth element using at() panics")
{
REQUIRE_THROWS_MATCHES(buffer.at(3), kstd::tests::os_panic,
Catch::Matchers::Message("[KSTD] Index out-of-bounds in ring_buffer element access!"));
}
+ THEN("accessing the first 3 elements using operator[] returns 'a'")
+ {
+ REQUIRE(buffer[0] == 'a');
+ REQUIRE(buffer[1] == 'a');
+ REQUIRE(buffer[2] == 'a');
+ }
+
WHEN("working through a const reference")
{
auto const & ref = buffer;
- THEN("accessing the first 3 element return 'a'")
+ THEN("accessing the first 3 elements using at() returns 'a'")
{
REQUIRE(ref.at(0) == 'a');
REQUIRE(ref.at(1) == 'a');
REQUIRE(ref.at(2) == 'a');
}
- THEN("accessing the fourth element panics")
+ THEN("accessing the fourth element using at() panics")
{
REQUIRE_THROWS_MATCHES(ref.at(3), kstd::tests::os_panic,
Catch::Matchers::Message("[KSTD] Index out-of-bounds in ring_buffer element access!"));
}
+
+ THEN("accessing the first 3 elements using operator[] returns 'a'")
+ {
+ REQUIRE(buffer[0] == 'a');
+ REQUIRE(buffer[1] == 'a');
+ REQUIRE(buffer[2] == 'a');
+ }
}
}
} \ No newline at end of file