Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4ReferenceCountedHandle.hh
Go to the documentation of this file.
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$
28//
29//
30// Class G4ReferenceCountedHandle
31//
32// Class description:
33//
34// A class to provide reference counting mechanism.
35// It is a templated class, acting as a smart pointer,
36// wrapping the type to be counted. It performs the reference counting
37// during the life-time of the counted object. When its count reaches zero
38// the counted object is destroyed by explicit call to its destructor.
39// This class provides overloaded operators *() and ->() to allow similar
40// syntax as for the normal "dumb" pointers.
41// The basic rule for the use of this class is that a handle must always
42// be exchanged by reference never dinamically allocated (i.e. never
43// instantiated using 'new').
44// The validity of a smart pointer object can be verified by using the
45// operator !() or operator bool(). I.e.:
46// if( !smartPtrObj ) { ... } // Problem! We must initialize it first!
47// else { ... } // OK!
48// Trying to 'delete' a smart pointer object will generate a compilation
49// error (since we're dealing with objects, not pointers!).
50
51// Author: Radovan Chytracek, CERN ([email protected])
52// Version: 3.0
53// Date: November 2001
54// ----------------------------------------------------------------------
55#ifndef _G4REFERENCECOUNTEDHANDLE_H_
56#define _G4REFERENCECOUNTEDHANDLE_H_ 1
57
58#include "G4Allocator.hh"
59
60template <class X> class G4CountedObject;
61
62template <class X>
64{
65
66public: // with description
67
68 inline G4ReferenceCountedHandle( X* rep = 0 );
69 // Constructor.
70
72 // Copy constructor.
73
75 // Destructor.
76
79 // Assignment operator by reference.
80
82 // Assignment operator by pointer.
83
84 inline unsigned int Count() const;
85 // Forward to Counter class.
86
87 inline X* operator ->() const;
88 // Operator -> allowing the access to counted object.
89 // The check for 0-ness is left out for performance reasons,
90 // see operator () below.
91 // May be called on initialised smart-pointer only!
92
93 inline G4bool operator !() const;
94 // Validity test operator.
95
96 inline operator bool() const;
97 // Boolean operator.
98
99 inline X* operator ()() const;
100 // Functor operator (for convenience).
101
102 // There is no provision that this class is subclassed.
103 // If it is subclassed & new data members are added then the
104 // following "new" & "delete" will fail and give errors.
105 //
106 inline void* operator new( size_t );
107 // Operator new defined for G4Allocator.
108
109 inline void operator delete( void *pObj );
110 // Operator delete defined for G4Allocator.
111
112public:
113
114#ifdef G4RF_DEBUG
115 void* operator new( size_t, void *pObj );
116 // This is required on some compilers (Windows/VC++, Linux/g++) when this
117 // class is used in the context of STL container. It generates a warning
118 // saying something about not existing correspondent delete...
119#endif
120
121private:
122
124 // The object subject to reference counting.
125};
126
127#ifdef G4GLOB_ALLOC_EXPORT
129#else
131#endif
132
133template <class X>
135{
136
137 friend class G4ReferenceCountedHandle<X>;
138
139public: // with description
140
141 G4CountedObject( X* pObj = 0 );
142 // Constructor.
143
145 // Destructor.
146
147 inline void AddRef();
148 // Increase the count.
149
150 inline void Release();
151 // Decrease the count and if zero destroy itself.
152
153 // There is no provision that this class is subclassed.
154 // If it is subclassed & new data members are added then the
155 // following "new" & "delete" will fail and give errors.
156 //
157 inline void* operator new( size_t );
158 // Operator new defined for G4Allocator.
159
160 inline void operator delete( void *pObj );
161 // operator delete defined for G4Allocator.
162
163private:
164
165 unsigned int fCount;
166 // Reference counter.
167 X* fRep;
168 // The counted object.
169};
170
171#ifdef G4GLOB_ALLOC_EXPORT
173#else
175#endif
176
177// --------- G4CountedObject<X> Inline function definitions ---------
178
179template <class X>
181 : fCount(0), fRep( pObj )
182{
183 if( pObj != 0 ) {
184 fCount = 1;
185 }
186}
187
188template <class X>
190{
191 delete fRep;
192}
193
194template <class X>
196{
197 ++fCount;
198}
199
200template <class X>
202{
203 if( --fCount == 0 ) delete this;
204}
205
206template <class X>
208{
209 return( (void *)aCountedObjectAllocator.MallocSingle() );
210}
211
212template <class X>
213void G4CountedObject<X>::operator delete( void *pObj )
214{
216}
217
218// --------- G4ReferenceCountedHandle<X> Inline function definitions ---------
219
220template <class X>
223 : fObj( 0 )
224{
225 if( rep != 0 ) {
226 fObj = new G4CountedObject<X>( rep );
227 }
228}
229
230template <class X>
233 : fObj( right.fObj )
234{
235 fObj->AddRef();
236}
237
238template <class X>
240{
241 if( fObj ) fObj->Release();
242}
243
244template <class X>
247{
248 if( fObj != right.fObj ) {
249 if( fObj )
250 fObj->Release();
251 this->fObj = right.fObj;
252 fObj->AddRef();
253 }
254 return *this;
255}
256
257template <class X>
259 operator =( X* objPtr )
260{
261 if( fObj )
262 fObj->Release();
263 this->fObj = new G4CountedObject<X>( objPtr );
264 return *this;
265}
266
267template <class X>
269{
270 return( fObj ? fObj->fCount : 0 );
271}
272
273template <class X>
275{
276 return( fObj ? fObj->fRep : 0 );
277}
278
279template <class X>
281{
282 return( ( !fObj ) ? true : false );
283}
284
285template <class X>
287{
288 return( ( fObj ) ? true : false );
289}
290
291template <class X>
293{
294 return( fObj ? fObj->fRep : 0 );
295}
296
297template <class X>
299{
300 return( (void *)aRCHAllocator.MallocSingle() );
301}
302
303template <class X>
305{
307}
308
309#ifdef G4RF_DEBUG
310template <class X>
311void* G4ReferenceCountedHandle<X>::operator new( size_t, void *pObj )
312{
313 return pObj;
314}
315#endif
316
317#endif // _G4REFERENCECOUNTEDHANDLE_H_
318
G4DLLIMPORT G4Allocator< G4ReferenceCountedHandle< void > > aRCHAllocator
G4DLLIMPORT G4Allocator< G4CountedObject< void > > aCountedObjectAllocator
#define G4DLLIMPORT
Definition: G4Types.hh:56
#define G4DLLEXPORT
Definition: G4Types.hh:55
bool G4bool
Definition: G4Types.hh:67
G4ReferenceCountedHandle< X > & operator=(const G4ReferenceCountedHandle< X > &right)
G4ReferenceCountedHandle(const G4ReferenceCountedHandle< X > &right)