Krita Source Code Documentation
Loading...
Searching...
No Matches
KisSharedPtr< T > Class Template Reference

#include <kis_shared_ptr.h>

+ Inheritance diagram for KisSharedPtr< T >:

Public Types

using element_type = T
 
using weak_type = KisWeakSharedPtr<T>
 

Public Member Functions

void attach (T *p)
 
void clear ()
 
const T * constData () const
 
T * data ()
 
const T * data () const
 
bool isNull () const
 
 KisSharedPtr ()
 
 KisSharedPtr (const KisSharedPtr< T > &o)
 
 KisSharedPtr (const KisWeakSharedPtr< T > &o)
 
 KisSharedPtr (T *p)
 
 operator const T * () const
 
template<class T2 >
 operator KisSharedPtr< T2 > () const
 
bool operator!= (const KisSharedPtr &o) const
 
bool operator!= (const T *p) const
 
T & operator* ()
 
const T & operator* () const
 
T * operator-> ()
 
const T * operator-> () const
 
KisSharedPtroperator= (const KisSharedPtr &o)
 
KisSharedPtr< T > & operator= (T *p)
 
bool operator== (const KisSharedPtr &o) const
 
bool operator== (const T *p) const
 
 ~KisSharedPtr ()
 

Static Public Member Functions

static bool deref (const KisSharedPtr< T > *sp, T *t)
 
static void ref (const KisSharedPtr< T > *sp, T *t)
 

Private Member Functions

void deref () const
 
void ref () const
 

Private Attributes

T * d
 

Friends

class KisWeakSharedPtr< T >
 

Detailed Description

template<class T>
class KisSharedPtr< T >

KisSharedPtr is a shared pointer similar to KSharedPtr and boost::shared_ptr. The difference with KSharedPtr is that our constructor is not explicit.

A shared pointer is a wrapper around a real pointer. The shared pointer keeps a reference count, and when the reference count drops to 0 the contained pointer is deleted. You can use the shared pointer just as you would use a real pointer.

See also item 28 and 29 of More Effective C++ and https://bugs.kde.org/show_bug.cgi?id=52261 as well as https://www.boost.org/libs/smart_ptr/shared_ptr.htm.

Advantage of KisSharedPtr over boost pointer or QSharedPointer?

The difference with boost share pointer is that in boost::shared_ptr, the counter is kept inside the smart pointer, meaning that you should never never remove the pointer from its smart pointer object, because if you do that, and somewhere in the code, the pointer is put back in a smart pointer, then you have two counters, and when one reach zero, then the object gets deleted while some other code thinks the pointer is still valid.

Disadvantage of KisSharedPtr compared to boost pointer?

KisSharedPtr requires the class to inherits KisShared.

Difference with QSharedPointer

QSharedPointer and KisSharedPtr are very similar, but QSharedPointer has an explicit constructor which makes it more painful to use in some constructions. And QSharedPointer doesn't offer a weak pointer.

Definition at line 56 of file kis_shared_ptr.h.

Member Typedef Documentation

◆ element_type

template<class T >
using KisSharedPtr< T >::element_type = T

Definition at line 60 of file kis_shared_ptr.h.

◆ weak_type

template<class T >
using KisSharedPtr< T >::weak_type = KisWeakSharedPtr<T>

Definition at line 61 of file kis_shared_ptr.h.

Constructor & Destructor Documentation

◆ KisSharedPtr() [1/4]

template<class T >
KisSharedPtr< T >::KisSharedPtr ( )
inline

Creates a null pointer.

Definition at line 66 of file kis_shared_ptr.h.

67 : d(0) { }

◆ KisSharedPtr() [2/4]

template<class T >
KisSharedPtr< T >::KisSharedPtr ( T * p)
inline

Creates a new pointer.

Parameters
pthe pointer

Definition at line 73 of file kis_shared_ptr.h.

74 : d(p) {
75 ref();
76 }
const Params2D p
void ref() const

References KisSharedPtr< T >::ref().

◆ KisSharedPtr() [3/4]

template<class T >
Q_INLINE_TEMPLATE KisSharedPtr< T >::KisSharedPtr ( const KisWeakSharedPtr< T > & o)
inline

Thread safety: Is the object we have just referenced still valid?

Definition at line 484 of file kis_shared_ptr.h.

485 : d(o.d)
486{
487 if (o.isValid()) {
488 ref();
489
494 Q_ASSERT(o.isConsistent());
495 }
496 else {
497 d = 0;
498 }
499}

References KisSharedPtr< T >::d, and KisSharedPtr< T >::ref().

◆ KisSharedPtr() [4/4]

template<class T >
KisSharedPtr< T >::KisSharedPtr ( const KisSharedPtr< T > & o)
inline

Copies a pointer.

Parameters
othe pointer to copy

Definition at line 90 of file kis_shared_ptr.h.

91 : d(o.d) {
92 ref();
93 }

References KisSharedPtr< T >::ref().

◆ ~KisSharedPtr()

template<class T >
KisSharedPtr< T >::~KisSharedPtr ( )
inline

Dereferences the object that this pointer points to. If it was the last reference, the object will be deleted.

Definition at line 99 of file kis_shared_ptr.h.

99 {
100 deref();
101 }
void deref() const

References KisSharedPtr< T >::deref().

Member Function Documentation

◆ attach()

template<class T >
Q_INLINE_TEMPLATE void KisSharedPtr< T >::attach ( T * p)

Definition at line 503 of file kis_shared_ptr.h.

504{
505 if (d != p) {
506 ref(this, p);
507 T* old = d;
508 d = p;
509 deref(this, old);
510 }
511}

References KisSharedPtr< T >::d, KisSharedPtr< T >::deref(), p, and KisSharedPtr< T >::ref().

◆ clear()

template<class T >
Q_INLINE_TEMPLATE void KisSharedPtr< T >::clear ( )

Definition at line 514 of file kis_shared_ptr.h.

515{
516 attach((T*)0);
517}
void attach(T *p)

References KisSharedPtr< T >::attach().

◆ constData()

template<class T >
const T * KisSharedPtr< T >::constData ( ) const
inline
Returns
a const pointer to the shared object.

Definition at line 153 of file kis_shared_ptr.h.

153 {
154 return d;
155 }

References KisSharedPtr< T >::d.

◆ data() [1/2]

template<class T >
T * KisSharedPtr< T >::data ( )
inline
Returns
the contained pointer. If you delete the contained pointer, you will make KisSharedPtr very unhappy. It is perfectly safe to put the contained pointer in another KisSharedPtr, though.

Definition at line 139 of file kis_shared_ptr.h.

139 {
140 return d;
141 }

References KisSharedPtr< T >::d.

◆ data() [2/2]

template<class T >
const T * KisSharedPtr< T >::data ( ) const
inline
Returns
the pointer

Definition at line 146 of file kis_shared_ptr.h.

146 {
147 return d;
148 }

References KisSharedPtr< T >::d.

◆ deref() [1/2]

template<class T >
void KisSharedPtr< T >::deref ( ) const
inlineprivate

Definition at line 214 of file kis_shared_ptr.h.

215 {
216 bool v = deref(this, d);
217#ifndef NDEBUG
218 if (!v) {
219 d = 0;
220 }
221#else
222 Q_UNUSED(v);
223#endif
224 }
qreal v

References KisSharedPtr< T >::d, KisSharedPtr< T >::deref(), and v.

◆ deref() [2/2]

template<class T >
static bool KisSharedPtr< T >::deref ( const KisSharedPtr< T > * sp,
T * t )
inlinestatic

Definition at line 194 of file kis_shared_ptr.h.

195 {
196#ifndef HAVE_MEMORY_LEAK_TRACKER
197 Q_UNUSED(sp);
198#else
200#endif
201 if (t && !t->deref()) {
202 delete t;
203 return false;
204 }
205 return true;
206 }
static KisMemoryLeakTracker * instance()
void dereference(const void *what, const void *bywho)

References KisMemoryLeakTracker::dereference(), and KisMemoryLeakTracker::instance().

◆ isNull()

template<class T >
bool KisSharedPtr< T >::isNull ( ) const
inline
Returns
true if the pointer is null

Definition at line 178 of file kis_shared_ptr.h.

178 {
179 return (d == 0);
180 }

References KisSharedPtr< T >::d.

◆ operator const T *()

template<class T >
KisSharedPtr< T >::operator const T * ( ) const
inline

Definition at line 125 of file kis_shared_ptr.h.

125 {
126 return d;
127 }

◆ operator KisSharedPtr< T2 >()

template<class T >
template<class T2 >
KisSharedPtr< T >::operator KisSharedPtr< T2 > ( ) const
inline

Definition at line 129 of file kis_shared_ptr.h.

129 {
130 return KisSharedPtr<T2>(d);
131 }

References KisSharedPtr< T >::d.

◆ operator!=() [1/2]

template<class T >
bool KisSharedPtr< T >::operator!= ( const KisSharedPtr< T > & o) const
inline

Definition at line 116 of file kis_shared_ptr.h.

116 {
117 return (d != o.d);
118 }

References KisSharedPtr< T >::d.

◆ operator!=() [2/2]

template<class T >
bool KisSharedPtr< T >::operator!= ( const T * p) const
inline

Definition at line 110 of file kis_shared_ptr.h.

110 {
111 return (d != p);
112 }

References KisSharedPtr< T >::d, and p.

◆ operator*() [1/2]

template<class T >
T & KisSharedPtr< T >::operator* ( )
inline

Definition at line 161 of file kis_shared_ptr.h.

161 {
162 Q_ASSERT(d);
163 return *d;
164 }

References KisSharedPtr< T >::d.

◆ operator*() [2/2]

template<class T >
const T & KisSharedPtr< T >::operator* ( ) const
inline

Definition at line 157 of file kis_shared_ptr.h.

157 {
158 Q_ASSERT(d);
159 return *d;
160 }

References KisSharedPtr< T >::d.

◆ operator->() [1/2]

template<class T >
T * KisSharedPtr< T >::operator-> ( )
inline

Definition at line 170 of file kis_shared_ptr.h.

170 {
171 Q_ASSERT(d);
172 return d;
173 }

References KisSharedPtr< T >::d.

◆ operator->() [2/2]

template<class T >
const T * KisSharedPtr< T >::operator-> ( ) const
inline

Definition at line 166 of file kis_shared_ptr.h.

166 {
167 Q_ASSERT(d);
168 return d;
169 }

References KisSharedPtr< T >::d.

◆ operator=() [1/2]

template<class T >
KisSharedPtr & KisSharedPtr< T >::operator= ( const KisSharedPtr< T > & o)
inline

Definition at line 103 of file kis_shared_ptr.h.

103 {
104 attach(o.d);
105 return *this;
106 }

References KisSharedPtr< T >::attach().

◆ operator=() [2/2]

template<class T >
KisSharedPtr< T > & KisSharedPtr< T >::operator= ( T * p)
inline

Definition at line 120 of file kis_shared_ptr.h.

120 {
121 attach(p);
122 return *this;
123 }

References KisSharedPtr< T >::attach(), and p.

◆ operator==() [1/2]

template<class T >
bool KisSharedPtr< T >::operator== ( const KisSharedPtr< T > & o) const
inline

Definition at line 113 of file kis_shared_ptr.h.

113 {
114 return (d == o.d);
115 }

References KisSharedPtr< T >::d.

◆ operator==() [2/2]

template<class T >
bool KisSharedPtr< T >::operator== ( const T * p) const
inline

Definition at line 107 of file kis_shared_ptr.h.

107 {
108 return (d == p);
109 }

References KisSharedPtr< T >::d, and p.

◆ ref() [1/2]

template<class T >
void KisSharedPtr< T >::ref ( ) const
inlineprivate

Definition at line 209 of file kis_shared_ptr.h.

210 {
211 ref(this, d);
212 }

References KisSharedPtr< T >::d, and KisSharedPtr< T >::ref().

◆ ref() [2/2]

template<class T >
static void KisSharedPtr< T >::ref ( const KisSharedPtr< T > * sp,
T * t )
inlinestatic

Definition at line 182 of file kis_shared_ptr.h.

183 {
184#ifndef HAVE_MEMORY_LEAK_TRACKER
185 Q_UNUSED(sp);
186#else
188#endif
189 if (t) {
190 t->ref();
191 }
192 }
void reference(const void *what, const void *bywho, const char *whatName=0)

References KisMemoryLeakTracker::instance(), and KisMemoryLeakTracker::reference().

Friends And Related Symbol Documentation

◆ KisWeakSharedPtr< T >

template<class T >
friend class KisWeakSharedPtr< T >
friend

Definition at line 514 of file kis_shared_ptr.h.

Member Data Documentation

◆ d

template<class T >
T* KisSharedPtr< T >::d
mutableprivate

Definition at line 227 of file kis_shared_ptr.h.


The documentation for this class was generated from the following files: