1
/* This file is part of the KDE libraries
2
Copyright (c) 1999 Waldo Bastian <bastian@kde.org>
3
Copyright (c) 2001-2003 Werner Trobin <trobin@kde.org>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License version 2 as published by the Free Software Foundation.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
Boston, MA 02111-1307, USA.
22
#include "wv2_export.h"
27
* Reference counting for shared objects. If you derive your object
28
* from this class, then you may use it in conjunction with
29
* @ref SharedPtr to control the lifetime of your object.
31
* Specifically, all classes that derive from Shared have an internal
32
* counter keeping track of how many other objects have a reference to
33
* their object. If used with @ref SharedPtr, then your object will
34
* not be deleted until all references to the object have been
37
* You should probably not ever use any of the methods in this class
38
* directly -- let the @ref SharedPtr take care of that. Just derive
39
* your class from Shared and forget about it.
41
* @author Waldo Bastian <bastian@kde.org>
43
class WV2_EXPORT Shared {
46
* Standard constructor. This will initialize the reference count
49
Shared() : count( 0 ) { }
52
* Copy constructor. This will @em not actually copy the objects
53
* but it will initialize the reference count on this object to 0
55
Shared( const Shared & ) : count( 0 ) { }
58
* Overloaded assignment operator
60
Shared &operator=( const Shared & ) { return *this; }
63
* Increases the reference count by one
65
void _Shared_ref() const { count++; }
68
* Releases a reference (decreases the reference count by one). If
69
* the count goes to 0, this object will delete itself
71
void _Shared_deref() const { if (!--count) delete this; }
74
* Return the current number of references held
76
* @return Number of references
78
int _Shared_count() const { return count; }
87
* Can be used to control the lifetime of an object that has derived
88
* @ref Shared. As long a someone holds a SharedPtr on some Shared
89
* object it won't become deleted but is deleted once its reference
90
* count is 0. This struct emulates C++ pointers perfectly. So just
91
* use it like a simple C++ pointer.
93
* @author Waldo Bastian <bastian@kde.org>
102
: ptr(t) { if ( ptr ) ptr->_Shared_ref(); }
103
SharedPtr( const SharedPtr& p )
104
: ptr(p.ptr) { if ( ptr ) ptr->_Shared_ref(); }
106
~SharedPtr() { if ( ptr ) ptr->_Shared_deref(); }
108
SharedPtr<T>& operator= ( const SharedPtr<T>& p ) {
109
if ( ptr == p.ptr ) return *this;
110
if ( ptr ) ptr->_Shared_deref();
112
if ( ptr ) ptr->_Shared_ref();
115
SharedPtr<T>& operator= ( T* p ) {
116
if ( ptr == p ) return *this;
117
if ( ptr ) ptr->_Shared_deref();
119
if ( ptr ) ptr->_Shared_ref();
122
bool operator== ( const SharedPtr<T>& p ) const { return ( ptr == p.ptr ); }
123
bool operator!= ( const SharedPtr<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; }
129
T* data() { return ptr; }
130
const T* data() const { return ptr; }
132
const T& operator*() const { return *ptr; }
133
T& operator*() { return *ptr; }
134
const T* operator->() const { return ptr; }
135
T* operator->() { return ptr; }
137
int count() const { return ptr->_Shared_count(); } // for debugging purposes
142
} // namespace wvWare