Main Page   Class Hierarchy   Compound List   File List   Compound Members  

test_simplesmartptr.cpp

Go to the documentation of this file.
00001 
00002 #include "zipios++/zipios-config.h"
00003 
00004 #include "zipios++/meta-iostreams.h"
00005 #include <vector>
00006 #include <memory>
00007 
00008 #include "zipios++/simplesmartptr.h"
00009 
00010 #include <cassert>
00011 
00012 using namespace zipios ;
00013 
00014 using std::cerr ;
00015 using std::cout ;
00016 using std::endl ;
00017 using std::auto_ptr ;
00018 using std::ofstream ;
00019 using std::vector ;
00020 
00021 /* We don't want Bogus in the doxygen generated class index :-) */
00022 #ifndef DOXYGEN
00023 
00024 class Bogus {
00025 public:
00026   Bogus(bool &isAlive) : _isAlive(isAlive) {}
00027   ~Bogus() { _isAlive = false; }
00028 protected:
00029   friend SimpleSmartPointer< Bogus > ;
00030   friend SimpleSmartPointer< const Bogus > ;
00031 
00032   void           ref() const { _refcount.ref() ;          }
00033   unsigned int unref() const { return _refcount.unref() ; }
00034   unsigned int getReferenceCount() const { return _refcount.getReferenceCount() ; }
00035   ReferenceCount< Bogus > _refcount ;
00036   bool &_isAlive;
00037 };
00038 
00039 typedef SimpleSmartPointer< Bogus > SPBogus ;
00040 
00041 #endif
00042 
00043 int main() {
00044   bool isAlive = true;
00045   {
00046     Bogus *p = new Bogus(isAlive);
00047     SPBogus sp1( p ) ;
00048     assert( sp1.getReferenceCount() == 1 );
00049     {
00050       SPBogus sp2 ;
00051       sp2 = sp1 ;
00052       assert( sp1.getReferenceCount() == 2 );
00053       { 
00054         SPBogus sp3 ;
00055         sp3 = p ;
00056         assert( sp1.getReferenceCount() == 3 );
00057       }
00058       assert( sp1.getReferenceCount() == 2 );
00059       assert( isAlive );
00060     }
00061     assert( sp1.getReferenceCount() == 1 );
00062     assert( isAlive );
00063   }
00064   assert( ! isAlive );
00065 }
00066    
00067 
00073 /*
00074   Zipios++ - a small C++ library that provides easy access to .zip files.
00075   Copyright (C) 2000  Thomas Søndergaard
00076   
00077   This library is free software; you can redistribute it and/or
00078   modify it under the terms of the GNU Lesser General Public
00079   License as published by the Free Software Foundation; either
00080   version 2 of the License, or (at your option) any later version.
00081   
00082   This library is distributed in the hope that it will be useful,
00083   but WITHOUT ANY WARRANTY; without even the implied warranty of
00084   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00085   Lesser General Public License for more details.
00086   
00087   You should have received a copy of the GNU Lesser General Public
00088   License along with this library; if not, write to the Free Software
00089   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00090 */

Generated at Tue Aug 14 20:39:26 2001 for Zipios++ by doxygen1.2.0 written by Dimitri van Heesch, © 1997-2000