diff --git a/include/Nazara/Core/SparsePtr.hpp b/include/Nazara/Core/SparsePtr.hpp new file mode 100644 index 000000000..0e4ed3b7c --- /dev/null +++ b/include/Nazara/Core/SparsePtr.hpp @@ -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 + +template +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 + +#endif // NAZARA_SPARSEPTR_HPP diff --git a/include/Nazara/Core/SparsePtr.inl b/include/Nazara/Core/SparsePtr.inl new file mode 100644 index 000000000..73bdee917 --- /dev/null +++ b/include/Nazara/Core/SparsePtr.inl @@ -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 + +template +NzSparsePtr::NzSparsePtr() : +m_ptr(nullptr), +m_stride(0) +{ +} + +template +NzSparsePtr::NzSparsePtr(void* ptr, unsigned int stride) +{ + Set(ptr); + SetStride(stride); +} + +template +void* NzSparsePtr::Get() const +{ + return m_ptr; +} + +template +unsigned int NzSparsePtr::GetStride() const +{ + return m_stride; +} + +template +void NzSparsePtr::Set(void* ptr) +{ + m_ptr = reinterpret_cast(ptr); +} + +template +void NzSparsePtr::SetStride(unsigned int stride) +{ + m_stride = stride; +} + +template +T& NzSparsePtr::operator*() +{ + return *reinterpret_cast(m_ptr); +} + +template +T& NzSparsePtr::operator->() +{ + return *reinterpret_cast(m_ptr); +} + +template +T& NzSparsePtr::operator[](unsigned int index) +{ + return *reinterpret_cast(m_ptr + index*m_stride); +} + +template +NzSparsePtr NzSparsePtr::operator+(unsigned int count) +{ + return NzSparsePtr(m_ptr + count*m_stride, m_stride); +} + +template +NzSparsePtr NzSparsePtr::operator-(unsigned int count) +{ + return NzSparsePtr(m_ptr - count*m_stride, m_stride); +} + +template +NzSparsePtr& NzSparsePtr::operator+=(unsigned int count) +{ + m_ptr += count*m_stride; + return *this; +} + +template +NzSparsePtr& NzSparsePtr::operator-=(unsigned int count) +{ + m_ptr -= count*m_stride; + return *this; +} + +template +NzSparsePtr& NzSparsePtr::operator++() +{ + m_ptr += m_stride; + return *this; +} + +template +NzSparsePtr NzSparsePtr::operator++(int) +{ + NzSparsePtr tmp(*this); + operator++(); + + return tmp; +} + +template +NzSparsePtr& NzSparsePtr::operator--() +{ + m_ptr -= m_stride; + return *this; +} + +template +NzSparsePtr NzSparsePtr::operator--(int) +{ + NzSparsePtr tmp(*this); + operator--(); + + return tmp; +} + +template +bool NzSparsePtr::operator==(const NzSparsePtr& ptr) const +{ + return m_ptr == ptr.m_ptr; +} + +template +bool NzSparsePtr::operator!=(const NzSparsePtr& ptr) const +{ + return m_ptr != ptr.m_ptr; +} + +template +bool NzSparsePtr::operator<(const NzSparsePtr& ptr) const +{ + return m_ptr < ptr.m_ptr; +} + +template +bool NzSparsePtr::operator>(const NzSparsePtr& ptr) const +{ + return m_ptr > ptr.m_ptr; +} + +template +bool NzSparsePtr::operator<=(const NzSparsePtr& ptr) const +{ + return m_ptr <= ptr.m_ptr; +} + +template +bool NzSparsePtr::operator>=(const NzSparsePtr& ptr) const +{ + return m_ptr >= ptr.m_ptr; +} + + +#include