Zipios++
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 namespace zipios {
00025 
00026 class Bogus {
00027 public:
00028   Bogus(bool &isAlive) : _isAlive(isAlive) {}
00029   ~Bogus() { _isAlive = false; }
00030 protected:
00031 // http://support.microsoft.com/default.aspx?scid=kb;EN-US;168384
00032   friend class SimpleSmartPointer< Bogus > ;
00033   friend class SimpleSmartPointer< const Bogus > ;
00034 
00035   void           ref() const { _refcount.ref() ;          }
00036   unsigned int unref() const { return _refcount.unref() ; }
00037   unsigned int getReferenceCount() const { return _refcount.getReferenceCount() ; }
00038   ReferenceCount< Bogus > _refcount ;
00039   bool &_isAlive;
00040 };
00041 
00042 } // namespace 
00043 
00044 typedef SimpleSmartPointer< Bogus > SPBogus ;
00045 
00046 #endif
00047 
00048 int main() {
00049   bool isAlive = true;
00050   {
00051     Bogus *p = new Bogus(isAlive);
00052     SPBogus sp1( p ) ;
00053     assert( sp1.getReferenceCount() == 1 );
00054     {
00055       SPBogus sp2 ;
00056       sp2 = sp1 ;
00057       assert( sp1.getReferenceCount() == 2 );
00058       { 
00059         SPBogus sp3 ;
00060         sp3 = p ;
00061         assert( sp1.getReferenceCount() == 3 );
00062       }
00063       assert( sp1.getReferenceCount() == 2 );
00064       assert( isAlive );
00065     }
00066     assert( sp1.getReferenceCount() == 1 );
00067     assert( isAlive );
00068   }
00069   assert( ! isAlive );
00070   return 0;
00071 }
00072    
00073 
00079 /*
00080   Zipios++ - a small C++ library that provides easy access to .zip files.
00081   Copyright (C) 2000  Thomas Søndergaard
00082   
00083   This library is free software; you can redistribute it and/or
00084   modify it under the terms of the GNU Lesser General Public
00085   License as published by the Free Software Foundation; either
00086   version 2 of the License, or (at your option) any later version.
00087   
00088   This library is distributed in the hope that it will be useful,
00089   but WITHOUT ANY WARRANTY; without even the implied warranty of
00090   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00091   Lesser General Public License for more details.
00092   
00093   You should have received a copy of the GNU Lesser General Public
00094   License along with this library; if not, write to the Free Software
00095   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00096 */