1 | // |
---|
2 | // ******************************************************************** |
---|
3 | // * License and Disclaimer * |
---|
4 | // * * |
---|
5 | // * The Geant4 software is copyright of the Copyright Holders of * |
---|
6 | // * the Geant4 Collaboration. It is provided under the terms and * |
---|
7 | // * conditions of the Geant4 Software License, included in the file * |
---|
8 | // * LICENSE and available at http://cern.ch/geant4/license . These * |
---|
9 | // * include a list of copyright holders. * |
---|
10 | // * * |
---|
11 | // * Neither the authors of this software system, nor their employing * |
---|
12 | // * institutes,nor the agencies providing financial support for this * |
---|
13 | // * work make any representation or warranty, express or implied, * |
---|
14 | // * regarding this software system or assume any liability for its * |
---|
15 | // * use. Please see the license in the file LICENSE and URL above * |
---|
16 | // * for the full disclaimer and the limitation of liability. * |
---|
17 | // * * |
---|
18 | // * This code implementation is the result of the scientific and * |
---|
19 | // * technical work of the GEANT4 collaboration. * |
---|
20 | // * By using, copying, modifying or distributing the software (or * |
---|
21 | // * any work based on the software) you agree to acknowledge its * |
---|
22 | // * use in resulting scientific publications, and indicate your * |
---|
23 | // * acceptance of all terms of the Geant4 Software license. * |
---|
24 | // ******************************************************************** |
---|
25 | // |
---|
26 | // |
---|
27 | // $Id: testReferenceCountedHandle.cc,v 1.10 2006/06/29 19:05:08 gunter Exp $ |
---|
28 | // |
---|
29 | // |
---|
30 | // The program testing features and behaviour of the |
---|
31 | // reference counted handle, smart-pointer, class. |
---|
32 | // ---------------------------------------------------------------------- |
---|
33 | |
---|
34 | #define sDefault "Default" |
---|
35 | #define csDefault "cDefault" |
---|
36 | |
---|
37 | #include "G4ios.hh" |
---|
38 | #include "G4ReferenceCountedHandle.hh" |
---|
39 | |
---|
40 | #include <assert.h> |
---|
41 | |
---|
42 | #include <iostream> |
---|
43 | #include <string> |
---|
44 | #include <vector> |
---|
45 | |
---|
46 | class TesterBase |
---|
47 | { |
---|
48 | public: |
---|
49 | TesterBase() { |
---|
50 | } |
---|
51 | |
---|
52 | virtual ~TesterBase() { |
---|
53 | G4cout << "TesterBase being destroyed..." << G4endl; |
---|
54 | } |
---|
55 | |
---|
56 | inline bool Ok() const { |
---|
57 | return true; |
---|
58 | } |
---|
59 | |
---|
60 | virtual void Report() = 0; |
---|
61 | }; |
---|
62 | |
---|
63 | class TesterString : public TesterBase |
---|
64 | { |
---|
65 | public: |
---|
66 | |
---|
67 | TesterString( const char* str = csDefault ) { |
---|
68 | fData = str; |
---|
69 | } |
---|
70 | |
---|
71 | TesterString( std::string& str ) : fData(str) {} |
---|
72 | |
---|
73 | ~TesterString(){ |
---|
74 | G4cout << "Tester " << fData << " being destroyed..." << G4endl; |
---|
75 | } |
---|
76 | |
---|
77 | inline const std::string& Data() const { |
---|
78 | return fData; |
---|
79 | } |
---|
80 | |
---|
81 | inline void SetData( const std::string& data ) { |
---|
82 | fData = data; |
---|
83 | } |
---|
84 | |
---|
85 | inline void SetData( const char* data ) { |
---|
86 | fData = data; |
---|
87 | } |
---|
88 | |
---|
89 | virtual void Report() { |
---|
90 | G4cout << " >>" << Data() << "<< "; |
---|
91 | } |
---|
92 | |
---|
93 | private: |
---|
94 | std::string fData; |
---|
95 | }; |
---|
96 | |
---|
97 | |
---|
98 | class TesterInt : public TesterBase |
---|
99 | { |
---|
100 | public: |
---|
101 | TesterInt( int i = 0 ) : fData( i ) {} |
---|
102 | |
---|
103 | ~TesterInt(){ |
---|
104 | G4cout << "Tester " << fData << " being destroyed..." << G4endl; |
---|
105 | } |
---|
106 | |
---|
107 | inline const int& Data() const { |
---|
108 | return fData; |
---|
109 | } |
---|
110 | |
---|
111 | inline void SetData( const int data ) { |
---|
112 | fData = data; |
---|
113 | } |
---|
114 | |
---|
115 | virtual void Report() { |
---|
116 | G4cout << " >>" << Data() << "<< "; |
---|
117 | } |
---|
118 | |
---|
119 | private: |
---|
120 | int fData; |
---|
121 | }; |
---|
122 | |
---|
123 | typedef G4ReferenceCountedHandle<TesterBase> Counted; |
---|
124 | typedef G4ReferenceCountedHandle<TesterString> CountedString; |
---|
125 | typedef G4ReferenceCountedHandle<TesterInt> CountedInt; |
---|
126 | |
---|
127 | /* |
---|
128 | // Beta prototype of the class monitoring run-time activity of the RC handles |
---|
129 | class HandleWatcher { |
---|
130 | public: |
---|
131 | typedef std::vector<G4ReferenceCountedHandle<TesterBase> > Handles; |
---|
132 | typedef Handles::iterator HandlesIt; |
---|
133 | typedef Handles::const_iterator HandlesCIt; |
---|
134 | |
---|
135 | public: |
---|
136 | HandleWatcher() {} |
---|
137 | |
---|
138 | ~HandleWatcher() { |
---|
139 | fHandles.clear(); |
---|
140 | } |
---|
141 | |
---|
142 | inline void AddHandle( const Counted& handle ) { |
---|
143 | SetCurrent( handle ); |
---|
144 | fHandles.push_back( handle ); |
---|
145 | } |
---|
146 | |
---|
147 | inline const Counted& operator[]( unsigned int index ) { |
---|
148 | return fHandles[index]; |
---|
149 | } |
---|
150 | |
---|
151 | inline void SetCurrent( const Counted& handle ) { |
---|
152 | fCurrent = handle; |
---|
153 | } |
---|
154 | |
---|
155 | void Report() { |
---|
156 | G4cout << "HandleWatcher Report:" << G4endl; |
---|
157 | G4cout << "---------------------" << G4endl; |
---|
158 | HandlesCIt it; |
---|
159 | for( it = fHandles.begin(); it != fHandles.end(); it++ ) { |
---|
160 | G4cout << "Handle "; (*it)->Report(); G4cout << " count " << (*it).Count() << G4endl; |
---|
161 | } |
---|
162 | G4cout << "\nCurrent handle "; fCurrent->Report(); |
---|
163 | G4cout << " count " << fCurrent.Count() << G4endl; |
---|
164 | } |
---|
165 | |
---|
166 | private: |
---|
167 | Handles fHandles; |
---|
168 | Counted fCurrent; |
---|
169 | }; |
---|
170 | */ |
---|
171 | |
---|
172 | inline void SimulateNavigator( Counted& rcObj ) { |
---|
173 | rcObj = new TesterString( "Simulated" ); |
---|
174 | } |
---|
175 | |
---|
176 | int main( int argc, char* argv[] ) |
---|
177 | { |
---|
178 | Counted t0; |
---|
179 | G4cout << "Testing reference counting..." << G4endl; |
---|
180 | //HandleWatcher watcher; |
---|
181 | |
---|
182 | Counted t1 = new TesterString(); |
---|
183 | t0 = t1; |
---|
184 | { |
---|
185 | assert( t1 ); |
---|
186 | assert( 2 == t1.Count() ); |
---|
187 | |
---|
188 | G4cout << G4endl << "Initial tester 1 count:................... " << t1.Count() << G4endl; |
---|
189 | |
---|
190 | //watcher.AddHandle( t1 ); |
---|
191 | |
---|
192 | Counted t2; |
---|
193 | assert( !t2 ); |
---|
194 | |
---|
195 | G4cout << "Initial tester 2 count:................... " << t2.Count() << G4endl; |
---|
196 | |
---|
197 | t2 = t1; |
---|
198 | |
---|
199 | assert( t1 ); |
---|
200 | assert( t2 ); |
---|
201 | assert( 3 == t1.Count() ); |
---|
202 | assert( 3 == t2.Count() ); |
---|
203 | |
---|
204 | G4cout << "After assignment tester 1 count:.......... " << t1.Count() << G4endl; |
---|
205 | G4cout << "After assignment tester 2 count:.......... " << t2.Count() << G4endl; |
---|
206 | |
---|
207 | //memory_watcher.AllocInfo(); |
---|
208 | //watcher.AddHandle( t2 ); |
---|
209 | |
---|
210 | { |
---|
211 | Counted t3 = t2; |
---|
212 | |
---|
213 | assert( t3 ); |
---|
214 | |
---|
215 | assert( 4 == t1.Count() ); |
---|
216 | assert( 4 == t2.Count() ); |
---|
217 | assert( 4 == t3.Count() ); |
---|
218 | |
---|
219 | G4cout << "After copy tester 1 count:................ " << t1.Count() << G4endl; |
---|
220 | G4cout << "After copy tester 2 count:................ " << t2.Count() << G4endl; |
---|
221 | G4cout << "Initial tester 3 count:................... " << t3.Count() << G4endl; |
---|
222 | |
---|
223 | //watcher.AddHandle( t3 ); |
---|
224 | } // t3 Done |
---|
225 | |
---|
226 | //memory_watcher.AllocInfo(); |
---|
227 | |
---|
228 | assert( t1 ); |
---|
229 | assert( t2 ); |
---|
230 | assert( 3 == t1.Count() ); |
---|
231 | assert( 3 == t2.Count() ); |
---|
232 | |
---|
233 | G4cout << "After destruction of t3 tester 1 count:... " << t1.Count() << G4endl; |
---|
234 | G4cout << " tester 2 count:... " << t2.Count() << G4endl; |
---|
235 | } // t2 Done |
---|
236 | |
---|
237 | assert( t1 ); |
---|
238 | assert( 2 == t1.Count() ); |
---|
239 | |
---|
240 | G4cout << "After destruction of t2 tester 1 count:... " << t1.Count() << G4endl; |
---|
241 | |
---|
242 | //-------------------------------------------------------------------------------- |
---|
243 | G4cout << "\nTesting dereferencing..." << G4endl; |
---|
244 | assert( t1->Ok() ); |
---|
245 | G4cout << "Dereferencing of t1 method Ok() is:....... " << (int)t1->Ok() << G4endl; |
---|
246 | |
---|
247 | const TesterBase* nativeObj = t1(); |
---|
248 | assert( nativeObj != 0); |
---|
249 | G4cout << "Dereferenced object's method Ok() is:..... " << (int)nativeObj->Ok() << G4endl; |
---|
250 | |
---|
251 | //-------------------------------------------------------------------------------- |
---|
252 | G4cout << "\nTesting copy and assignment\nby a pointer to counted object...\n" << G4endl; |
---|
253 | G4cout << "Tester 1 is counting:....................."; t1->Report(); |
---|
254 | G4cout << "with count: " << t1.Count() << G4endl; |
---|
255 | Counted t4 = t1; |
---|
256 | G4cout << "\nTester 1 being copied to the new tester 4 ...\n" << G4endl; |
---|
257 | G4cout << "Tester 1 is counting:....................."; t1->Report(); |
---|
258 | G4cout << "with count: " << t1.Count() << G4endl; |
---|
259 | G4cout << "Tester 4 is counting:....................."; t4->Report(); |
---|
260 | G4cout << "with count: " << t4.Count() << G4endl; |
---|
261 | |
---|
262 | //watcher.AddHandle( t4 ); |
---|
263 | |
---|
264 | std::string str = "New"; |
---|
265 | t1 = new TesterString( str ); |
---|
266 | assert( 1 == t1.Count() ); |
---|
267 | G4cout << "\nAfter assignment of a counted object pointer to t1:\n" << G4endl; |
---|
268 | G4cout << "Tester 0 is counting:....................."; t0->Report(); |
---|
269 | G4cout << "with count: " << t0.Count() << G4endl; |
---|
270 | G4cout << "Tester 1 is counting:....................."; t1->Report(); |
---|
271 | G4cout << "with count: " << t1.Count() << G4endl; |
---|
272 | G4cout << "Tester 4 is counting:....................."; t4->Report(); |
---|
273 | G4cout << "with count: " << t4.Count() << G4endl; |
---|
274 | |
---|
275 | TesterString* dumbPtr = dynamic_cast<TesterString*>( t4() ); |
---|
276 | dumbPtr->SetData( "Updated" ); |
---|
277 | G4cout << "\nAfter the counted object's data update in t4:\n" << G4endl; |
---|
278 | G4cout << "Tester 0 is counting:....................."; t0->Report(); |
---|
279 | G4cout << "with count: " << t0.Count() << G4endl; |
---|
280 | G4cout << "Tester 1 is counting:....................."; t1->Report(); |
---|
281 | G4cout << "with count: " << t1.Count() << G4endl; |
---|
282 | G4cout << "Tester 4 is counting:....................."; t4->Report(); |
---|
283 | G4cout << "with count: " << t4.Count() << G4endl; |
---|
284 | |
---|
285 | t4 = new TesterInt( 100 ); |
---|
286 | G4cout << "\nAfter assignment of an int tester object to t1:\n" << G4endl; |
---|
287 | G4cout << "Tester 0 is counting:....................."; t0->Report(); |
---|
288 | G4cout << "with count: " << t0.Count() << G4endl; |
---|
289 | G4cout << "Tester 1 is counting:....................."; t1->Report(); |
---|
290 | G4cout << "with count: " << t1.Count() << G4endl; |
---|
291 | G4cout << "Tester 4 is counting:....................."; t4->Report(); |
---|
292 | G4cout << "with count: " << t4.Count() << G4endl; |
---|
293 | |
---|
294 | G4cout << "\nCalling SimulateNavigator( t0 )...\n" << G4endl; |
---|
295 | SimulateNavigator( t0 ); |
---|
296 | |
---|
297 | G4cout << "\nAfter SimulateNavigator( t0 ) call...\n" << G4endl; |
---|
298 | G4cout << "Tester 0 is counting:....................."; t0->Report(); |
---|
299 | G4cout << "with count: " << t0.Count() << G4endl; |
---|
300 | G4cout << "Tester 1 is counting:....................."; t1->Report(); |
---|
301 | G4cout << "with count: " << t1.Count() << G4endl; |
---|
302 | G4cout << "Tester 4 is counting:....................."; t4->Report(); |
---|
303 | G4cout << "with count: " << t4.Count() << G4endl; |
---|
304 | |
---|
305 | //watcher.Report(); |
---|
306 | |
---|
307 | G4cout << "\nEnd of program, expecting the automatic heap memory cleanup...\n" << G4endl; |
---|
308 | |
---|
309 | return 0; |
---|
310 | } |
---|