Core/SparsePtr: Use template type for +/- and +=/-=

This commit is contained in:
Jérôme Leclercq 2022-01-23 13:27:39 +01:00
parent bba5d7a803
commit 89e9e41357
3 changed files with 29 additions and 51 deletions

View File

@ -19,8 +19,8 @@ namespace Nz
class SparsePtr class SparsePtr
{ {
public: public:
using BytePtr = typename std::conditional<std::is_const<T>::value, const UInt8*, UInt8*>::type; using BytePtr = std::conditional_t<std::is_const<T>::value, const UInt8*, UInt8*>;
using VoidPtr = typename std::conditional<std::is_const<T>::value, const void*, void*>::type; using VoidPtr = std::conditional_t<std::is_const<T>::value, const void*, void*>;
SparsePtr(); SparsePtr();
SparsePtr(T* ptr); SparsePtr(T* ptr);
@ -46,18 +46,16 @@ namespace Nz
explicit operator T*() const; explicit operator T*() const;
T& operator*() const; T& operator*() const;
T* operator->() const; T* operator->() const;
T& operator[](std::size_t index) const; template<typename U> T& operator[](U index) const;
SparsePtr& operator=(const SparsePtr& ptr) = default; SparsePtr& operator=(const SparsePtr& ptr) = default;
SparsePtr operator+(Int64 count) const; template<typename U> SparsePtr operator+(U count) const;
SparsePtr operator+(UInt64 count) const; template<typename U> SparsePtr operator-(U count) const;
SparsePtr operator-(Int64 count) const;
SparsePtr operator-(UInt64 count) const;
std::ptrdiff_t operator-(const SparsePtr& ptr) const; std::ptrdiff_t operator-(const SparsePtr& ptr) const;
SparsePtr& operator+=(Int64 count); template<typename U> SparsePtr& operator+=(U count);
SparsePtr& operator-=(Int64 count); template<typename U> SparsePtr& operator-=(U count);
SparsePtr& operator++(); SparsePtr& operator++();
SparsePtr operator++(int); SparsePtr operator++(int);

View File

@ -247,8 +247,11 @@ namespace Nz
*/ */
template<typename T> template<typename T>
T& SparsePtr<T>::operator[](std::size_t index) const template<typename U>
T& SparsePtr<T>::operator[](U index) const
{ {
static_assert(std::is_integral_v<U>, "index must be an integral type");
return *reinterpret_cast<T*>(m_ptr + index * m_stride); return *reinterpret_cast<T*>(m_ptr + index * m_stride);
} }
@ -260,8 +263,11 @@ namespace Nz
*/ */
template<typename T> template<typename T>
SparsePtr<T> SparsePtr<T>::operator+(Int64 count) const template<typename U>
SparsePtr<T> SparsePtr<T>::operator+(U count) const
{ {
static_assert(std::is_integral_v<U>, "count must be an integral type");
return SparsePtr(m_ptr + count * m_stride, m_stride); return SparsePtr(m_ptr + count * m_stride, m_stride);
} }
@ -273,34 +279,11 @@ namespace Nz
*/ */
template<typename T> template<typename T>
SparsePtr<T> SparsePtr<T>::operator+(UInt64 count) const template<typename U>
SparsePtr<T> SparsePtr<T>::operator-(U count) const
{ {
return SparsePtr(m_ptr + count * m_stride, m_stride); static_assert(std::is_integral_v<U>, "count must be an integral type");
}
/*!
* \brief Gets the SparsePtr with an offset
* \return A SparsePtr with the new stride
*
* \param count Number of stride to do
*/
template<typename T>
SparsePtr<T> SparsePtr<T>::operator-(Int64 count) const
{
return SparsePtr(m_ptr - count * m_stride, m_stride);
}
/*!
* \brief Gets the SparsePtr with an offset
* \return A SparsePtr with the new stride
*
* \param count Number of stride to do
*/
template<typename T>
SparsePtr<T> SparsePtr<T>::operator-(UInt64 count) const
{
return SparsePtr(m_ptr - count * m_stride, m_stride); return SparsePtr(m_ptr - count * m_stride, m_stride);
} }
@ -325,25 +308,22 @@ namespace Nz
*/ */
template<typename T> template<typename T>
SparsePtr<T>& SparsePtr<T>::operator+=(Int64 count) template<typename U>
SparsePtr<T>& SparsePtr<T>::operator+=(U count)
{ {
m_ptr += count * m_stride; static_assert(std::is_integral_v<U>, "count must be an integral type");
m_ptr += count * m_stride;
return *this; return *this;
} }
/*!
* \brief Gets the SparsePtr with an offset
* \return A reference to this pointer with the new stride
*
* \param count Number of stride to do
*/
template<typename T> template<typename T>
SparsePtr<T>& SparsePtr<T>::operator-=(Int64 count) template<typename U>
SparsePtr<T>& SparsePtr<T>::operator-=(U count)
{ {
m_ptr -= count * m_stride; static_assert(std::is_integral_v<U>, "count must be an integral type");
m_ptr -= count * m_stride;
return *this; return *this;
} }

View File

@ -34,13 +34,13 @@ SCENARIO("SparsePtr", "[CORE][SPARSEPTR]")
THEN("Operator+ and operator-") THEN("Operator+ and operator-")
{ {
auto offsetTwo = sparsePtr + 2ull; auto offsetTwo = sparsePtr + 2;
CHECK(4 == *offsetTwo); CHECK(4 == *offsetTwo);
auto offsetZero = offsetTwo - 2ull; auto offsetZero = offsetTwo - 2;
CHECK(0 == *offsetZero); CHECK(0 == *offsetZero);
CHECK((offsetTwo - offsetZero) == 2ull); CHECK((offsetTwo - offsetZero) == 2);
} }
} }
} }