diff --git a/include/Nazara/Core/SparsePtr.hpp b/include/Nazara/Core/SparsePtr.hpp index 737a2f1fe..2977cd9bc 100644 --- a/include/Nazara/Core/SparsePtr.hpp +++ b/include/Nazara/Core/SparsePtr.hpp @@ -19,8 +19,8 @@ namespace Nz class SparsePtr { public: - using BytePtr = typename std::conditional::value, const UInt8*, UInt8*>::type; - using VoidPtr = typename std::conditional::value, const void*, void*>::type; + using BytePtr = std::conditional_t::value, const UInt8*, UInt8*>; + using VoidPtr = std::conditional_t::value, const void*, void*>; SparsePtr(); SparsePtr(T* ptr); @@ -46,18 +46,16 @@ namespace Nz explicit operator T*() const; T& operator*() const; T* operator->() const; - T& operator[](std::size_t index) const; + template T& operator[](U index) const; SparsePtr& operator=(const SparsePtr& ptr) = default; - SparsePtr operator+(Int64 count) const; - SparsePtr operator+(UInt64 count) const; - SparsePtr operator-(Int64 count) const; - SparsePtr operator-(UInt64 count) const; + template SparsePtr operator+(U count) const; + template SparsePtr operator-(U count) const; std::ptrdiff_t operator-(const SparsePtr& ptr) const; - SparsePtr& operator+=(Int64 count); - SparsePtr& operator-=(Int64 count); + template SparsePtr& operator+=(U count); + template SparsePtr& operator-=(U count); SparsePtr& operator++(); SparsePtr operator++(int); diff --git a/include/Nazara/Core/SparsePtr.inl b/include/Nazara/Core/SparsePtr.inl index a429da527..aab212438 100644 --- a/include/Nazara/Core/SparsePtr.inl +++ b/include/Nazara/Core/SparsePtr.inl @@ -247,8 +247,11 @@ namespace Nz */ template - T& SparsePtr::operator[](std::size_t index) const + template + T& SparsePtr::operator[](U index) const { + static_assert(std::is_integral_v, "index must be an integral type"); + return *reinterpret_cast(m_ptr + index * m_stride); } @@ -260,8 +263,11 @@ namespace Nz */ template - SparsePtr SparsePtr::operator+(Int64 count) const + template + SparsePtr SparsePtr::operator+(U count) const { + static_assert(std::is_integral_v, "count must be an integral type"); + return SparsePtr(m_ptr + count * m_stride, m_stride); } @@ -273,34 +279,11 @@ namespace Nz */ template - SparsePtr SparsePtr::operator+(UInt64 count) const + template + SparsePtr SparsePtr::operator-(U count) const { - return SparsePtr(m_ptr + count * m_stride, m_stride); - } + static_assert(std::is_integral_v, "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 - SparsePtr SparsePtr::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 - SparsePtr SparsePtr::operator-(UInt64 count) const - { return SparsePtr(m_ptr - count * m_stride, m_stride); } @@ -325,25 +308,22 @@ namespace Nz */ template - SparsePtr& SparsePtr::operator+=(Int64 count) + template + SparsePtr& SparsePtr::operator+=(U count) { - m_ptr += count * m_stride; + static_assert(std::is_integral_v, "count must be an integral type"); + m_ptr += count * m_stride; 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 - SparsePtr& SparsePtr::operator-=(Int64 count) + template + SparsePtr& SparsePtr::operator-=(U count) { - m_ptr -= count * m_stride; + static_assert(std::is_integral_v, "count must be an integral type"); + m_ptr -= count * m_stride; return *this; } diff --git a/tests/Engine/Core/SparsePtrTest.cpp b/tests/Engine/Core/SparsePtrTest.cpp index 2e8c19844..d3c832a71 100644 --- a/tests/Engine/Core/SparsePtrTest.cpp +++ b/tests/Engine/Core/SparsePtrTest.cpp @@ -34,13 +34,13 @@ SCENARIO("SparsePtr", "[CORE][SPARSEPTR]") THEN("Operator+ and operator-") { - auto offsetTwo = sparsePtr + 2ull; + auto offsetTwo = sparsePtr + 2; CHECK(4 == *offsetTwo); - auto offsetZero = offsetTwo - 2ull; + auto offsetZero = offsetTwo - 2; CHECK(0 == *offsetZero); - CHECK((offsetTwo - offsetZero) == 2ull); + CHECK((offsetTwo - offsetZero) == 2); } } }