Geant4  10.02.p01
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: G4ReferenceCountedHandle.hh 67970 2013-03-13 10:10:06Z gcosmo $
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 (Radovan.Chytracek@cern.ch)
52 // Version: 3.0
53 // Date: November 2001
54 // ----------------------------------------------------------------------
55 #ifndef _G4REFERENCECOUNTEDHANDLE_H_
56 #define _G4REFERENCECOUNTEDHANDLE_H_ 1
57 
58 #include "G4Allocator.hh"
59 
60 template <class X> class G4CountedObject;
61 
62 template <class X>
64 {
65 
66 public: // 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 
81  inline G4ReferenceCountedHandle<X>& operator =( X* objPtr );
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 
112 public:
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 
121 private:
122 
124  // The object subject to reference counting.
125 };
126 
128 
129 template <class X>
130 class G4CountedObject
131 {
132 
133  friend class G4ReferenceCountedHandle<X>;
134 
135 public: // with description
136 
137  G4CountedObject( X* pObj = 0 );
138  // Constructor.
139 
141  // Destructor.
142 
143  inline void AddRef();
144  // Increase the count.
145 
146  inline void Release();
147  // Decrease the count and if zero destroy itself.
148 
149  // There is no provision that this class is subclassed.
150  // If it is subclassed & new data members are added then the
151  // following "new" & "delete" will fail and give errors.
152  //
153  inline void* operator new( size_t );
154  // Operator new defined for G4Allocator.
155 
156  inline void operator delete( void *pObj );
157  // operator delete defined for G4Allocator.
158 
159 private:
160 
161  unsigned int fCount;
162  // Reference counter.
163  X* fRep;
164  // The counted object.
165 };
166 
168 
169 // --------- G4CountedObject<X> Inline function definitions ---------
170 
171 template <class X>
173  : fCount(0), fRep( pObj )
174 {
175  if( pObj != 0 ) fCount = 1;
176 }
177 
178 template <class X>
180 {
181  delete fRep;
182 }
183 
184 template <class X>
186 {
187  ++fCount;
188 }
189 
190 template <class X>
192 {
193  if( --fCount == 0 ) delete this;
194 }
195 
196 template <class X>
197 void* G4CountedObject<X>::operator new( size_t )
198 {
201  return( (void *)aCountedObjectAllocator->MallocSingle() );
202 }
203 
204 template <class X>
205 void G4CountedObject<X>::operator delete( void *pObj )
206 {
207  aCountedObjectAllocator->FreeSingle( (G4CountedObject<void>*)pObj );
208 }
209 
210 // --------- G4ReferenceCountedHandle<X> Inline function definitions ---------
211 
212 template <class X>
215  : fObj( 0 )
216 {
217  if( rep != 0 )
218  fObj = new G4CountedObject<X>( rep );
219 }
220 
221 template <class X>
224  : fObj( right.fObj )
225 {
226  fObj->AddRef();
227 }
228 
229 template <class X>
231 {
232  if( fObj ) fObj->Release();
233 }
234 
235 template <class X>
238 {
239  if( fObj != right.fObj )
240  {
241  if( fObj )
242  fObj->Release();
243  this->fObj = right.fObj;
244  fObj->AddRef();
245  }
246  return *this;
247 }
248 
249 template <class X>
251  operator =( X* objPtr )
252 {
253  if( fObj )
254  fObj->Release();
255  this->fObj = new G4CountedObject<X>( objPtr );
256  return *this;
257 }
258 
259 template <class X>
261 {
262  return( fObj ? fObj->fCount : 0 );
263 }
264 
265 template <class X>
267 {
268  return( fObj ? fObj->fRep : 0 );
269 }
270 
271 template <class X>
273 {
274  return( ( !fObj ) ? true : false );
275 }
276 
277 template <class X>
279 {
280  return( ( fObj ) ? true : false );
281 }
282 
283 template <class X>
285 {
286  return( fObj ? fObj->fRep : 0 );
287 }
288 
289 template <class X>
291 {
292  if (!aRCHAllocator)
294  return( (void *)aRCHAllocator->MallocSingle() );
295 }
296 
297 template <class X>
298 void G4ReferenceCountedHandle<X>::operator delete( void *pObj )
299 {
300  aRCHAllocator->FreeSingle( (G4ReferenceCountedHandle<void>*)pObj );
301 }
302 
303 #ifdef G4RF_DEBUG
304 template <class X>
305 void* G4ReferenceCountedHandle<X>::operator new( size_t, void *pObj )
306 {
307  return pObj;
308 }
309 #endif
310 
311 #endif // _G4REFERENCECOUNTEDHANDLE_H_
G4GLOB_DLL G4ThreadLocal G4Allocator< G4ReferenceCountedHandle< void > > * aRCHAllocator
#define G4ThreadLocal
Definition: tls.hh:89
#define G4GLOB_DLL
Definition: G4Types.hh:64
G4ReferenceCountedHandle< X > & operator=(const G4ReferenceCountedHandle< X > &right)
bool G4bool
Definition: G4Types.hh:79
G4GLOB_DLL G4ThreadLocal G4Allocator< G4CountedObject< void > > * aCountedObjectAllocator