15 template<
class RefCountable>
20 Ptr(std::nullptr_t =
nullptr) {}
23 template<
class OtherRefCountable>
25 std::enable_if_t<std::is_base_of<RefCountable, OtherRefCountable>::value,
int >;
29 template<
class OtherRefCountable, IsConvertibleFrom<OtherRefCountable> = 0>
30 explicit Ptr(OtherRefCountable* ptr): m_ptr(ptr) {}
32 template<
class OtherRefCountable, IsConvertibleFrom<OtherRefCountable> = 0>
33 Ptr(
const Ptr<OtherRefCountable>& other): m_ptr(other.get()) { addRef(); }
36 Ptr(
const Ptr& other): m_ptr(other.get()) { addRef(); }
38 template<
class OtherRefCountable, IsConvertibleFrom<OtherRefCountable> = 0>
44 template<
class OtherRefCountable, IsConvertibleFrom<OtherRefCountable> = 0>
50 template<
class OtherRefCountable, IsConvertibleFrom<OtherRefCountable> = 0>
56 ~
Ptr() { releaseRef(); }
58 template<
class OtherRefCountable, IsConvertibleFrom<OtherRefCountable> = 0>
59 bool operator==(
const Ptr<OtherRefCountable>& other)
const {
return m_ptr == other.get(); }
61 template<
class OtherRefCountable, IsConvertibleFrom<OtherRefCountable> = 0>
62 bool operator!=(
const Ptr<OtherRefCountable>& other)
const {
return !operator==(other); }
78 template<
class OtherRefCountable, IsConvertibleFrom<OtherRefCountable> = 0>
79 void reset(OtherRefCountable* ptr)
98 RefCountable* operator->()
const {
return m_ptr; }
100 operator bool()
const {
return m_ptr !=
nullptr; }
102 template<
class OtherRefCountable>
103 Ptr<OtherRefCountable> dynamicCast()
const 105 auto ptr =
dynamic_cast<OtherRefCountable*
>(m_ptr);
108 return Ptr<OtherRefCountable>(ptr);
124 Ptr& assignConst(
const Ptr& other)
126 if (
this != &other && m_ptr != other.get())
135 Ptr& assignRvalue(
Ptr&& other)
137 if (
this != &other && m_ptr != other.get())
140 m_ptr = other.releasePtr();
146 RefCountable* m_ptr =
nullptr;
149 template<
class RefCountable>
150 bool operator==(
const Ptr<RefCountable>& ptr, std::nullptr_t) {
return ! (bool) ptr; }
152 template<
class RefCountable>
153 bool operator==(std::nullptr_t,
const Ptr<RefCountable>& ptr) {
return ! (bool) ptr; }
155 template<
class RefCountable>
156 bool operator!=(
const Ptr<RefCountable>& ptr, std::nullptr_t) {
return (
bool) ptr; }
158 template<
class RefCountable>
159 bool operator!=(std::nullptr_t,
const Ptr<RefCountable>& ptr) {
return (
bool) ptr; }
167 template<
class RefCountable>
168 static Ptr<RefCountable> toPtr(RefCountable* refCountable)
170 return Ptr<RefCountable>(refCountable);
176 template<
class RefCountable,
typename... Args>
177 static Ptr<RefCountable> makePtr(Args&&... args)
179 return Ptr<RefCountable>{
new RefCountable(std::forward<Args>(args)...)};
185 template<
class Interface,
class RefCountablePtr>
186 static Ptr<Interface> queryInterfacePtr(RefCountablePtr refCountable)
188 return Ptr<Interface>(
189 static_cast<Interface*
>(refCountable->queryInterface(Interface::interfaceId())));
195 template<
class Interface,
class RefCountablePtr,
196 typename InterfaceId>
197 static Ptr<Interface> queryInterfacePtr(
198 RefCountablePtr refCountable,
const InterfaceId& interfaceId)
200 return Ptr<Interface>(
201 static_cast<Interface*
>(refCountable->queryInterface(interfaceId)));
208 template<
class RefCountable>
209 int refCount(
const Ptr<RefCountable>& ptr)
211 return refCount(ptr.get());
Ptr(Ptr &&other)
Definition: ptr.h:42
RefCountable * releasePtr()
Definition: ptr.h:90
Ptr(std::nullptr_t=nullptr)
Definition: ptr.h:20
Ptr & operator=(Ptr &&other)
Definition: ptr.h:54
Ptr & operator=(const Ptr &other)
Definition: ptr.h:48
Ptr(const Ptr &other)
Definition: ptr.h:36
void reset(OtherRefCountable *ptr)
Definition: ptr.h:79
std::enable_if_t< std::is_base_of< RefCountable, OtherRefCountable >::value, int > IsConvertibleFrom
Definition: ptr.h:25
Definition: ref_countable.h:79
void reset()
Definition: ptr.h:68