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

#include <kis_shared_ptr.h>

Public Types

using element_type = T
 

Public Member Functions

const T * constData () const
 
T * data ()
 
const T * data () const
 
bool isNull () const
 
bool isValid () const
 
 KisWeakSharedPtr ()
 
 KisWeakSharedPtr (const KisSharedPtr< T > &o)
 
 KisWeakSharedPtr (const KisWeakSharedPtr< T > &o)
 
 KisWeakSharedPtr (T *p)
 
 operator const T * () const
 
template<class T2 >
 operator KisWeakSharedPtr< T2 > () const
 
bool operator!= (const KisWeakSharedPtr &o) const
 
bool operator!= (const T *p) const
 
T & operator* ()
 
const T & operator* () const
 
T * operator-> ()
 
const T * operator-> () const
 
KisWeakSharedPtroperator= (const KisWeakSharedPtr &o)
 
KisWeakSharedPtr< T > & operator= (T *p)
 
bool operator== (const KisWeakSharedPtr &o) const
 
bool operator== (const T *p) const
 
KisSharedPtr< T > toStrongRef () const
 toStrongRef returns a KisSharedPtr which may be dereferenced.
 
 ~KisWeakSharedPtr ()
 

Private Member Functions

void attach (const KisWeakSharedPtr &o)
 
void attach (T *newValue)
 
void detach ()
 
bool isConsistent () const
 
void load (T *newValue)
 

Static Private Member Functions

static bool isOdd (const qint32 &x)
 

Private Attributes

T * d
 
QAtomicInt * weakReference
 

Static Private Attributes

static const qint32 WEAK_REF = 2
 

Friends

class KisSharedPtr< T >
 

Detailed Description

template<class T>
class KisWeakSharedPtr< T >

A weak shared ptr is an ordinary shared ptr, with two differences: it doesn't delete the contained pointer if the refcount drops to zero and it doesn't prevent the contained pointer from being deleted if the last strong shared pointer goes out of scope.

Definition at line 237 of file kis_shared_ptr.h.

Member Typedef Documentation

◆ element_type

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

Definition at line 241 of file kis_shared_ptr.h.

Constructor & Destructor Documentation

◆ KisWeakSharedPtr() [1/4]

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

Creates a null pointer.

Definition at line 246 of file kis_shared_ptr.h.

247 : d(0), weakReference(0) { }
QAtomicInt * weakReference

◆ KisWeakSharedPtr() [2/4]

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

Creates a new pointer.

Parameters
pthe pointer

Definition at line 253 of file kis_shared_ptr.h.

253 {
254 load(p);
255 }
const Params2D p
void load(T *newValue)

References KisWeakSharedPtr< T >::load(), and p.

◆ KisWeakSharedPtr() [3/4]

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

Definition at line 257 of file kis_shared_ptr.h.

257 {
258 load(o.d);
259 }

References KisWeakSharedPtr< T >::load().

◆ KisWeakSharedPtr() [4/4]

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

Copies a pointer.

Parameters
othe pointer to copy

Definition at line 265 of file kis_shared_ptr.h.

265 {
266 if (o.isConsistent()) {
267 load(o.d);
268 }
269 else {
270 d = 0;
271 weakReference = 0;
272 }
273 }

References KisWeakSharedPtr< T >::d, KisWeakSharedPtr< T >::load(), and KisWeakSharedPtr< T >::weakReference.

◆ ~KisWeakSharedPtr()

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

Definition at line 275 of file kis_shared_ptr.h.

275 {
276 detach();
277 }

References KisWeakSharedPtr< T >::detach().

Member Function Documentation

◆ attach() [1/2]

template<class T >
void KisWeakSharedPtr< T >::attach ( const KisWeakSharedPtr< T > & o)
inlineprivate

Definition at line 452 of file kis_shared_ptr.h.

452 {
453 detach();
454 if (o.isConsistent()) {
455 load(o.d);
456 }
457 else {
458 d = 0;
459 weakReference = 0;
460 }
461 }

References KisWeakSharedPtr< T >::d, KisWeakSharedPtr< T >::detach(), KisWeakSharedPtr< T >::load(), and KisWeakSharedPtr< T >::weakReference.

◆ attach() [2/2]

template<class T >
void KisWeakSharedPtr< T >::attach ( T * newValue)
inlineprivate

Definition at line 447 of file kis_shared_ptr.h.

447 {
448 detach();
449 load(newValue);
450 }

References KisWeakSharedPtr< T >::detach(), and KisWeakSharedPtr< T >::load().

◆ constData()

template<class T >
const T * KisWeakSharedPtr< T >::constData ( ) const
inline
See also
data()

Definition at line 336 of file kis_shared_ptr.h.

336 {
337 if (!isConsistent()) {
338 warnKrita.noquote() << kisBacktrace();
339 Q_ASSERT_X(0, "KisWeakSharedPtr", "Weak pointer is not valid!");
340 }
341
342 return d;
343 }
bool isConsistent() const
QString kisBacktrace()
Definition kis_debug.cpp:51
#define warnKrita
Definition kis_debug.h:87

References KisWeakSharedPtr< T >::d, KisWeakSharedPtr< T >::isConsistent(), kisBacktrace(), and warnKrita.

◆ data() [1/2]

template<class T >
T * KisWeakSharedPtr< T >::data ( )
inline

Note that if you use this function, the pointer might be destroyed if KisSharedPtr pointing to this pointer are deleted, resulting in a segmentation fault. Use with care.

Returns
a const pointer to the shared object.

Definition at line 312 of file kis_shared_ptr.h.

312 {
313 if (!isConsistent()) {
314 warnKrita.noquote() << kisBacktrace();
315 Q_ASSERT_X(0, "KisWeakSharedPtr", "Weak pointer is not valid!");
316 }
317
318 return d;
319 }

References KisWeakSharedPtr< T >::d, KisWeakSharedPtr< T >::isConsistent(), kisBacktrace(), and warnKrita.

◆ data() [2/2]

template<class T >
const T * KisWeakSharedPtr< T >::data ( ) const
inline
See also
data()

Definition at line 324 of file kis_shared_ptr.h.

324 {
325 if (!isConsistent()) {
326 warnKrita.noquote() << kisBacktrace();
327 Q_ASSERT_X(0, "KisWeakSharedPtr", "Weak pointer is not valid!");
328 }
329
330 return d;
331 }

References KisWeakSharedPtr< T >::d, KisWeakSharedPtr< T >::isConsistent(), kisBacktrace(), and warnKrita.

◆ detach()

template<class T >
void KisWeakSharedPtr< T >::detach ( )
inlineprivate

Definition at line 464 of file kis_shared_ptr.h.

464 {
465 d = 0;
466
467 if (weakReference &&
468 weakReference->fetchAndAddOrdered(-WEAK_REF) <= WEAK_REF) {
469
470 // sanity check:
471 Q_ASSERT((int)*weakReference == 0);
472
473 delete weakReference;
474 weakReference = 0;
475 }
476 }
static const qint32 WEAK_REF

References KisWeakSharedPtr< T >::d, KisWeakSharedPtr< T >::WEAK_REF, and KisWeakSharedPtr< T >::weakReference.

◆ isConsistent()

template<class T >
bool KisWeakSharedPtr< T >::isConsistent ( ) const
inlineprivate

Definition at line 428 of file kis_shared_ptr.h.

428 {
429 Q_ASSERT(!d || weakReference);
430
431 return !d || (weakReference && isOdd((int)*weakReference));
432 }
#define isOdd(x)

References KisWeakSharedPtr< T >::d, isOdd, and KisWeakSharedPtr< T >::weakReference.

◆ isNull()

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

Definition at line 396 of file kis_shared_ptr.h.

396 {
397 return (d == 0);
398 }

References KisWeakSharedPtr< T >::d.

◆ isOdd()

template<class T >
static bool KisWeakSharedPtr< T >::isOdd ( const qint32 & x)
inlinestaticprivate

Definition at line 424 of file kis_shared_ptr.h.

424 {
425 return x & 0x01;
426 }

◆ isValid()

template<class T >
bool KisWeakSharedPtr< T >::isValid ( ) const
inline
Returns
true if the weak pointer points to a valid pointer and false if the data has been deleted or is null

Definition at line 404 of file kis_shared_ptr.h.

404 {
405 Q_ASSERT(!d || weakReference);
406
407 return d && weakReference && isOdd((int)*weakReference);
408 }

References KisWeakSharedPtr< T >::d, isOdd, and KisWeakSharedPtr< T >::weakReference.

◆ load()

template<class T >
void KisWeakSharedPtr< T >::load ( T * newValue)
inlineprivate

Definition at line 434 of file kis_shared_ptr.h.

434 {
435 d = newValue;
436
437 if (d) {
438 weakReference = d->sharedWeakReference();
439 weakReference->fetchAndAddOrdered(WEAK_REF);
440 }
441 else {
442 weakReference = 0;
443 }
444 }

References KisWeakSharedPtr< T >::d, KisWeakSharedPtr< T >::WEAK_REF, and KisWeakSharedPtr< T >::weakReference.

◆ operator const T *()

template<class T >
KisWeakSharedPtr< T >::operator const T * ( ) const
inline
See also
data()

This operator is used in boolean expressions where we check for pointer consistency, so return 0 instead of asserting.

Definition at line 348 of file kis_shared_ptr.h.

348 {
354 return isConsistent() ? d : 0;
355 }

◆ operator KisWeakSharedPtr< T2 >()

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

Definition at line 302 of file kis_shared_ptr.h.

302 {
303 return KisWeakSharedPtr<T2>(d);
304 }

References KisWeakSharedPtr< T >::d.

◆ operator!=() [1/2]

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

Definition at line 293 of file kis_shared_ptr.h.

293 {
294 return (d != o.d);
295 }

References KisWeakSharedPtr< T >::d.

◆ operator!=() [2/2]

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

Definition at line 287 of file kis_shared_ptr.h.

287 {
288 return (d != p);
289 }

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

◆ operator*() [1/2]

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

Definition at line 366 of file kis_shared_ptr.h.

366 {
367 if (!isValid()) {
368 warnKrita.noquote() << kisBacktrace();
369 Q_ASSERT_X(0, "KisWeakSharedPtr", "Weak pointer is not valid!");
370 }
371
372 return *d;
373 }
bool isValid() const

References KisWeakSharedPtr< T >::d, KisWeakSharedPtr< T >::isValid(), kisBacktrace(), and warnKrita.

◆ operator*() [2/2]

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

Definition at line 357 of file kis_shared_ptr.h.

357 {
358 if (!isValid()) {
359 warnKrita.noquote() << kisBacktrace();
360 Q_ASSERT_X(0, "KisWeakSharedPtr", "Weak pointer is not valid!");
361 }
362
363 return *d;
364 }

References KisWeakSharedPtr< T >::d, KisWeakSharedPtr< T >::isValid(), kisBacktrace(), and warnKrita.

◆ operator->() [1/2]

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

Definition at line 384 of file kis_shared_ptr.h.

384 {
385 if (!isValid()) {
386 warnKrita.noquote() << kisBacktrace();
387 Q_ASSERT_X(0, "KisWeakSharedPtr", "Weak pointer is not valid!");
388 }
389
390 return d;
391 }

References KisWeakSharedPtr< T >::d, KisWeakSharedPtr< T >::isValid(), kisBacktrace(), and warnKrita.

◆ operator->() [2/2]

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

Definition at line 375 of file kis_shared_ptr.h.

375 {
376 if (!isValid()) {
377 warnKrita.noquote() << kisBacktrace();
378 Q_ASSERT_X(0, "KisWeakSharedPtr", "Weak pointer is not valid!");
379 }
380
381 return d;
382 }

References KisWeakSharedPtr< T >::d, KisWeakSharedPtr< T >::isValid(), kisBacktrace(), and warnKrita.

◆ operator=() [1/2]

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

Definition at line 279 of file kis_shared_ptr.h.

279 {
280 attach(o);
281 return *this;
282 }
void attach(T *newValue)

References KisWeakSharedPtr< T >::attach().

◆ operator=() [2/2]

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

Definition at line 297 of file kis_shared_ptr.h.

297 {
298 attach(p);
299 return *this;
300 }

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

◆ operator==() [1/2]

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

Definition at line 290 of file kis_shared_ptr.h.

290 {
291 return (d == o.d);
292 }

References KisWeakSharedPtr< T >::d.

◆ operator==() [2/2]

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

Definition at line 284 of file kis_shared_ptr.h.

284 {
285 return (d == p);
286 }

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

◆ toStrongRef()

template<class T >
KisSharedPtr< T > KisWeakSharedPtr< T >::toStrongRef ( ) const
inline

toStrongRef returns a KisSharedPtr which may be dereferenced.

Weak pointers should only be used to track ownership but never be used as pointers. This has historically not been the case, but in new API this function should be used instead of directly using a weak pointer as pointer.

Returns
a KisSharedPtr, which may be null

Definition at line 418 of file kis_shared_ptr.h.

418 {
419 return KisSharedPtr<T>(*this);
420 }
friend class KisSharedPtr< T >

References KisWeakSharedPtr< T >::KisSharedPtr< T >.

Friends And Related Symbol Documentation

◆ KisSharedPtr< T >

template<class T >
friend class KisSharedPtr< T >
friend

Definition at line 227 of file kis_shared_ptr.h.

Member Data Documentation

◆ d

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

Definition at line 478 of file kis_shared_ptr.h.

◆ WEAK_REF

template<class T >
const qint32 KisWeakSharedPtr< T >::WEAK_REF = 2
staticprivate

Definition at line 423 of file kis_shared_ptr.h.

◆ weakReference

template<class T >
QAtomicInt* KisWeakSharedPtr< T >::weakReference
private

Definition at line 479 of file kis_shared_ptr.h.


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