Skip to content

Commit

Permalink
allow accessing vector data only for trivially copieable types
Browse files Browse the repository at this point in the history
  • Loading branch information
drexlerd committed Aug 27, 2024
1 parent 92d45be commit 5b66ee5
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions include/flatmemory/details/types/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ class View<Vector<T>>
*/

using ValueType = T;
using T_ = typename maybe_builder<T>::type;

/**
* Constructors
Expand All @@ -196,8 +195,10 @@ class View<Vector<T>>
auto operator[](size_t pos) const;
auto at(size_t pos);
auto at(size_t pos) const;
T_* data();
const T_* data() const;
T* data()
requires(IsTriviallyCopyable<T>);
const T* data() const
requires(IsTriviallyCopyable<T>);
uint8_t* buffer();
const uint8_t* buffer() const;

Expand Down Expand Up @@ -289,7 +290,6 @@ class ConstView<Vector<T>>
*/

using ValueType = T;
using T_ = typename maybe_builder<T>::type;

/**
* Constructors
Expand All @@ -316,7 +316,8 @@ class ConstView<Vector<T>>

auto operator[](size_t pos) const;
auto at(size_t pos) const;
const T_* data() const;
const T* data() const
requires(IsTriviallyCopyable<T>);
const uint8_t* buffer() const;

/**
Expand Down Expand Up @@ -684,15 +685,17 @@ auto View<Vector<T>>::at(size_t pos) const
}

template<IsTriviallyCopyableOrNonTrivialType T>
View<Vector<T>>::T_* View<Vector<T>>::data()
T* View<Vector<T>>::data()
requires(IsTriviallyCopyable<T>)
{
return reinterpret_cast<View<Vector<T>>::T_*>(m_buf + Layout<Vector<T>>::vector_data_position);
return reinterpret_cast<T*>(m_buf + Layout<Vector<T>>::vector_data_position);
}

template<IsTriviallyCopyableOrNonTrivialType T>
const View<Vector<T>>::T_* View<Vector<T>>::data() const
const T* View<Vector<T>>::data() const
requires(IsTriviallyCopyable<T>)
{
return reinterpret_cast<const View<Vector<T>>::T_*>(m_buf + Layout<Vector<T>>::vector_data_position);
return reinterpret_cast<const T*>(m_buf + Layout<Vector<T>>::vector_data_position);
}

template<IsTriviallyCopyableOrNonTrivialType T>
Expand Down Expand Up @@ -950,9 +953,10 @@ auto ConstView<Vector<T>>::at(size_t pos) const
}

template<IsTriviallyCopyableOrNonTrivialType T>
const ConstView<Vector<T>>::T_* ConstView<Vector<T>>::data() const
const T* ConstView<Vector<T>>::data() const
requires(IsTriviallyCopyable<T>)
{
return reinterpret_cast<const ConstView<Vector<T>>::T_*>(m_buf + Layout<Vector<T>>::vector_data_position);
return reinterpret_cast<const T*>(m_buf + Layout<Vector<T>>::vector_data_position);
}

template<IsTriviallyCopyableOrNonTrivialType T>
Expand Down

0 comments on commit 5b66ee5

Please sign in to comment.