17 template<
class RefCountable>
22 Ptr(std::nullptr_t =
nullptr)
26 "Ptr layout should be the same as of a raw pointer.");
31 template<
class OtherRefCountable>
32 explicit Ptr(OtherRefCountable* ptr): m_ptr(ptr) {}
34 template<
class OtherRefCountable>
35 Ptr(
const Ptr<OtherRefCountable>& other): m_ptr(other.get()) { addRef(); }
38 Ptr(
const Ptr& other): m_ptr(other.get()) { addRef(); }
40 template<
class OtherRefCountable>
46 template<
class OtherRefCountable>
52 template<
class OtherRefCountable>
58 ~
Ptr() { releaseRef(); }
60 template<
class OtherRefCountable>
61 bool operator==(
const Ptr<OtherRefCountable>& other)
const {
return m_ptr == other.get(); }
63 template<
class OtherRefCountable>
64 bool operator!=(
const Ptr<OtherRefCountable>& other)
const {
return !operator==(other); }
80 template<
class OtherRefCountable>
81 void reset(OtherRefCountable* ptr)
100 RefCountable* operator->()
const {
return m_ptr; }
101 RefCountable& operator*()
const {
return *m_ptr; }
103 explicit operator bool()
const {
return m_ptr !=
nullptr; }
118 Ptr& assignConst(
const Ptr& other)
120 if (
this != &other && m_ptr != other.get())
129 Ptr& assignRvalue(
Ptr&& other)
131 if (
this != &other && m_ptr != other.get())
134 m_ptr = other.releasePtr();
140 RefCountable* m_ptr =
nullptr;
143 template<
class RefCountable,
typename Object>
144 bool operator==(
const Ptr<RefCountable>& ptr, Object* p) {
return ptr.get() == p; }
146 template<
typename Object,
class RefCountable>
147 bool operator==(Object* p,
const Ptr<RefCountable>& ptr) {
return p == ptr.get(); }
149 template<
class RefCountable,
typename Object>
150 bool operator!=(
const Ptr<RefCountable>& ptr, Object* p) {
return ptr.get() != p; }
152 template<
typename Object,
class RefCountable>
153 bool operator!=(Object* p,
const Ptr<RefCountable>& ptr) {
return p != ptr.get(); }
155 template<
class RefCountable>
156 bool operator==(
const Ptr<RefCountable>& ptr, std::nullptr_t) {
return !ptr; }
158 template<
class RefCountable>
159 bool operator==(std::nullptr_t,
const Ptr<RefCountable>& ptr) {
return !ptr; }
161 template<
class RefCountable>
162 bool operator!=(
const Ptr<RefCountable>& ptr, std::nullptr_t) {
return (
bool) ptr; }
164 template<
class RefCountable>
165 bool operator!=(std::nullptr_t,
const Ptr<RefCountable>& ptr) {
return (
bool) ptr; }
167 template<
typename RefCountable>
168 bool operator<(const Ptr<const RefCountable>& first,
const Ptr<const RefCountable>& second)
170 return first.get() < second.get();
173 template<
typename RefCountable>
174 bool operator<(const Ptr<const RefCountable>& refCountable, std::nullptr_t)
176 return refCountable.get() <
nullptr;
179 template<
typename RefCountable>
180 bool operator<(std::nullptr_t, const Ptr<RefCountable>& refCountable)
182 return nullptr < refCountable.get();
189 template<
class RefCountable>
190 static Ptr<RefCountable> shareToPtr(RefCountable* refCountable)
193 refCountable->addRef();
194 return Ptr(refCountable);
201 template<
class RefCountable>
202 static Ptr<RefCountable> shareToPtr(
const Ptr<RefCountable>& ptr)
210 template<
class RefCountable,
typename... Args>
211 static Ptr<RefCountable> makePtr(Args&&... args)
213 return Ptr(
new RefCountable(std::forward<Args>(args)...));
220 template<
class Interface,
class RefCountablePtr,
221 typename OldInterfaceId>
222 static Ptr<Interface> queryInterfaceOfOldSdk(
223 RefCountablePtr refCountable,
const OldInterfaceId& interfaceId)
226 ? Ptr(static_cast<Interface*>(refCountable->queryInterface(interfaceId)))
234 template<
class RefCountable>
235 int refCount(
const Ptr<RefCountable>& ptr)
237 return refCount(ptr.get());
Ptr(Ptr &&other)
Definition: ptr.h:44
RefCountable * releasePtr()
Definition: ptr.h:92
Ptr(std::nullptr_t=nullptr)
Definition: ptr.h:22
Ptr & operator=(Ptr &&other)
Definition: ptr.h:56
Ptr & operator=(const Ptr &other)
Definition: ptr.h:50
Definition: apple_utils.h:6
Ptr(const Ptr &other)
Definition: ptr.h:38
void reset(OtherRefCountable *ptr)
Definition: ptr.h:81
Definition: ref_countable.h:83
void reset()
Definition: ptr.h:70