author | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
commit | b9aad1f15dc600e4dbe4c62d3fcced6363188ba3 (patch) (unidiff) | |
tree | 2c3d4004fb21c72cba65793859f9bcd8ffd3a49c /microkde/kdecore/ksharedptr.h | |
download | kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.zip kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.gz kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.bz2 |
Initial revision
Diffstat (limited to 'microkde/kdecore/ksharedptr.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | microkde/kdecore/ksharedptr.h | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/microkde/kdecore/ksharedptr.h b/microkde/kdecore/ksharedptr.h new file mode 100644 index 0000000..545058a --- a/dev/null +++ b/microkde/kdecore/ksharedptr.h | |||
@@ -0,0 +1,171 @@ | |||
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., 59 Temple Place - Suite 330, | ||
16 | Boston, MA 02111-1307, USA. | ||
17 | */ | ||
18 | #ifndef KSharedPTR_H | ||
19 | #define KSharedPTR_H | ||
20 | |||
21 | /** | ||
22 | * Reference counting for shared objects. If you derive your object | ||
23 | * from this class, then you may use it in conjunction with | ||
24 | * @ref KSharedPtr to control the lifetime of your object. | ||
25 | * | ||
26 | * Specifically, all classes that derive from KShared have an internal | ||
27 | * counter keeping track of how many other objects have a reference to | ||
28 | * their object. If used with @ref KSharedPtr, then your object will | ||
29 | * not be deleted until all references to the object have been | ||
30 | * released. | ||
31 | * | ||
32 | * You should probably not ever use any of the methods in this class | ||
33 | * directly -- let the @ref KSharedPtr take care of that. Just derive | ||
34 | * your class from KShared and forget about it. | ||
35 | * | ||
36 | * @author Waldo Bastian <bastian@kde.org> | ||
37 | * @version $Id$ | ||
38 | */ | ||
39 | class KShared { | ||
40 | public: | ||
41 | /** | ||
42 | * Standard constructor. This will initialize the reference count | ||
43 | * on this object to 0. | ||
44 | */ | ||
45 | KShared() : count(0) { } | ||
46 | |||
47 | /** | ||
48 | * Copy constructor. This will @em not actually copy the objects | ||
49 | * but it will initialize the reference count on this object to 0. | ||
50 | */ | ||
51 | KShared( const KShared & ) : count(0) { } | ||
52 | |||
53 | /** | ||
54 | * Overloaded assignment operator. | ||
55 | */ | ||
56 | KShared &operator=(const KShared & ) { return *this; } | ||
57 | |||
58 | /** | ||
59 | * Increases the reference count by one. | ||
60 | */ | ||
61 | void _KShared_ref() const { count++; } | ||
62 | |||
63 | /** | ||
64 | * Releases a reference (decreases the reference count by one). If | ||
65 | * the count goes to 0, this object will delete itself. | ||
66 | */ | ||
67 | void _KShared_unref() const { if (!--count) delete this; } | ||
68 | |||
69 | /** | ||
70 | * Return the current number of references held. | ||
71 | * | ||
72 | * @return Number of references | ||
73 | */ | ||
74 | int _KShared_count() const { return count; } | ||
75 | |||
76 | protected: | ||
77 | virtual ~KShared() { } | ||
78 | private: | ||
79 | mutable int count; | ||
80 | }; | ||
81 | |||
82 | /** | ||
83 | * Can be used to control the lifetime of an object that has derived | ||
84 | * @ref KShared. As long a someone holds a KSharedPtr on some @ref KShared | ||
85 | * object it won't become deleted but is deleted once its reference | ||
86 | * count is 0. This struct emulates C++ pointers perfectly. So just | ||
87 | * use it like a simple C++ pointer. | ||
88 | * | ||
89 | * KShared and KSharedPtr are preferred over QShared / QSharedPtr | ||
90 | * since they are more safe. | ||
91 | * | ||
92 | * @author Waldo Bastian <bastian@kde.org> | ||
93 | * @version $Id$ | ||
94 | */ | ||
95 | template< class T > | ||
96 | struct KSharedPtr | ||
97 | { | ||
98 | public: | ||
99 | /** | ||
100 | * Creates a null pointer. | ||
101 | */ | ||
102 | KSharedPtr() | ||
103 | : ptr(0) { } | ||
104 | /** | ||
105 | * Creates a new pointer. | ||
106 | * @param the pointer | ||
107 | */ | ||
108 | KSharedPtr( T* t ) | ||
109 | : ptr(t) { if ( ptr ) ptr->_KShared_ref(); } | ||
110 | |||
111 | /** | ||
112 | * Copies a pointer. | ||
113 | * @param the pointer to copy | ||
114 | */ | ||
115 | KSharedPtr( const KSharedPtr& p ) | ||
116 | : ptr(p.ptr) { if ( ptr ) ptr->_KShared_ref(); } | ||
117 | |||
118 | /** | ||
119 | * Unreferences the object that this pointer points to. If it was | ||
120 | * the last reference, the object will be deleted. | ||
121 | */ | ||
122 | ~KSharedPtr() { if ( ptr ) ptr->_KShared_unref(); } | ||
123 | |||
124 | KSharedPtr<T>& operator= ( const KSharedPtr<T>& p ) { | ||
125 | if ( ptr == p.ptr ) return *this; | ||
126 | if ( ptr ) ptr->_KShared_unref(); | ||
127 | ptr = p.ptr; | ||
128 | if ( ptr ) ptr->_KShared_ref(); | ||
129 | return *this; | ||
130 | } | ||
131 | KSharedPtr<T>& operator= ( T* p ) { | ||
132 | if ( ptr == p ) return *this; | ||
133 | if ( ptr ) ptr->_KShared_unref(); | ||
134 | ptr = p; | ||
135 | if ( ptr ) ptr->_KShared_ref(); | ||
136 | return *this; | ||
137 | } | ||
138 | bool operator== ( const KSharedPtr<T>& p ) const { return ( ptr == p.ptr ); } | ||
139 | bool operator!= ( const KSharedPtr<T>& p ) const { return ( ptr != p.ptr ); } | ||
140 | bool operator== ( const T* p ) const { return ( ptr == p ); } | ||
141 | bool operator!= ( const T* p ) const { return ( ptr != p ); } | ||
142 | bool operator!() const { return ( ptr == 0 ); } | ||
143 | operator T*() const { return ptr; } | ||
144 | |||
145 | /** | ||
146 | * Returns the pointer. | ||
147 | * @return the pointer | ||
148 | */ | ||
149 | T* data() { return ptr; } | ||
150 | |||
151 | /** | ||
152 | * Returns the pointer. | ||
153 | * @return the pointer | ||
154 | */ | ||
155 | const T* data() const { return ptr; } | ||
156 | |||
157 | const T& operator*() const { return *ptr; } | ||
158 | T& operator*() { return *ptr; } | ||
159 | const T* operator->() const { return ptr; } | ||
160 | T* operator->() { return ptr; } | ||
161 | |||
162 | /** | ||
163 | * Returns the number of references. | ||
164 | * @return the number of references | ||
165 | */ | ||
166 | int count() const { return ptr->_KShared_count(); } // for debugging purposes | ||
167 | private: | ||
168 | T* ptr; | ||
169 | }; | ||
170 | |||
171 | #endif | ||