Zipios++
simplesmartptr.h
Go to the documentation of this file.
1 #ifndef SIMPLESMARTPTR_H
2 #define SIMPLESMARTPTR_H
3 
4 #include "zipios++/zipios-config.h"
5 
6 namespace zipios {
7 
14 template< class Type >
16 public:
17  Type *operator-> () const { return _p ; }
18 
19  Type &operator* () const { return *_p ; }
20 
21  SimpleSmartPointer( Type *p = 0 ) : _p( p ) { ref() ; }
22 
23  template< class T2 > SimpleSmartPointer( const SimpleSmartPointer< T2 > &src )
24  : _p( src.get() ) { ref() ; }
25 
26  SimpleSmartPointer( const SimpleSmartPointer &src ) : _p( src.get() ) {
27  ref() ;
28  }
29 
30  ~SimpleSmartPointer () { if ( unref() == 0 ) deleteIt() ; }
31 
32  template< class T2 >
33  SimpleSmartPointer &operator= ( const SimpleSmartPointer< T2 > &src ) {
34  ref( src.get() ) ;
35  if ( unref() == 0 )
36  deleteIt() ;
37  _p = src.get() ;
38  return *this ;
39  }
40 
41  SimpleSmartPointer &operator= ( const SimpleSmartPointer &src ) {
42  ref( src.get() ) ;
43  if ( unref() == 0 )
44  deleteIt() ;
45  _p = src.get() ;
46  return *this ;
47  }
48 
49  SimpleSmartPointer &operator=( Type *src ) {
50  _p = src ;
51  ref() ;
52  return *this ;
53  }
54 
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) ; }
63 
64  Type *get() const { return _p ; }
65 
67  unsigned int getReferenceCount() const { return _p->getReferenceCount(); }
68 
69 
70 private:
71  template< class T2 >
72  void ref( const T2 *ptr ) { if ( ptr ) ptr->ref() ; }
73 
74  void ref() const { if ( _p ) _p->ref() ; }
75  unsigned int unref() const {
76  if ( _p )
77  return _p->unref();
78  else
79  return 0 ;
80  }
81  void deleteIt() {
82 // if( _p )
83 // cerr << "SimpleSmartPointer: Deleting object!" << endl ;
84  delete _p ;
85  }
86  Type *_p ;
87 };
88 
89 
98 template< class Type >
102  friend class SimpleSmartPointer< Type > ;
103  friend class SimpleSmartPointer< const Type > ;
108  //
109  // Originally the following template parameter was made a friend.
110  // This is not allowed by the standard so comment it out:
111  //
112  // friend Type ;
113  //
114  // Initially hack things by making the necessary classes friends
115  // even though we don't know really which they are. This is an
116  // Hideous Hack.
117  friend class FileEntry ;
118  friend class Bogus ;
119 
120 public:
122  ReferenceCount() : _ref_count( 0 ) {}
123 
126  ReferenceCount( const ReferenceCount &src ) : _ref_count( 0 ) {}
127 
130  const ReferenceCount &operator= ( const ReferenceCount &src ) { return *this; }
131 private:
132 
134  void ref() const { ++_ref_count ; }
135 
137  unsigned int unref() const { return --_ref_count ; }
138 
140  unsigned int getReferenceCount() const { return _ref_count; }
141 
143  mutable unsigned short _ref_count ;
144 };
145 
146 
147 
148 } // namespace
149 
150 #endif
151 
156 /*
157  Zipios++ - a small C++ library that provides easy access to .zip files.
158  Copyright (C) 2000 Thomas Søndergaard
159 
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.
164 
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.
169 
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
173 */
const ReferenceCount & operator=(const ReferenceCount &src)
The assignment operator doesn't copy the reference count, it leaves it unchanged. ...
SimpleSmartPointer is a simple reference counting smart pointer template.
ReferenceCount(const ReferenceCount &src)
Copy-constructor intializes count to zero.
ReferenceCount is useful to ensure proper handling of the reference count for (objects of) classes ha...
A FileEntry represents an entry in a FileCollection.
Definition: fileentry.h:52
unsigned int getReferenceCount() const
Returns the reference count - For debugging purposes.
ReferenceCount()
Constructor intializes count to zero.