SPH
RawPtr.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "common/Assert.h"
9 
11 
18 template <typename T>
19 class RawPtr {
20 private:
21  T* ptr;
22 
23 public:
25  : ptr(nullptr) {}
26 
27  INLINE RawPtr(std::nullptr_t)
28  : ptr(nullptr) {}
29 
30  template <typename T2, typename = std::enable_if_t<std::is_convertible<T2*, T*>::value>>
31  INLINE RawPtr(T2* ptr)
32  : ptr(ptr) {}
33 
34  INLINE RawPtr& operator=(std::nullptr_t) {
35  ptr = nullptr;
36  return *this;
37  }
38 
39  template <typename T2, typename = std::enable_if_t<std::is_convertible<T2*, T*>::value>>
40  INLINE RawPtr& operator=(T2* rhs) {
41  ptr = rhs;
42  return *this;
43  }
44 
45  INLINE T& operator*() const {
46  SPH_ASSERT(ptr);
47  return *ptr;
48  }
49 
50  INLINE T* operator->() const {
51  SPH_ASSERT(ptr);
52  return ptr;
53  }
54 
55  INLINE explicit operator bool() const {
56  return ptr != nullptr;
57  }
58 
59  INLINE bool operator!() const {
60  return !ptr;
61  }
62 
63  operator RawPtr<const T>() const {
64  return ptr;
65  }
66 
67  INLINE T* get() const {
68  return ptr;
69  }
70 
71  INLINE void swap(RawPtr& other) {
72  std::swap(ptr, other.ptr);
73  }
74 };
75 
76 template <typename T1, typename T2>
78  return dynamic_cast<T1*>(source.get());
79 }
80 
81 template <typename T>
83  return std::addressof(ref);
84 }
85 
86 template <typename T>
87 INLINE bool operator==(const RawPtr<T> lhs, std::nullptr_t) {
88  return !lhs;
89 }
90 
91 template <typename T>
92 INLINE bool operator==(std::nullptr_t, const RawPtr<T> rhs) {
93  return !rhs;
94 }
95 
96 template <typename T>
97 INLINE bool operator!=(const RawPtr<T> lhs, std::nullptr_t) {
98  return bool(lhs);
99 }
100 
101 template <typename T>
102 INLINE bool operator!=(std::nullptr_t, const RawPtr<T> rhs) {
103  return bool(rhs);
104 }
105 
106 template <typename T1, typename T2>
107 INLINE bool operator==(const RawPtr<T1> lhs, const RawPtr<T2> rhs) {
108  return lhs.get() == rhs.get();
109 }
110 
111 template <typename T1, typename T2>
112 INLINE bool operator!=(const RawPtr<T1> lhs, const RawPtr<T2> rhs) {
113  return lhs.get() != rhs.get();
114 }
115 
116 template <typename T>
117 INLINE bool operator<(const RawPtr<T>& lhs, const RawPtr<T>& rhs) {
118  return lhs.get() < rhs.get();
119 }
120 
122 
123 namespace std {
124 template <typename T>
125 void swap(Sph::RawPtr<T>& p1, Sph::RawPtr<T>& p2) {
126  p1.swap(p2);
127 }
128 } // namespace std
Custom assertions.
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
INLINE bool operator<(const RawPtr< T > &lhs, const RawPtr< T > &rhs)
Definition: RawPtr.h:117
INLINE RawPtr< T1 > dynamicCast(RawPtr< T2 > source)
Definition: RawPtr.h:77
INLINE RawPtr< T > addressOf(T &ref)
Definition: RawPtr.h:82
INLINE bool operator==(const RawPtr< T > lhs, std::nullptr_t)
Definition: RawPtr.h:87
INLINE bool operator!=(const RawPtr< T > lhs, std::nullptr_t)
Definition: RawPtr.h:97
Non-owning wrapper of pointer.
Definition: RawPtr.h:19
INLINE RawPtr & operator=(std::nullptr_t)
Definition: RawPtr.h:34
INLINE bool operator!() const
Definition: RawPtr.h:59
INLINE RawPtr(std::nullptr_t)
Definition: RawPtr.h:27
INLINE void swap(RawPtr &other)
Definition: RawPtr.h:71
INLINE RawPtr()
Definition: RawPtr.h:24
INLINE T & operator*() const
Definition: RawPtr.h:45
INLINE RawPtr & operator=(T2 *rhs)
Definition: RawPtr.h:40
INLINE T * operator->() const
Definition: RawPtr.h:50
INLINE RawPtr(T2 *ptr)
Definition: RawPtr.h:31
INLINE T * get() const
Definition: RawPtr.h:67
Overload of std::swap for Sph::Array.
Definition: Array.h:578
void swap(Sph::Array< T, TCounter > &ar1, Sph::Array< T, TCounter > &ar2)
Definition: Array.h:580