]> git.donarmstrong.com Git - flightcrew.git/blob - src/zipios/zipios++/simplesmartptr.h
update changelog, target experimental
[flightcrew.git] / src / zipios / zipios++ / simplesmartptr.h
1 #ifndef SIMPLESMARTPTR_H
2 #define SIMPLESMARTPTR_H
3
4 #include "zipios++/zipios-config.h"
5
6 namespace zipios {
7
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
13     template class. */
14 template< class Type >
15 class SimpleSmartPointer {
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
66   /** Returns the reference count - For debugging purposes. */
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
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. */
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:
121   /** Constructor intializes count to zero. */
122   ReferenceCount() : _ref_count( 0 ) {}
123
124   /** Copy-constructor intializes count to zero. It doesn't copy it
125       from src. */
126   ReferenceCount( const ReferenceCount &src ) : _ref_count( 0 ) {}
127
128   /** The assignment operator doesn't copy the reference count, it
129       leaves it unchanged.  */
130   const ReferenceCount &operator= ( const ReferenceCount &src ) { return *this; }
131 private:
132
133   /** Increases the reference count. */
134   void ref() const           { ++_ref_count ;        }
135
136   /** Decreases the reference count. */
137   unsigned int unref() const { return --_ref_count ; }
138
139   /** Returns the reference count - For debugging purposes. */
140   unsigned int getReferenceCount() const { return _ref_count; }
141
142   /** Holds the actual reference count */
143   mutable unsigned short _ref_count ;
144 };
145
146
147
148 } // namespace
149
150 #endif
151
152 /** \file
153     Header file that defines SimpleSmartPointer and ReferenceCount.
154 */
155
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 */