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>
44 Ptr(
Ptr&& other) noexcept: m_ptr(other.releasePtr()) {}
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 First,
typename Second>
168 bool operator<(const Ptr<First>& first,
const Ptr<Second>& second)
170 return first.get() < second.get();
173 template<
typename RefCountable>
174 bool operator<(const Ptr<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();
185 template<
typename First,
typename Second>
186 bool operator<(const Ptr<First>& first,
const Second* second)
188 return first.get() < second;
191 template<
typename First,
typename Second>
192 bool operator<(const First* first, const Ptr<Second>& second)
194 return first < second.get();
201 template<
class RefCountable>
202 static Ptr<RefCountable> shareToPtr(RefCountable* refCountable)
205 refCountable->addRef();
206 return Ptr(refCountable);
213 template<
class RefCountable>
214 static Ptr<RefCountable> shareToPtr(
const Ptr<RefCountable>& ptr)
222 template<
class RefCountable,
typename... Args>
223 static Ptr<RefCountable> makePtr(Args&&... args)
225 return Ptr(
new RefCountable(std::forward<Args>(args)...));
232 template<
class NewRefCountable,
class OldRefCountable>
233 static Ptr<NewRefCountable> reinterpretPtr(
const Ptr<OldRefCountable>& ptr)
235 return shareToPtr(reinterpret_cast<NewRefCountable*>(ptr.get()));
242 template<
class Interface,
class RefCountablePtr,
243 typename OldInterfaceId>
244 static Ptr<Interface> queryInterfaceOfOldSdk(
245 RefCountablePtr refCountable,
const OldInterfaceId& interfaceId)
248 ? Ptr(static_cast<Interface*>(refCountable->queryInterface(interfaceId)))
256 template<
class RefCountable>
257 static int refCount(
const Ptr<RefCountable>& ptr)
259 return refCount(ptr.get());
RefCountable * releasePtr()
Definition: ptr.h:92
Ptr(std::nullptr_t=nullptr)
Definition: ptr.h:22
Ptr & operator=(const Ptr &other)
Definition: ptr.h:50
Definition: apple_utils.h:6
Ptr & operator=(Ptr &&other) noexcept
Definition: ptr.h:56
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
Ptr(Ptr &&other) noexcept
Definition: ptr.h:44