Added SparsePtr class
Which is basically a pointer with a different stride than it's type size Former-commit-id: a025889d7f9fa70f99e9cd5944682aebbd02317e
This commit is contained in:
parent
b595f703da
commit
f45a9d5bf6
|
|
@ -0,0 +1,60 @@
|
|||
// Copyright (C) 2014 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Graphics module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_SPARSEPTR_HPP
|
||||
#define NAZARA_SPARSEPTR_HPP
|
||||
|
||||
///FIXME: Est-ce que SparsePtr est vraiment le meilleur nom pour cette classe ?
|
||||
|
||||
#include <Nazara/Prerequesites.hpp>
|
||||
|
||||
template<typename T>
|
||||
class NzSparsePtr
|
||||
{
|
||||
public:
|
||||
NzSparsePtr();
|
||||
NzSparsePtr(void* ptr, unsigned int stride);
|
||||
NzSparsePtr(const NzSparsePtr& ptr) = default;
|
||||
~NzSparsePtr() = default;
|
||||
|
||||
void* Get() const;
|
||||
unsigned int GetStride() const;
|
||||
void Set(void* ptr);
|
||||
void SetStride(unsigned int stride);
|
||||
|
||||
T& operator*();
|
||||
T& operator->();
|
||||
T& operator[](unsigned int index);
|
||||
|
||||
NzSparsePtr operator+(unsigned int count);
|
||||
NzSparsePtr operator-(unsigned int count);
|
||||
|
||||
NzSparsePtr& operator+=(unsigned int count);
|
||||
NzSparsePtr& operator-=(unsigned int count);
|
||||
|
||||
NzSparsePtr& operator++();
|
||||
NzSparsePtr operator++(int);
|
||||
|
||||
NzSparsePtr& operator--();
|
||||
NzSparsePtr operator--(int);
|
||||
|
||||
bool operator==(const NzSparsePtr& ptr) const;
|
||||
bool operator!=(const NzSparsePtr& ptr) const;
|
||||
bool operator<(const NzSparsePtr& ptr) const;
|
||||
bool operator>(const NzSparsePtr& ptr) const;
|
||||
bool operator<=(const NzSparsePtr& ptr) const;
|
||||
bool operator>=(const NzSparsePtr& ptr) const;
|
||||
|
||||
NzSparsePtr& operator=(const NzSparsePtr& ptr) = default;
|
||||
|
||||
private:
|
||||
nzUInt8* m_ptr;
|
||||
unsigned int m_stride;
|
||||
};
|
||||
|
||||
#include <Nazara/Core/SparsePtr.inl>
|
||||
|
||||
#endif // NAZARA_SPARSEPTR_HPP
|
||||
|
|
@ -0,0 +1,158 @@
|
|||
// Copyright (C) 2014 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
template<typename T>
|
||||
NzSparsePtr<T>::NzSparsePtr() :
|
||||
m_ptr(nullptr),
|
||||
m_stride(0)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzSparsePtr<T>::NzSparsePtr(void* ptr, unsigned int stride)
|
||||
{
|
||||
Set(ptr);
|
||||
SetStride(stride);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void* NzSparsePtr<T>::Get() const
|
||||
{
|
||||
return m_ptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
unsigned int NzSparsePtr<T>::GetStride() const
|
||||
{
|
||||
return m_stride;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void NzSparsePtr<T>::Set(void* ptr)
|
||||
{
|
||||
m_ptr = reinterpret_cast<nzUInt8*>(ptr);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void NzSparsePtr<T>::SetStride(unsigned int stride)
|
||||
{
|
||||
m_stride = stride;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T& NzSparsePtr<T>::operator*()
|
||||
{
|
||||
return *reinterpret_cast<T*>(m_ptr);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T& NzSparsePtr<T>::operator->()
|
||||
{
|
||||
return *reinterpret_cast<T*>(m_ptr);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T& NzSparsePtr<T>::operator[](unsigned int index)
|
||||
{
|
||||
return *reinterpret_cast<T*>(m_ptr + index*m_stride);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzSparsePtr<T> NzSparsePtr<T>::operator+(unsigned int count)
|
||||
{
|
||||
return NzSparsePtr(m_ptr + count*m_stride, m_stride);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzSparsePtr<T> NzSparsePtr<T>::operator-(unsigned int count)
|
||||
{
|
||||
return NzSparsePtr(m_ptr - count*m_stride, m_stride);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzSparsePtr<T>& NzSparsePtr<T>::operator+=(unsigned int count)
|
||||
{
|
||||
m_ptr += count*m_stride;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzSparsePtr<T>& NzSparsePtr<T>::operator-=(unsigned int count)
|
||||
{
|
||||
m_ptr -= count*m_stride;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzSparsePtr<T>& NzSparsePtr<T>::operator++()
|
||||
{
|
||||
m_ptr += m_stride;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzSparsePtr<T> NzSparsePtr<T>::operator++(int)
|
||||
{
|
||||
NzSparsePtr tmp(*this);
|
||||
operator++();
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzSparsePtr<T>& NzSparsePtr<T>::operator--()
|
||||
{
|
||||
m_ptr -= m_stride;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzSparsePtr<T> NzSparsePtr<T>::operator--(int)
|
||||
{
|
||||
NzSparsePtr tmp(*this);
|
||||
operator--();
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzSparsePtr<T>::operator==(const NzSparsePtr& ptr) const
|
||||
{
|
||||
return m_ptr == ptr.m_ptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzSparsePtr<T>::operator!=(const NzSparsePtr& ptr) const
|
||||
{
|
||||
return m_ptr != ptr.m_ptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzSparsePtr<T>::operator<(const NzSparsePtr& ptr) const
|
||||
{
|
||||
return m_ptr < ptr.m_ptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzSparsePtr<T>::operator>(const NzSparsePtr& ptr) const
|
||||
{
|
||||
return m_ptr > ptr.m_ptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzSparsePtr<T>::operator<=(const NzSparsePtr& ptr) const
|
||||
{
|
||||
return m_ptr <= ptr.m_ptr;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzSparsePtr<T>::operator>=(const NzSparsePtr& ptr) const
|
||||
{
|
||||
return m_ptr >= ptr.m_ptr;
|
||||
}
|
||||
|
||||
|
||||
#include <Nazara/Core/DebugOff.hpp>
|
||||
Loading…
Reference in New Issue