From 636e1990c72b89210fad1c323819799702d5a6f9 Mon Sep 17 00:00:00 2001 From: Jeff Ithier Date: Thu, 7 Nov 2024 12:33:31 +0100 Subject: [PATCH] [#490] Make const MutableSlice also prevent mutation of elements --- iceoryx2-ffi/cxx/include/iox/slice.hpp | 20 ++++++++++---------- iceoryx2-ffi/cxx/tests/src/slice_tests.cpp | 9 +++++---- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/iceoryx2-ffi/cxx/include/iox/slice.hpp b/iceoryx2-ffi/cxx/include/iox/slice.hpp index 217d29dda..7edd953fe 100644 --- a/iceoryx2-ffi/cxx/include/iox/slice.hpp +++ b/iceoryx2-ffi/cxx/include/iox/slice.hpp @@ -29,7 +29,8 @@ namespace iox { template class Slice { public: - using Iterator = std::conditional_t, const T*, T*>; + using Iterator = T*; + using ConstIterator = const T*; using ValueType = std::remove_const_t; /// @brief Constructs a Slice object. @@ -49,7 +50,7 @@ class Slice { /// @param[in] n The index of the element to access. /// @return A const reference to the element at the specified index. /// @pre The index must be less than the number of elements in the slice. - auto operator[](uint64_t n) const -> std::conditional_t, const ValueType&, ValueType&>; + auto operator[](uint64_t n) const -> const ValueType&; /// @brief Accesses the element at the specified index (non-const version). /// @param[in] n The index of the element to access. @@ -59,7 +60,7 @@ class Slice { /// @brief Returns an iterator to the beginning of the slice (const version). /// @return An iterator pointing to the first element of the slice. - auto begin() const -> Iterator; + auto begin() const -> ConstIterator; /// @brief Returns an iterator to the beginning of the slice (non-const version). /// @return An iterator pointing to the first element of the slice. @@ -67,7 +68,7 @@ class Slice { /// @brief Returns an iterator to the end of the slice (const version). /// @return An iterator pointing one past the last element of the slice. - auto end() const -> Iterator; + auto end() const -> ConstIterator; /// @brief Returns an iterator to the end of the slice (non-const version). /// @return An iterator pointing one past the last element of the slice. @@ -75,7 +76,7 @@ class Slice { /// @brief Returns a pointer to the underlying data of the slice (const version). /// @return A pointer to the first element of the slice. - auto data() const -> Iterator; + auto data() const -> ConstIterator; /// @brief Returns a pointer to the underlying data of the slice (non-const version). /// @return A pointer to the first element of the slice. @@ -110,8 +111,7 @@ auto Slice::number_of_elements() const -> uint64_t { } template -auto Slice::operator[](const uint64_t n) const - -> std::conditional_t, const ValueType&, ValueType&> { +auto Slice::operator[](const uint64_t n) const -> const ValueType& { IOX_ASSERT(n < m_number_of_elements, "Index out of bounds"); return *(m_data + n); } @@ -128,7 +128,7 @@ auto Slice::begin() -> Iterator { } template -auto Slice::begin() const -> Iterator { +auto Slice::begin() const -> ConstIterator { return m_data; } @@ -138,7 +138,7 @@ auto Slice::end() -> Iterator { } template -auto Slice::end() const -> Iterator { +auto Slice::end() const -> ConstIterator { return m_data + m_number_of_elements; } @@ -148,7 +148,7 @@ auto Slice::data() -> Iterator { } template -auto Slice::data() const -> Iterator { +auto Slice::data() const -> ConstIterator { return m_data; } diff --git a/iceoryx2-ffi/cxx/tests/src/slice_tests.cpp b/iceoryx2-ffi/cxx/tests/src/slice_tests.cpp index 9c0cbf95d..d9ff1aeb2 100644 --- a/iceoryx2-ffi/cxx/tests/src/slice_tests.cpp +++ b/iceoryx2-ffi/cxx/tests/src/slice_tests.cpp @@ -35,11 +35,12 @@ TEST(SliceTest, const_correctness_is_maintained) { ASSERT_FALSE(std::is_const_v>); ASSERT_FALSE(std::is_const_v>); + // const instances of MutableSlice are also not mutable const auto const_mutable_slice = iox::MutableSlice(elements.data(), SLICE_MAX_LENGTH); - ASSERT_FALSE(std::is_const_v>); - ASSERT_FALSE(std::is_const_v>); - ASSERT_FALSE(std::is_const_v>); - ASSERT_FALSE(std::is_const_v>); + ASSERT_TRUE(std::is_const_v>); + ASSERT_TRUE(std::is_const_v>); + ASSERT_TRUE(std::is_const_v>); + ASSERT_TRUE(std::is_const_v>); auto immutable_slice = iox::ImmutableSlice(elements.data(), SLICE_MAX_LENGTH); ASSERT_TRUE(std::is_const_v>);