Zipios++
test_simplesmartptr.cpp
Go to the documentation of this file.
1
2#include "zipios++/zipios-config.h"
3
4#include "zipios++/meta-iostreams.h"
5#include <vector>
6#include <memory>
7
9
10#include <cassert>
11
12using namespace zipios ;
13
14using std::cerr ;
15using std::cout ;
16using std::endl ;
17using std::auto_ptr ;
18using std::ofstream ;
19using std::vector ;
20
21/* We don't want Bogus in the doxygen generated class index :-) */
22#ifndef DOXYGEN
23
24namespace zipios {
25
26class Bogus {
27public:
28 Bogus(bool &isAlive) : _isAlive(isAlive) {}
29 ~Bogus() { _isAlive = false; }
30protected:
31// http://support.microsoft.com/default.aspx?scid=kb;EN-US;168384
32 friend class SimpleSmartPointer< Bogus > ;
33 friend class SimpleSmartPointer< const Bogus > ;
34
35 void ref() const { _refcount.ref() ; }
36 unsigned int unref() const { return _refcount.unref() ; }
37 unsigned int getReferenceCount() const { return _refcount.getReferenceCount() ; }
38 ReferenceCount< Bogus > _refcount ;
39 bool &_isAlive;
40};
41
42} // namespace
43
44typedef SimpleSmartPointer< Bogus > SPBogus ;
45
46#endif
47
48int main() {
49 bool isAlive = true;
50 {
51 Bogus *p = new Bogus(isAlive);
52 SPBogus sp1( p ) ;
53 assert( sp1.getReferenceCount() == 1 );
54 {
55 SPBogus sp2 ;
56 sp2 = sp1 ;
57 assert( sp1.getReferenceCount() == 2 );
58 {
59 SPBogus sp3 ;
60 sp3 = p ;
61 assert( sp1.getReferenceCount() == 3 );
62 }
63 assert( sp1.getReferenceCount() == 2 );
64 assert( isAlive );
65 }
66 assert( sp1.getReferenceCount() == 1 );
67 assert( isAlive );
68 }
69 assert( ! isAlive );
70 return 0;
71}
72
73
79/*
80 Zipios++ - a small C++ library that provides easy access to .zip files.
81 Copyright (C) 2000 Thomas Søndergaard
82
83 This library is free software; you can redistribute it and/or
84 modify it under the terms of the GNU Lesser General Public
85 License as published by the Free Software Foundation; either
86 version 2 of the License, or (at your option) any later version.
87
88 This library is distributed in the hope that it will be useful,
89 but WITHOUT ANY WARRANTY; without even the implied warranty of
90 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
91 Lesser General Public License for more details.
92
93 You should have received a copy of the GNU Lesser General Public
94 License along with this library; if not, write to the Free Software
95 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
96*/
ReferenceCount is useful to ensure proper handling of the reference count for (objects of) classes ha...
SimpleSmartPointer is a simple reference counting smart pointer template.
Header file that defines SimpleSmartPointer and ReferenceCount.