summaryrefslogtreecommitdiff
path: root/noncore/apps/tinykate/libkate/microkde/ksharedptr.h
Unidiff
Diffstat (limited to 'noncore/apps/tinykate/libkate/microkde/ksharedptr.h') (more/less context) (show whitespace changes)
-rw-r--r--noncore/apps/tinykate/libkate/microkde/ksharedptr.h142
1 files changed, 142 insertions, 0 deletions
diff --git a/noncore/apps/tinykate/libkate/microkde/ksharedptr.h b/noncore/apps/tinykate/libkate/microkde/ksharedptr.h
new file mode 100644
index 0000000..55ed2e9
--- a/dev/null
+++ b/noncore/apps/tinykate/libkate/microkde/ksharedptr.h
@@ -0,0 +1,142 @@
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 */
39class KShared {
40public:
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() { 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() { 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() { return count; }
75
76protected:
77 virtual ~KShared() { }
78 int count; // ### KDE 3.0: rename to something like _KShared_count
79 // or make private
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 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 */
95template< class T >
96struct KSharedPtr
97{
98public:
99 KSharedPtr()
100 : ptr(0) { }
101 KSharedPtr( T* t )
102 : ptr(t) { if ( ptr ) ptr->_KShared_ref(); }
103 KSharedPtr( const KSharedPtr& p )
104 : ptr(p.ptr) { if ( ptr ) ptr->_KShared_ref(); }
105
106 ~KSharedPtr() { if ( ptr ) ptr->_KShared_unref(); }
107
108 KSharedPtr<T>& operator= ( const KSharedPtr<T>& p ) {
109 if ( ptr == p.ptr ) return *this;
110 if ( ptr ) ptr->_KShared_unref();
111 ptr = p.ptr;
112 if ( ptr ) ptr->_KShared_ref();
113 return *this;
114 }
115 KSharedPtr<T>& operator= ( T* p ) {
116 if ( ptr == p ) return *this;
117 if ( ptr ) ptr->_KShared_unref();
118 ptr = p;
119 if ( ptr ) ptr->_KShared_ref();
120 return *this;
121 }
122 bool operator== ( const KSharedPtr<T>& p ) const { return ( ptr == p.ptr ); }
123 bool operator!= ( const KSharedPtr<T>& p ) const { return ( ptr != p.ptr ); }
124 bool operator== ( const T* p ) const { return ( ptr == p ); }
125 bool operator!= ( const T* p ) const { return ( ptr != p ); }
126 bool operator!() const { return ( ptr == 0 ); }
127 operator T*() const { return ptr; }
128
129 T* data() { return ptr; }
130 const T* data() const { return ptr; }
131
132 const T& operator*() const { return *ptr; }
133 T& operator*() { return *ptr; }
134 const T* operator->() const { return ptr; }
135 T* operator->() { return ptr; }
136
137 int count() const { return ptr->_KShared_count(); } // for debugging purposes
138private:
139 T* ptr;
140};
141
142#endif