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

tdecore

  • tdecore
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 TDESharedPTR_H
19#define TDESharedPTR_H
20
21#include "tdelibs_export.h"
22
40class TDECORE_EXPORT TDEShared {
41public:
46 TDEShared() : count(0) { }
47
52 TDEShared( const TDEShared & ) : count(0) { }
53
57 TDEShared &operator=(const TDEShared & ) { return *this; }
58
62 void _TDEShared_ref() const { count++; }
63
68 void _TDEShared_unref() const { if (!--count) delete this; }
69
75 int _TDEShared_count() const { return count; }
76
77protected:
78 virtual ~TDEShared() { }
79private:
80 mutable int count;
81};
82
99template< class T >
100class TDESharedPtr
101{
102public:
106 TDESharedPtr()
107 : ptr(0) { }
112 TDESharedPtr( T* t )
113 : ptr(t) { if ( ptr ) ptr->_TDEShared_ref(); }
114
119 TDESharedPtr( const TDESharedPtr& p )
120 : ptr(p.ptr) { if ( ptr ) ptr->_TDEShared_ref(); }
121
126 ~TDESharedPtr() { if ( ptr ) ptr->_TDEShared_unref(); }
127
128 TDESharedPtr<T>& operator= ( const TDESharedPtr<T>& p ) {
129 if ( ptr == p.ptr ) return *this;
130 if ( ptr ) ptr->_TDEShared_unref();
131 ptr = p.ptr;
132 if ( ptr ) ptr->_TDEShared_ref();
133 return *this;
134 }
135 TDESharedPtr<T>& operator= ( T* p ) {
136 if ( ptr == p ) return *this;
137 if ( ptr ) ptr->_TDEShared_unref();
138 ptr = p;
139 if ( ptr ) ptr->_TDEShared_ref();
140 return *this;
141 }
142 bool operator== ( const TDESharedPtr<T>& p ) const { return ( ptr == p.ptr ); }
143 bool operator!= ( const TDESharedPtr<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->_TDEShared_count(); } // for debugging purposes
171private:
172 T* ptr;
173};
174
175#endif
TDESharedPtr
Can be used to control the lifetime of an object that has derived TDEShared.
Definition: ksharedptr.h:101
TDESharedPtr::TDESharedPtr
TDESharedPtr()
Creates a null pointer.
Definition: ksharedptr.h:106
TDESharedPtr::~TDESharedPtr
~TDESharedPtr()
Unreferences the object that this pointer points to.
Definition: ksharedptr.h:126
TDESharedPtr::TDESharedPtr
TDESharedPtr(const TDESharedPtr &p)
Copies a pointer.
Definition: ksharedptr.h:119
TDESharedPtr::count
int count() const
Returns the number of references.
Definition: ksharedptr.h:170
TDESharedPtr::data
T * data()
Returns the pointer.
Definition: ksharedptr.h:153
TDESharedPtr::TDESharedPtr
TDESharedPtr(T *t)
Creates a new pointer.
Definition: ksharedptr.h:112
TDESharedPtr::data
const T * data() const
Returns the pointer.
Definition: ksharedptr.h:159
TDEShared
Reference counting for shared objects.
Definition: ksharedptr.h:40
TDEShared::_TDEShared_unref
void _TDEShared_unref() const
Releases a reference (decreases the reference count by one).
Definition: ksharedptr.h:68
TDEShared::_TDEShared_ref
void _TDEShared_ref() const
Increases the reference count by one.
Definition: ksharedptr.h:62
TDEShared::TDEShared
TDEShared()
Standard constructor.
Definition: ksharedptr.h:46
TDEShared::TDEShared
TDEShared(const TDEShared &)
Copy constructor.
Definition: ksharedptr.h:52
TDEShared::operator=
TDEShared & operator=(const TDEShared &)
Overloaded assignment operator.
Definition: ksharedptr.h:57
TDEShared::_TDEShared_count
int _TDEShared_count() const
Return the current number of references held.
Definition: ksharedptr.h:75

tdecore

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

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.4
This website is maintained by Timothy Pearson.