Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
SparsePtr.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Graphics module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_SPARSEPTR_HPP
8 #define NAZARA_SPARSEPTR_HPP
9 
11 
12 #include <Nazara/Prerequesites.hpp>
13 #include <cstddef>
14 #include <type_traits>
15 
16 namespace Nz
17 {
18  template<typename T>
19  class SparsePtr
20  {
21  public:
22  using BytePtr = typename std::conditional<std::is_const<T>::value, const UInt8*, UInt8*>::type;
23  using VoidPtr = typename std::conditional<std::is_const<T>::value, const void*, void*>::type;
24 
25  SparsePtr();
26  SparsePtr(T* ptr);
27  SparsePtr(VoidPtr ptr, int stride);
28  SparsePtr(VoidPtr ptr, std::size_t stride);
29  template<typename U> SparsePtr(const SparsePtr<U>& ptr);
30  SparsePtr(const SparsePtr& ptr) = default;
31  ~SparsePtr() = default;
32 
33  VoidPtr GetPtr() const;
34  int GetStride() const;
35 
36  void Reset();
37  void Reset(T* ptr);
38  void Reset(VoidPtr ptr, int stride);
39  void Reset(const SparsePtr& ptr);
40  template<typename U> void Reset(const SparsePtr<U>& ptr);
41 
42  void SetPtr(VoidPtr ptr);
43  void SetStride(int stride);
44 
45  explicit operator bool() const;
46  operator T*() const;
47  T& operator*() const;
48  T* operator->() const;
49  T& operator[](int index) const;
50 
51  SparsePtr& operator=(const SparsePtr& ptr) = default;
52 
53  SparsePtr operator+(int count) const;
54  SparsePtr operator+(unsigned int count) const;
55  SparsePtr operator-(int count) const;
56  SparsePtr operator-(unsigned int count) const;
57  std::ptrdiff_t operator-(const SparsePtr& ptr) const;
58 
59  SparsePtr& operator+=(int count);
60  SparsePtr& operator-=(int count);
61 
63  SparsePtr operator++(int);
64 
66  SparsePtr operator--(int);
67 
68  bool operator==(const SparsePtr& ptr) const;
69  bool operator!=(const SparsePtr& ptr) const;
70  bool operator<(const SparsePtr& ptr) const;
71  bool operator>(const SparsePtr& ptr) const;
72  bool operator<=(const SparsePtr& ptr) const;
73  bool operator>=(const SparsePtr& ptr) const;
74 
75  private:
76  BytePtr m_ptr;
77  int m_stride;
78  };
79 }
80 
81 #include <Nazara/Core/SparsePtr.inl>
82 
83 #endif // NAZARA_SPARSEPTR_HPP
bool operator<=(const SparsePtr &ptr) const
Compares the SparsePtr to another one.
Definition: SparsePtr.inl:471
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
SparsePtr operator-(int count) const
Gets the SparsePtr with an offset.
Definition: SparsePtr.inl:289
SparsePtr & operator--()
Gets the SparsePtr with the previous element.
Definition: SparsePtr.inl:387
SparsePtr operator+(int count) const
Gets the SparsePtr with an offset.
Definition: SparsePtr.inl:263
T & operator[](int index) const
Gets the ith element of the stride pointer.
Definition: SparsePtr.inl:250
bool operator!=(const SparsePtr &ptr) const
Compares the SparsePtr to another one.
Definition: SparsePtr.inl:432
VoidPtr GetPtr() const
Gets the original pointer.
Definition: SparsePtr.inl:89
Core class that represents a pointer and the step between two elements.
Definition: SparsePtr.hpp:19
bool operator<(const SparsePtr &ptr) const
Compares the SparsePtr to another one.
Definition: SparsePtr.inl:445
SparsePtr & operator+=(int count)
Gets the SparsePtr with an offset.
Definition: SparsePtr.inl:328
int GetStride() const
Gets the stride.
Definition: SparsePtr.inl:100
bool operator>=(const SparsePtr &ptr) const
Compares the SparsePtr to another one.
Definition: SparsePtr.inl:484
void SetStride(int stride)
Sets the stride.
Definition: SparsePtr.inl:193
SparsePtr()
Constructs a SparsePtr object by default.
Definition: SparsePtr.inl:24
SparsePtr & operator++()
Gets the SparsePtr with the next element.
Definition: SparsePtr.inl:356
T & operator*() const
Dereferences the pointer.
Definition: SparsePtr.inl:226
SparsePtr & operator-=(int count)
Gets the SparsePtr with an offset.
Definition: SparsePtr.inl:343
bool operator==(const SparsePtr &ptr) const
Compares the SparsePtr to another one.
Definition: SparsePtr.inl:419
void SetPtr(VoidPtr ptr)
Sets the pointer.
Definition: SparsePtr.inl:181
T * operator->() const
Dereferences the pointer.
Definition: SparsePtr.inl:237
void Reset()
Resets the SparsePtr.
Definition: SparsePtr.inl:110
bool operator>(const SparsePtr &ptr) const
Compares the SparsePtr to another one.
Definition: SparsePtr.inl:458