• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kdecore
 

kdecore

  • kdecore
ksharedptr.h
1 /* This file is part of the KDE libraries
2  Copyright (c) 1999 Waldo Bastian <bastian@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 #ifndef KSharedPTR_H
19 #define KSharedPTR_H
20 
21 #include "kdelibs_export.h"
22 
40 class KDECORE_EXPORT KShared {
41 public:
46  KShared() : count(0) { }
47 
52  KShared( const KShared & ) : count(0) { }
53 
57  KShared &operator=(const KShared & ) { return *this; }
58 
62  void _KShared_ref() const { count++; }
63 
68  void _KShared_unref() const { if (!--count) delete this; }
69 
75  int _KShared_count() const { return count; }
76 
77 protected:
78  virtual ~KShared() { }
79 private:
80  mutable int count;
81 };
82 
99 template< class T >
100 class KSharedPtr
101 {
102 public:
106  KSharedPtr()
107  : ptr(0) { }
112  KSharedPtr( T* t )
113  : ptr(t) { if ( ptr ) ptr->_KShared_ref(); }
114 
119  KSharedPtr( const KSharedPtr& p )
120  : ptr(p.ptr) { if ( ptr ) ptr->_KShared_ref(); }
121 
126  ~KSharedPtr() { if ( ptr ) ptr->_KShared_unref(); }
127 
128  KSharedPtr<T>& operator= ( const KSharedPtr<T>& p ) {
129  if ( ptr == p.ptr ) return *this;
130  if ( ptr ) ptr->_KShared_unref();
131  ptr = p.ptr;
132  if ( ptr ) ptr->_KShared_ref();
133  return *this;
134  }
135  KSharedPtr<T>& operator= ( T* p ) {
136  if ( ptr == p ) return *this;
137  if ( ptr ) ptr->_KShared_unref();
138  ptr = p;
139  if ( ptr ) ptr->_KShared_ref();
140  return *this;
141  }
142  bool operator== ( const KSharedPtr<T>& p ) const { return ( ptr == p.ptr ); }
143  bool operator!= ( const KSharedPtr<T>& p ) const { return ( ptr != p.ptr ); }
144  bool operator== ( const T* p ) const { return ( ptr == p ); }
145  bool operator!= ( const T* p ) const { return ( ptr != p ); }
146  bool operator!() const { return ( ptr == 0 ); }
147  operator T*() const { return ptr; }
148 
153  T* data() { return ptr; }
154 
159  const T* data() const { return ptr; }
160 
161  const T& operator*() const { return *ptr; }
162  T& operator*() { return *ptr; }
163  const T* operator->() const { return ptr; }
164  T* operator->() { return ptr; }
165 
170  int count() const { return ptr->_KShared_count(); } // for debugging purposes
171 private:
172  T* ptr;
173 };
174 
175 #endif
KSharedPtr
Can be used to control the lifetime of an object that has derived KShared.
Definition: ksharedptr.h:100
KShared::KShared
KShared()
Standard constructor.
Definition: ksharedptr.h:46
KSharedPtr::data
T * data()
Returns the pointer.
Definition: ksharedptr.h:153
KSharedPtr::~KSharedPtr
~KSharedPtr()
Unreferences the object that this pointer points to.
Definition: ksharedptr.h:126
KShared::_KShared_unref
void _KShared_unref() const
Releases a reference (decreases the reference count by one).
Definition: ksharedptr.h:68
KSharedPtr::count
int count() const
Returns the number of references.
Definition: ksharedptr.h:170
KShared::KShared
KShared(const KShared &)
Copy constructor.
Definition: ksharedptr.h:52
KSharedPtr::KSharedPtr
KSharedPtr(T *t)
Creates a new pointer.
Definition: ksharedptr.h:112
KShared::operator=
KShared & operator=(const KShared &)
Overloaded assignment operator.
Definition: ksharedptr.h:57
KShared::_KShared_count
int _KShared_count() const
Return the current number of references held.
Definition: ksharedptr.h:75
KSharedPtr::KSharedPtr
KSharedPtr(const KSharedPtr &p)
Copies a pointer.
Definition: ksharedptr.h:119
KSharedPtr::KSharedPtr
KSharedPtr()
Creates a null pointer.
Definition: ksharedptr.h:106
KSharedPtr::data
const T * data() const
Returns the pointer.
Definition: ksharedptr.h:159
KShared::_KShared_ref
void _KShared_ref() const
Increases the reference count by one.
Definition: ksharedptr.h:62
KShared
Reference counting for shared objects.
Definition: ksharedptr.h:40

kdecore

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

kdecore

Skip menu "kdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdecore by doxygen 1.8.6
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |