• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.3 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 
38 typedef QSharedData KShared;
39 
62 template< class T >
63 class KSharedPtr
64 {
65 public:
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 
192 protected:
193  T* d;
194 };
195 
196 template <class T>
197 Q_INLINE_TEMPLATE bool operator== (const T* p, const KSharedPtr<T>& o)
198 {
199  return ( o == p );
200 }
201 
202 template <class T>
203 Q_INLINE_TEMPLATE bool operator!= (const T* p, const KSharedPtr<T>& o)
204 {
205  return ( o != p );
206 }
207 
208 template <class T>
209 Q_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 
219 template <class T>
220 Q_INLINE_TEMPLATE void KSharedPtr<T>::clear()
221 {
222  attach(static_cast<T*>(0));
223 }
224 
225 #endif
226 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat May 18 2013 11:36:11 by doxygen 1.8.3.1 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.10.3 API Reference

Skip menu "kdelibs-4.10.3 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