1
#ifndef SIMPLESMARTPTR_H
2
#define SIMPLESMARTPTR_H
4
#include "zipios-config.h"
8
/** SimpleSmartPointer is a simple reference counting smart pointer
9
template. The type pointed to must keep a reference count that is
10
accessible through the two methods void ref() const and unsigned
11
int unref() const. The type must also handle the reference count
12
properly. The easiest way to do that is to use the ReferenceCount
14
template< class Type >
15
class SimpleSmartPointer {
17
Type *operator-> () const { return _p ; }
19
Type &operator* () const { return *_p ; }
21
SimpleSmartPointer( Type *p = 0 ) : _p( p ) { ref() ; }
23
template< class T2 > SimpleSmartPointer( const SimpleSmartPointer< T2 > &src )
24
: _p( src.get() ) { ref() ; }
26
SimpleSmartPointer( const SimpleSmartPointer &src ) : _p( src.get() ) {
30
~SimpleSmartPointer () { if ( unref() == 0 ) deleteIt() ; }
33
SimpleSmartPointer &operator= ( const SimpleSmartPointer< T2 > &src ) {
41
SimpleSmartPointer &operator= ( const SimpleSmartPointer &src ) {
49
SimpleSmartPointer &operator=( Type *src ) {
55
bool operator== ( const Type *p ) const { return _p == p ; }
56
bool operator!= ( const Type *p ) const { return _p != p ; }
57
bool operator== ( const SimpleSmartPointer &sp ) const { return _p == sp.get() ; }
58
bool operator!= ( const SimpleSmartPointer &sp ) const { return _p != sp.get() ; }
59
bool operator! () const { return ! _p ; }
60
// This next method is inspired by iostream, and is for use with
61
// if ( some_smart_pointer ).
62
operator void*() const { return _p ? (void *)(-1) : (void *)(0) ; }
64
Type *get() const { return _p ; }
66
/** Returns the reference count - For debugging purposes. */
67
unsigned int getReferenceCount() const { return _p->getReferenceCount(); }
72
void ref( const T2 *ptr ) { if ( ptr ) ptr->ref() ; }
74
void ref() const { if ( _p ) _p->ref() ; }
75
unsigned int unref() const {
83
// cerr << "SimpleSmartPointer: Deleting object!" << endl ;
90
/** ReferenceCount is useful to ensure proper handling of the
91
reference count for (objects of) classes handled through a
92
SimpleSmartPointer. Subclassing ReferenceCount is all a class
93
needs to become ready for being handled by
94
SimpleSmartPointer. Another way is to add a ReferenceCount member
95
variable to a class and write two methods 'void ref() const' and
96
'unsigned int unref() const' that invoke the same methods in the
97
ReferenceCount variable. */
98
template< class Type >
99
class ReferenceCount {
100
/** SimpleSmartPointer needs to be a friend to invoke the private
101
ref() and unref() methods. */
102
friend class SimpleSmartPointer< Type > ;
103
friend class SimpleSmartPointer< const Type > ;
104
/** Type also needs to be a friend to invoke the private ref() and
105
unref() methods, in case Type doesn't want to inherit
106
ReferenceCount and thus needs to invoke ref() and unref()
107
through forwarding member functions. */
109
// Originally the following template parameter was made a friend.
110
// This is not allowed by the standard so comment it out:
114
// Initially hack things by making the necessary classes friends
115
// even though we don't know really which they are. This is an
117
friend class FileEntry ;
121
/** Constructor intializes count to zero. */
122
ReferenceCount() : _ref_count( 0 ) {}
124
/** Copy-constructor intializes count to zero. It doesn't copy it
126
ReferenceCount( const ReferenceCount & /*src*/ ) : _ref_count( 0 ) {}
128
/** The assignment operator doesn't copy the reference count, it
129
leaves it unchanged. */
130
const ReferenceCount &operator= ( const ReferenceCount & /*src*/ ) { return *this; }
133
/** Increases the reference count. */
134
void ref() const { ++_ref_count ; }
136
/** Decreases the reference count. */
137
unsigned int unref() const { return --_ref_count ; }
139
/** Returns the reference count - For debugging purposes. */
140
unsigned int getReferenceCount() const { return _ref_count; }
142
/** Holds the actual reference count */
143
mutable unsigned short _ref_count ;
153
Header file that defines SimpleSmartPointer and ReferenceCount.
157
Zipios++ - a small C++ library that provides easy access to .zip files.
158
Copyright (C) 2000 Thomas S�ndergaard
160
This library is free software; you can redistribute it and/or
161
modify it under the terms of the GNU Lesser General Public
162
License as published by the Free Software Foundation; either
163
version 2 of the License, or (at your option) any later version.
165
This library is distributed in the hope that it will be useful,
166
but WITHOUT ANY WARRANTY; without even the implied warranty of
167
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
168
Lesser General Public License for more details.
170
You should have received a copy of the GNU Lesser General Public
171
License along with this library; if not, write to the Free Software
172
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA