Skip to content

Commit

Permalink
[eclipse-iceoryx#490] Make const MutableSlice also prevent mutation o…
Browse files Browse the repository at this point in the history
…f elements
  • Loading branch information
orecham committed Nov 7, 2024
1 parent b353e0a commit 636e199
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
20 changes: 10 additions & 10 deletions iceoryx2-ffi/cxx/include/iox/slice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ namespace iox {
template <typename T>
class Slice {
public:
using Iterator = std::conditional_t<std::is_const_v<T>, const T*, T*>;
using Iterator = T*;
using ConstIterator = const T*;
using ValueType = std::remove_const_t<T>;

/// @brief Constructs a Slice object.
Expand All @@ -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<std::is_const_v<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.
Expand All @@ -59,23 +60,23 @@ 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.
auto begin() -> Iterator;

/// @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.
auto end() -> Iterator;

/// @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.
Expand Down Expand Up @@ -110,8 +111,7 @@ auto Slice<T>::number_of_elements() const -> uint64_t {
}

template <typename T>
auto Slice<T>::operator[](const uint64_t n) const
-> std::conditional_t<std::is_const_v<T>, const ValueType&, ValueType&> {
auto Slice<T>::operator[](const uint64_t n) const -> const ValueType& {
IOX_ASSERT(n < m_number_of_elements, "Index out of bounds");
return *(m_data + n);
}
Expand All @@ -128,7 +128,7 @@ auto Slice<T>::begin() -> Iterator {
}

template <typename T>
auto Slice<T>::begin() const -> Iterator {
auto Slice<T>::begin() const -> ConstIterator {
return m_data;
}

Expand All @@ -138,7 +138,7 @@ auto Slice<T>::end() -> Iterator {
}

template <typename T>
auto Slice<T>::end() const -> Iterator {
auto Slice<T>::end() const -> ConstIterator {
return m_data + m_number_of_elements;
}

Expand All @@ -148,7 +148,7 @@ auto Slice<T>::data() -> Iterator {
}

template <typename T>
auto Slice<T>::data() const -> Iterator {
auto Slice<T>::data() const -> ConstIterator {
return m_data;
}

Expand Down
9 changes: 5 additions & 4 deletions iceoryx2-ffi/cxx/tests/src/slice_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ TEST(SliceTest, const_correctness_is_maintained) {
ASSERT_FALSE(std::is_const_v<std::remove_pointer_t<decltype(mutable_slice.data())>>);
ASSERT_FALSE(std::is_const_v<std::remove_reference_t<decltype(mutable_slice[0])>>);

// const instances of MutableSlice are also not mutable
const auto const_mutable_slice = iox::MutableSlice<DummyData>(elements.data(), SLICE_MAX_LENGTH);
ASSERT_FALSE(std::is_const_v<std::remove_pointer_t<decltype(const_mutable_slice.begin())>>);
ASSERT_FALSE(std::is_const_v<std::remove_pointer_t<decltype(const_mutable_slice.end())>>);
ASSERT_FALSE(std::is_const_v<std::remove_pointer_t<decltype(const_mutable_slice.data())>>);
ASSERT_FALSE(std::is_const_v<std::remove_reference_t<decltype(const_mutable_slice[0])>>);
ASSERT_TRUE(std::is_const_v<std::remove_pointer_t<decltype(const_mutable_slice.begin())>>);
ASSERT_TRUE(std::is_const_v<std::remove_pointer_t<decltype(const_mutable_slice.end())>>);
ASSERT_TRUE(std::is_const_v<std::remove_pointer_t<decltype(const_mutable_slice.data())>>);
ASSERT_TRUE(std::is_const_v<std::remove_reference_t<decltype(const_mutable_slice[0])>>);

auto immutable_slice = iox::ImmutableSlice<DummyData>(elements.data(), SLICE_MAX_LENGTH);
ASSERT_TRUE(std::is_const_v<std::remove_pointer_t<decltype(immutable_slice.begin())>>);
Expand Down

0 comments on commit 636e199

Please sign in to comment.