diff --git a/include/Nazara/Core/SparsePtr.hpp b/include/Nazara/Core/SparsePtr.hpp index eec23149c..aff3460ac 100644 --- a/include/Nazara/Core/SparsePtr.hpp +++ b/include/Nazara/Core/SparsePtr.hpp @@ -10,21 +10,34 @@ ///FIXME: Est-ce que SparsePtr est vraiment le meilleur nom pour cette classe ? #include +#include template class NzSparsePtr { public: + using BytePtr = typename std::conditional::value, const nzUInt8*, nzUInt8*>::type; + using VoidPtr = typename std::conditional::value, const void*, void*>::type; + NzSparsePtr(); - NzSparsePtr(void* ptr, unsigned int stride); + NzSparsePtr(T* ptr); + NzSparsePtr(VoidPtr ptr, unsigned int stride); NzSparsePtr(const NzSparsePtr& ptr) = default; ~NzSparsePtr() = default; - void* Get() const; + VoidPtr GetPtr() const; unsigned int GetStride() const; - void Set(void* ptr); + + void Reset(); + void Reset(T* ptr); + void Reset(VoidPtr ptr, unsigned int stride); + void Reset(const NzSparsePtr& ptr); + + void SetPtr(VoidPtr ptr); void SetStride(unsigned int stride); + operator bool() const; + operator T*() const; T& operator*() const; T& operator->() const; T& operator[](unsigned int index) const; @@ -51,7 +64,7 @@ class NzSparsePtr NzSparsePtr& operator=(const NzSparsePtr& ptr) = default; private: - nzUInt8* m_ptr; + BytePtr m_ptr; unsigned int m_stride; }; diff --git a/include/Nazara/Core/SparsePtr.inl b/include/Nazara/Core/SparsePtr.inl index f769e577e..b98599bfd 100644 --- a/include/Nazara/Core/SparsePtr.inl +++ b/include/Nazara/Core/SparsePtr.inl @@ -5,21 +5,25 @@ #include template -NzSparsePtr::NzSparsePtr() : -m_ptr(nullptr), -m_stride(0) +NzSparsePtr::NzSparsePtr() { + Reset(); } template -NzSparsePtr::NzSparsePtr(void* ptr, unsigned int stride) +NzSparsePtr::NzSparsePtr(T* ptr) { - Set(ptr); - SetStride(stride); + Reset(ptr); } template -void* NzSparsePtr::Get() const +NzSparsePtr::NzSparsePtr(VoidPtr ptr, unsigned int stride) +{ + Reset(ptr, stride); +} + +template +typename NzSparsePtr::VoidPtr NzSparsePtr::GetPtr() const { return m_ptr; } @@ -31,9 +35,37 @@ unsigned int NzSparsePtr::GetStride() const } template -void NzSparsePtr::Set(void* ptr) +void NzSparsePtr::Reset() { - m_ptr = reinterpret_cast(ptr); + SetPtr(nullptr); + SetStride(0); +} + +template +void NzSparsePtr::Reset(T* ptr) +{ + SetPtr(ptr); + SetStride(sizeof(T)); +} + +template +void NzSparsePtr::Reset(VoidPtr ptr, unsigned int stride) +{ + SetPtr(ptr); + SetStride(stride); +} + +template +void NzSparsePtr::Reset(const NzSparsePtr& ptr) +{ + SetPtr(ptr.GetPtr()); + SetStride(ptr.GetStride()); +} + +template +void NzSparsePtr::SetPtr(VoidPtr ptr) +{ + m_ptr = reinterpret_cast(ptr); } template @@ -42,6 +74,18 @@ void NzSparsePtr::SetStride(unsigned int stride) m_stride = stride; } +template +NzSparsePtr::operator bool() const +{ + return m_ptr != nullptr; +} + +template +NzSparsePtr::operator T*() const +{ + return reinterpret_cast(m_ptr); +} + template T& NzSparsePtr::operator*() const {