Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4ReferenceCountedHandle.hh
이 파일의 문서화 페이지로 가기
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 110251 2018-05-17 14:09:28Z 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 // Date: November 2001
53 // ----------------------------------------------------------------------
54 #ifndef _G4REFERENCECOUNTEDHANDLE_H_
55 #define _G4REFERENCECOUNTEDHANDLE_H_ 1
56 
57 #include "G4Types.hh"
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 private:
113 
115  // The object subject to reference counting.
116 };
117 
118 extern G4GLOB_DLL
120 
121 template <class X>
122 class G4CountedObject
123 {
124 
126 
127 public: // with description
128 
129  G4CountedObject( X* pObj = 0 );
130  // Constructor.
131 
133  // Destructor.
134 
135  inline void AddRef();
136  // Increase the count.
137 
138  inline void Release();
139  // Decrease the count and if zero destroy itself.
140 
141  // There is no provision that this class is subclassed.
142  // If it is subclassed & new data members are added then the
143  // following "new" & "delete" will fail and give errors.
144  //
145  inline void* operator new( size_t );
146  // Operator new defined for G4Allocator.
147 
148  inline void operator delete( void *pObj );
149  // operator delete defined for G4Allocator.
150 
151 private:
152 
153  unsigned int fCount;
154  // Reference counter.
155  X* fRep;
156  // The counted object.
157 };
158 
159 extern G4GLOB_DLL
161 
162 // --------- G4CountedObject<X> Inline function definitions ---------
163 
164 template <class X>
166  : fCount(0), fRep( pObj )
167 {
168  if( pObj != 0 ) fCount = 1;
169 }
170 
171 template <class X>
173 {
174  delete fRep;
175 }
176 
177 template <class X>
179 {
180  ++fCount;
181 }
182 
183 template <class X>
185 {
186  if( --fCount == 0 ) delete this;
187 }
188 
189 template <class X>
190 void* G4CountedObject<X>::operator new( size_t )
191 {
194  return( (void *)aCountedObjectAllocator()->MallocSingle() );
195 }
196 
197 template <class X>
198 void G4CountedObject<X>::operator delete( void *pObj )
199 {
200  aCountedObjectAllocator()->FreeSingle( (G4CountedObject<void>*)pObj );
201 }
202 
203 // --------- G4ReferenceCountedHandle<X> Inline function definitions ---------
204 
205 template <class X>
208  : fObj( 0 )
209 {
210  if( rep != 0 )
211  fObj = new G4CountedObject<X>( rep );
212 }
213 
214 template <class X>
217  : fObj( right.fObj )
218 {
219  fObj->AddRef();
220 }
221 
222 template <class X>
224 {
225  if( fObj ) fObj->Release();
226 }
227 
228 template <class X>
231 {
232  if( fObj != right.fObj )
233  {
234  if( fObj )
235  fObj->Release();
236  this->fObj = right.fObj;
237  fObj->AddRef();
238  }
239  return *this;
240 }
241 
242 template <class X>
244  operator =( X* objPtr )
245 {
246  if( fObj )
247  fObj->Release();
248  this->fObj = new G4CountedObject<X>( objPtr );
249  return *this;
250 }
251 
252 template <class X>
254 {
255  return( fObj ? fObj->fCount : 0 );
256 }
257 
258 template <class X>
260 {
261  return( fObj ? fObj->fRep : 0 );
262 }
263 
264 template <class X>
266 {
267  return( ( !fObj ) ? true : false );
268 }
269 
270 template <class X>
272 {
273  return( ( fObj ) ? true : false );
274 }
275 
276 template <class X>
278 {
279  return( fObj ? fObj->fRep : 0 );
280 }
281 
282 template <class X>
284 {
285  if (!aRCHAllocator())
287  return( (void *)aRCHAllocator()->MallocSingle() );
288 }
289 
290 template <class X>
291 void G4ReferenceCountedHandle<X>::operator delete( void *pObj )
292 {
293  aRCHAllocator()->FreeSingle( (G4ReferenceCountedHandle<void>*)pObj );
294 }
295 
296 #endif // _G4REFERENCECOUNTEDHANDLE_H_
G4ReferenceCountedHandle< X > & operator=(const G4ReferenceCountedHandle< X > &right)
bool G4bool
Definition: G4Types.hh:79
#define G4GLOB_DLL
Definition: G4Types.hh:64
G4GLOB_DLL G4Allocator< G4ReferenceCountedHandle< void > > *& aRCHAllocator()
G4GLOB_DLL G4Allocator< G4CountedObject< void > > *& aCountedObjectAllocator()
Float_t X