simplesmartptr.h
Go to the documentation of this file.
#ifndef SIMPLESMARTPTR_H
#define SIMPLESMARTPTR_H
#include "zipios++/zipios-config.h"
namespace zipios {
template< class Type >
class SimpleSmartPointer {
public:
Type *operator-> () const { return _p ; }
Type &operator* () const { return *_p ; }
SimpleSmartPointer( Type *p = 0 ) : _p( p ) { ref() ; }
template< class T2 > SimpleSmartPointer( const SimpleSmartPointer< T2 > &src )
: _p( src.get() ) { ref() ; }
SimpleSmartPointer( const SimpleSmartPointer &src ) : _p( src.get() ) {
ref() ;
}
~SimpleSmartPointer () { if ( unref() == 0 ) deleteIt() ; }
template< class T2 >
SimpleSmartPointer &operator= ( const SimpleSmartPointer< T2 > &src ) {
ref( src.get() ) ;
if ( unref() == 0 )
deleteIt() ;
_p = src.get() ;
return *this ;
}
SimpleSmartPointer &operator= ( const SimpleSmartPointer &src ) {
ref( src.get() ) ;
if ( unref() == 0 )
deleteIt() ;
_p = src.get() ;
return *this ;
}
SimpleSmartPointer &operator=( Type *src ) {
_p = src ;
ref() ;
return *this ;
}
bool operator== ( const Type *p ) const { return _p == p ; }
bool operator!= ( const Type *p ) const { return _p != p ; }
bool operator== ( const SimpleSmartPointer &sp ) const { return _p == sp.get() ; }
bool operator!= ( const SimpleSmartPointer &sp ) const { return _p != sp.get() ; }
bool operator! () const { return ! _p ; }
// This next method is inspired by iostream, and is for use with
// if ( some_smart_pointer ).
operator void*() const { return _p ? (void *)(-1) : (void *)(0) ; }
Type *get() const { return _p ; }
unsigned int getReferenceCount() const { return _p->getReferenceCount(); }
private:
template< class T2 >
void ref( const T2 *ptr ) { if ( ptr ) ptr->ref() ; }
void ref() const { if ( _p ) _p->ref() ; }
unsigned int unref() const {
if ( _p )
return _p->unref();
else
return 0 ;
}
void deleteIt() {
// if( _p )
// cerr << "SimpleSmartPointer: Deleting object!" << endl ;
delete _p ;
}
Type *_p ;
};
template< class Type >
class ReferenceCount {
friend class SimpleSmartPointer< Type > ;
friend class SimpleSmartPointer< const Type > ;
//
// Originally the following template parameter was made a friend.
// This is not allowed by the standard so comment it out:
//
// friend Type ;
//
// Initially hack things by making the necessary classes friends
// even though we don't know really which they are. This is an
// Hideous Hack.
friend class FileEntry ;
friend class Bogus ;
public:
ReferenceCount() : _ref_count( 0 ) {}
ReferenceCount( const ReferenceCount &src ) : _ref_count( 0 ) {}
const ReferenceCount &operator= ( const ReferenceCount &src ) { return *this; }
private:
void ref() const { ++_ref_count ; }
unsigned int unref() const { return --_ref_count ; }
unsigned int getReferenceCount() const { return _ref_count; }
mutable unsigned short _ref_count ;
};
} // namespace
#endif
/*
Zipios++ - a small C++ library that provides easy access to .zip files.
Copyright (C) 2000 Thomas Søndergaard
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
zipios::ReferenceCount::ReferenceCount
ReferenceCount()
Constructor intializes count to zero.
Definition simplesmartptr.h:122
zipios::ReferenceCount::FileEntry
friend class FileEntry
Type also needs to be a friend to invoke the private ref() and unref() methods, in case Type doesn't ...
Definition simplesmartptr.h:117
zipios::ReferenceCount::ReferenceCount
ReferenceCount(const ReferenceCount &src)
Copy-constructor intializes count to zero.
Definition simplesmartptr.h:126
zipios::ReferenceCount::operator=
const ReferenceCount & operator=(const ReferenceCount &src)
The assignment operator doesn't copy the reference count, it leaves it unchanged.
Definition simplesmartptr.h:130
zipios::SimpleSmartPointer
SimpleSmartPointer is a simple reference counting smart pointer template.
Definition simplesmartptr.h:15
zipios::SimpleSmartPointer::getReferenceCount
unsigned int getReferenceCount() const
Returns the reference count - For debugging purposes.
Definition simplesmartptr.h:67