17 template<
class RefCountable>
22 Ptr(std::nullptr_t =
nullptr)
26 "Ptr layout should be the same as of a raw pointer.");
29 template<
class OtherRefCountable>
33 Ptr(
const Ptr& other): m_ptr(other.get()) { addRef(); }
35 template<
class OtherRefCountable>
41 template<
class OtherRefCountable>
47 template<
class OtherRefCountable>
53 ~
Ptr() { releaseRef(); }
55 template<
class OtherRefCountable>
56 bool operator==(
const Ptr<OtherRefCountable>& other)
const {
return m_ptr == other.get(); }
58 template<
class OtherRefCountable>
59 bool operator!=(
const Ptr<OtherRefCountable>& other)
const {
return !operator==(other); }
75 template<
class OtherRefCountable>
76 void reset(OtherRefCountable* ptr)
89 RefCountable* result = m_ptr;
94 RefCountable*
get()
const {
return m_ptr; }
95 RefCountable* operator->()
const {
return m_ptr; }
96 RefCountable& operator*()
const {
return *m_ptr; }
98 operator bool()
const {
return m_ptr !=
nullptr; }
101 template<
class OtherRefCountable>
102 friend Ptr<OtherRefCountable> toPtr(OtherRefCountable* refCountable);
104 explicit Ptr(RefCountable* ptr): m_ptr(ptr) {}
106 template<
class OtherRefCountable>
107 explicit Ptr(OtherRefCountable* ptr): m_ptr(ptr) {}
121 Ptr& assignConst(
const Ptr& other)
123 if (
this != &other && m_ptr != other.get())
132 Ptr& assignRvalue(
Ptr&& other)
134 if (
this != &other && m_ptr != other.get())
137 m_ptr = other.releasePtr();
143 RefCountable* m_ptr =
nullptr;
146 template<
class RefCountable,
typename Object>
147 bool operator==(
const Ptr<RefCountable>& ptr, Object* p) {
return ptr.get() == p; }
149 template<
typename Object,
class RefCountable>
150 bool operator==(Object* p,
const Ptr<RefCountable>& ptr) {
return p == ptr.get(); }
152 template<
class RefCountable,
typename Object>
153 bool operator!=(
const Ptr<RefCountable>& ptr, Object* p) {
return ptr.get() != p; }
155 template<
typename Object,
class RefCountable>
156 bool operator!=(Object* p,
const Ptr<RefCountable>& ptr) {
return p != ptr.get(); }
158 template<
class RefCountable>
159 bool operator==(
const Ptr<RefCountable>& ptr, std::nullptr_t) {
return !ptr; }
161 template<
class RefCountable>
162 bool operator==(std::nullptr_t,
const Ptr<RefCountable>& ptr) {
return !ptr; }
164 template<
class RefCountable>
165 bool operator!=(
const Ptr<RefCountable>& ptr, std::nullptr_t) {
return (
bool) ptr; }
167 template<
class RefCountable>
168 bool operator!=(std::nullptr_t,
const Ptr<RefCountable>& ptr) {
return (
bool) ptr; }
170 template<
typename RefCountable>
171 bool operator<(const Ptr<const RefCountable>& first,
const Ptr<const RefCountable>& second)
173 return first.get() < second.get();
176 template<
typename RefCountable>
177 bool operator<(const Ptr<const RefCountable>& refCountable, std::nullptr_t)
179 return refCountable.get() <
nullptr;
182 template<
typename RefCountable>
183 bool operator<(std::nullptr_t, const Ptr<RefCountable>& refCountable)
185 return nullptr < refCountable.get();
194 template<
class RefCountable>
195 static Ptr<RefCountable> toPtr(RefCountable* refCountable)
197 return Ptr<RefCountable>(refCountable);
203 template<
class RefCountable,
typename... Args>
204 static Ptr<RefCountable> makePtr(Args&&... args)
206 return toPtr(
new RefCountable(std::forward<Args>(args)...));
213 template<
class Interface,
class RefCountablePtr,
214 typename OldInterfaceId>
215 static Ptr<Interface> queryInterfaceOfOldSdk(
216 RefCountablePtr refCountable,
const OldInterfaceId& interfaceId)
219 ? toPtr(static_cast<Interface*>(refCountable->queryInterface(interfaceId)))
227 template<
class RefCountable>
228 int refCount(
const Ptr<RefCountable>& ptr)
230 return refCount(ptr.get());
Ptr(Ptr &&other)
Definition: ptr.h:39
RefCountable * releasePtr()
Definition: ptr.h:87
Ptr(std::nullptr_t=nullptr)
Definition: ptr.h:22
Ptr & operator=(Ptr &&other)
Definition: ptr.h:51
Ptr & operator=(const Ptr &other)
Definition: ptr.h:45
Definition: apple_utils.h:6
Ptr(const Ptr &other)
Definition: ptr.h:33
void reset(OtherRefCountable *ptr)
Definition: ptr.h:76
void reset()
Definition: ptr.h:65