• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • util
ksharedptr.h
Go to the documentation of this file.
1/*
2 * This file is part of the KDE libraries.
3 *
4 * Copyright 2005 Frerich Raabe <raabe@kde.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27#ifndef KSHAREDPTR_H
28#define KSHAREDPTR_H
29
30#include <QtCore/QExplicitlySharedDataPointer>
31#include <QtCore/QAtomicPointer>
32#include <kdemacros.h>
33
38typedef QSharedData KShared;
39
62template< class T >
63class KSharedPtr
64{
65public:
69 inline KSharedPtr()
70 : d(0) { }
71
76 inline explicit KSharedPtr( T* p )
77 : d(p) { if(d) d->ref.ref(); }
78
83 inline KSharedPtr( const KSharedPtr& o )
84 : d(o.d) { if(d) d->ref.ref(); }
85
90 inline ~KSharedPtr() { if (d && !d->ref.deref()) delete d; }
91
92 inline KSharedPtr<T>& operator= ( const KSharedPtr& o ) { attach(o.d); return *this; }
93 inline bool operator== ( const KSharedPtr& o ) const { return ( d == o.d ); }
94 inline bool operator!= ( const KSharedPtr& o ) const { return ( d != o.d ); }
95 inline bool operator< ( const KSharedPtr& o ) const { return ( d < o.d ); }
96
97 inline KSharedPtr<T>& operator= ( T* p ) { attach(p); return *this; }
98 inline bool operator== ( const T* p ) const { return ( d == p ); }
99 inline bool operator!= ( const T* p ) const { return ( d != p ); }
100
106 inline operator bool() const { return ( d != 0 ); }
107
111 inline T* data() { return d; }
112
116 inline const T* data() const { return d; }
117
121 inline const T* constData() const { return d; }
122
123 inline const T& operator*() const { Q_ASSERT(d); return *d; }
124 inline T& operator*() { Q_ASSERT(d); return *d; }
125 inline const T* operator->() const { Q_ASSERT(d); return d; }
126 inline T* operator->() { Q_ASSERT(d); return d; }
127
133 void attach(T* p);
134
138 void clear();
139
144 inline int count() const { return d ? static_cast<int>(d->ref) : 0; } // for debugging purposes
145
151 inline bool isNull() const { return (d == 0); }
152
158 inline bool isUnique() const { return count() == 1; }
159
160 template <class U> friend class KSharedPtr;
161
172 template <class U>
173 static KSharedPtr<T> staticCast( const KSharedPtr<U>& o ) {
174 return KSharedPtr<T>( static_cast<T *>( o.d ) );
175 }
187 template <class U>
188 static KSharedPtr<T> dynamicCast( const KSharedPtr<U>& o ) {
189 return KSharedPtr<T>( dynamic_cast<T *>( o.d ) );
190 }
191
192protected:
193 T* d;
194};
195
196template <class T>
197Q_INLINE_TEMPLATE bool operator== (const T* p, const KSharedPtr<T>& o)
198{
199 return ( o == p );
200}
201
202template <class T>
203Q_INLINE_TEMPLATE bool operator!= (const T* p, const KSharedPtr<T>& o)
204{
205 return ( o != p );
206}
207
208template <class T>
209Q_INLINE_TEMPLATE void KSharedPtr<T>::attach(T* p)
210{
211 if (d != p) {
212 if (p) p->ref.ref();
213 if (d && !d->ref.deref())
214 delete d;
215 d = p;
216 }
217}
218
219template <class T>
220Q_INLINE_TEMPLATE void KSharedPtr<T>::clear()
221{
222 attach(static_cast<T*>(0));
223}
224
225#endif
226
KSharedPtr
Can be used to control the lifetime of an object that has derived QSharedData.
Definition: ksharedptr.h:64
KSharedPtr::KSharedPtr
KSharedPtr()
Creates a null pointer.
Definition: ksharedptr.h:69
KSharedPtr::constData
const T * constData() const
Definition: ksharedptr.h:121
KSharedPtr::KSharedPtr
KSharedPtr(const KSharedPtr &o)
Copies a pointer.
Definition: ksharedptr.h:83
KSharedPtr::operator->
T * operator->()
Definition: ksharedptr.h:126
KSharedPtr::operator*
const T & operator*() const
Definition: ksharedptr.h:123
KSharedPtr::clear
void clear()
Clear the pointer, i.e.
Definition: ksharedptr.h:220
KSharedPtr::staticCast
static KSharedPtr< T > staticCast(const KSharedPtr< U > &o)
Convert KSharedPtr to KSharedPtr<T>, using a static_cast.
Definition: ksharedptr.h:173
KSharedPtr::KSharedPtr
KSharedPtr(T *p)
Creates a new pointer.
Definition: ksharedptr.h:76
KSharedPtr::data
T * data()
Definition: ksharedptr.h:111
KSharedPtr::dynamicCast
static KSharedPtr< T > dynamicCast(const KSharedPtr< U > &o)
Convert KSharedPtr to KSharedPtr<T>, using a dynamic_cast.
Definition: ksharedptr.h:188
KSharedPtr::data
const T * data() const
Definition: ksharedptr.h:116
KSharedPtr::operator*
T & operator*()
Definition: ksharedptr.h:124
KSharedPtr::d
T * d
Definition: ksharedptr.h:193
KSharedPtr::operator->
const T * operator->() const
Definition: ksharedptr.h:125
KSharedPtr::isNull
bool isNull() const
Test if the shared pointer is null.
Definition: ksharedptr.h:151
KSharedPtr::~KSharedPtr
~KSharedPtr()
Unreferences the object that this pointer points to.
Definition: ksharedptr.h:90
KSharedPtr::attach
void attach(T *p)
Attach the given pointer to the current KSharedPtr.
Definition: ksharedptr.h:209
KSharedPtr::isUnique
bool isUnique() const
Definition: ksharedptr.h:158
KSharedPtr::count
int count() const
Returns the number of references.
Definition: ksharedptr.h:144
bool
operator==
bool operator==(const KEntry &k1, const KEntry &k2)
Definition: kconfigdata.h:72
operator<
bool operator<(const KEntryKey &k1, const KEntryKey &k2)
Compares two KEntryKeys (needed for QMap).
Definition: kconfigdata.h:124
operator!=
bool operator!=(const KEntry &k1, const KEntry &k2)
Definition: kconfigdata.h:79
operator==
Q_INLINE_TEMPLATE bool operator==(const T *p, const KSharedPtr< T > &o)
Definition: ksharedptr.h:197
operator!=
Q_INLINE_TEMPLATE bool operator!=(const T *p, const KSharedPtr< T > &o)
Definition: ksharedptr.h:203
KShared
QSharedData KShared
Definition: ksharedptr.h:38
T
#define T
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal