Improved SparsePtr class
Added implicit conversion from a normal pointer to a sparse pointer Added implicit conversion to boolean Added implicit conversion to normal pointer Added support for const pointers Renamed Get/Set to GetPtr/SetPtr Former-commit-id: 32d5d2ec6a7b296c5b89b722de9ca142d5c64aae
This commit is contained in:
parent
e32c0c626e
commit
522ceb1e30
|
|
@ -10,21 +10,34 @@
|
|||
///FIXME: Est-ce que SparsePtr est vraiment le meilleur nom pour cette classe ?
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
#include <type_traits>
|
||||
|
||||
template<typename T>
|
||||
class NzSparsePtr
|
||||
{
|
||||
public:
|
||||
using BytePtr = typename std::conditional<std::is_const<T>::value, const nzUInt8*, nzUInt8*>::type;
|
||||
using VoidPtr = typename std::conditional<std::is_const<T>::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;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -5,21 +5,25 @@
|
|||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
template<typename T>
|
||||
NzSparsePtr<T>::NzSparsePtr() :
|
||||
m_ptr(nullptr),
|
||||
m_stride(0)
|
||||
NzSparsePtr<T>::NzSparsePtr()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzSparsePtr<T>::NzSparsePtr(void* ptr, unsigned int stride)
|
||||
NzSparsePtr<T>::NzSparsePtr(T* ptr)
|
||||
{
|
||||
Set(ptr);
|
||||
SetStride(stride);
|
||||
Reset(ptr);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void* NzSparsePtr<T>::Get() const
|
||||
NzSparsePtr<T>::NzSparsePtr(VoidPtr ptr, unsigned int stride)
|
||||
{
|
||||
Reset(ptr, stride);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename NzSparsePtr<T>::VoidPtr NzSparsePtr<T>::GetPtr() const
|
||||
{
|
||||
return m_ptr;
|
||||
}
|
||||
|
|
@ -31,9 +35,37 @@ unsigned int NzSparsePtr<T>::GetStride() const
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
void NzSparsePtr<T>::Set(void* ptr)
|
||||
void NzSparsePtr<T>::Reset()
|
||||
{
|
||||
m_ptr = reinterpret_cast<nzUInt8*>(ptr);
|
||||
SetPtr(nullptr);
|
||||
SetStride(0);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void NzSparsePtr<T>::Reset(T* ptr)
|
||||
{
|
||||
SetPtr(ptr);
|
||||
SetStride(sizeof(T));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void NzSparsePtr<T>::Reset(VoidPtr ptr, unsigned int stride)
|
||||
{
|
||||
SetPtr(ptr);
|
||||
SetStride(stride);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void NzSparsePtr<T>::Reset(const NzSparsePtr& ptr)
|
||||
{
|
||||
SetPtr(ptr.GetPtr());
|
||||
SetStride(ptr.GetStride());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void NzSparsePtr<T>::SetPtr(VoidPtr ptr)
|
||||
{
|
||||
m_ptr = reinterpret_cast<BytePtr>(ptr);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
@ -42,6 +74,18 @@ void NzSparsePtr<T>::SetStride(unsigned int stride)
|
|||
m_stride = stride;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzSparsePtr<T>::operator bool() const
|
||||
{
|
||||
return m_ptr != nullptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzSparsePtr<T>::operator T*() const
|
||||
{
|
||||
return reinterpret_cast<T*>(m_ptr);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T& NzSparsePtr<T>::operator*() const
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue