Geant4
9.6.p02
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
geant4_9_6_p02
source
global
management
include
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 (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>
63
class
G4ReferenceCountedHandle
64
{
65
66
public
:
// with description
67
68
inline
G4ReferenceCountedHandle
(
X
* rep = 0 );
69
// Constructor.
70
71
inline
G4ReferenceCountedHandle
(
const
G4ReferenceCountedHandle<X>
&
right
);
72
// Copy constructor.
73
74
inline
~G4ReferenceCountedHandle
();
75
// Destructor.
76
77
inline
G4ReferenceCountedHandle<X>
&
78
operator =
(
const
G4ReferenceCountedHandle<X>
& right );
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
123
G4CountedObject<X>
* fObj;
124
// The object subject to reference counting.
125
};
126
127
#ifdef G4GLOB_ALLOC_EXPORT
128
extern
G4DLLEXPORT
G4Allocator<G4ReferenceCountedHandle<void>
>
aRCHAllocator
;
129
#else
130
extern
G4DLLIMPORT
G4Allocator<G4ReferenceCountedHandle<void>
>
aRCHAllocator
;
131
#endif
132
133
template
<
class
X>
134
class
G4CountedObject
135
{
136
137
friend
class
G4ReferenceCountedHandle
<
X
>;
138
139
public
:
// with description
140
141
G4CountedObject
(
X
* pObj = 0 );
142
// Constructor.
143
144
~G4CountedObject
();
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
163
private
:
164
165
unsigned
int
fCount;
166
// Reference counter.
167
X
* fRep;
168
// The counted object.
169
};
170
171
#ifdef G4GLOB_ALLOC_EXPORT
172
extern
G4DLLEXPORT
G4Allocator<G4CountedObject<void>
>
aCountedObjectAllocator
;
173
#else
174
extern
G4DLLIMPORT
G4Allocator<G4CountedObject<void>
>
aCountedObjectAllocator
;
175
#endif
176
177
// --------- G4CountedObject<X> Inline function definitions ---------
178
179
template
<
class
X>
180
G4CountedObject<X>::G4CountedObject
(
X
* pObj )
181
: fCount(0), fRep( pObj )
182
{
183
if
( pObj != 0 ) {
184
fCount = 1;
185
}
186
}
187
188
template
<
class
X>
189
G4CountedObject<X>::~G4CountedObject
()
190
{
191
delete
fRep;
192
}
193
194
template
<
class
X>
195
void
G4CountedObject<X>::AddRef
()
196
{
197
++fCount;
198
}
199
200
template
<
class
X>
201
void
G4CountedObject<X>::Release
()
202
{
203
if
( --fCount == 0 )
delete
this
;
204
}
205
206
template
<
class
X>
207
void
*
G4CountedObject<X>::operator
new
( size_t )
208
{
209
return
( (
void
*)
aCountedObjectAllocator
.MallocSingle() );
210
}
211
212
template
<
class
X>
213
void
G4CountedObject<X>::operator
delete
(
void
*pObj )
214
{
215
aCountedObjectAllocator
.FreeSingle( (
G4CountedObject<void>
*)pObj );
216
}
217
218
// --------- G4ReferenceCountedHandle<X> Inline function definitions ---------
219
220
template
<
class
X>
221
G4ReferenceCountedHandle<X>::
222
G4ReferenceCountedHandle
(
X
* rep )
223
: fObj( 0 )
224
{
225
if
( rep != 0 ) {
226
fObj =
new
G4CountedObject<X>
( rep );
227
}
228
}
229
230
template
<
class
X>
231
G4ReferenceCountedHandle<X>::
232
G4ReferenceCountedHandle
(
const
G4ReferenceCountedHandle<X>
&
right
)
233
: fObj( right.fObj )
234
{
235
fObj->AddRef();
236
}
237
238
template
<
class
X>
239
G4ReferenceCountedHandle<X>::~G4ReferenceCountedHandle
()
240
{
241
if
( fObj ) fObj->Release();
242
}
243
244
template
<
class
X>
245
G4ReferenceCountedHandle<X>
&
G4ReferenceCountedHandle<X>::
246
operator =
(
const
G4ReferenceCountedHandle<X>
&
right
)
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
257
template
<
class
X>
258
G4ReferenceCountedHandle<X>
&
G4ReferenceCountedHandle<X>::
259
operator =
(
X
* objPtr )
260
{
261
if
( fObj )
262
fObj->Release();
263
this->fObj =
new
G4CountedObject<X>
( objPtr );
264
return
*
this
;
265
}
266
267
template
<
class
X>
268
unsigned
int
G4ReferenceCountedHandle<X>::Count
()
const
269
{
270
return
( fObj ? fObj->fCount : 0 );
271
}
272
273
template
<
class
X>
274
X
*
G4ReferenceCountedHandle<X>::operator ->
()
const
275
{
276
return
( fObj ? fObj->fRep : 0 );
277
}
278
279
template
<
class
X>
280
G4bool
G4ReferenceCountedHandle<X>::operator !
()
const
281
{
282
return
( ( !fObj ) ?
true
:
false
);
283
}
284
285
template
<
class
X>
286
G4ReferenceCountedHandle<X>::operator
bool()
const
287
{
288
return
( ( fObj ) ?
true
:
false
);
289
}
290
291
template
<
class
X>
292
X
*
G4ReferenceCountedHandle<X>::operator ()
()
const
293
{
294
return
( fObj ? fObj->fRep : 0 );
295
}
296
297
template
<
class
X>
298
void
*
G4ReferenceCountedHandle<X>::operator
new
( size_t )
299
{
300
return
( (
void
*)
aRCHAllocator
.MallocSingle() );
301
}
302
303
template
<
class
X>
304
void
G4ReferenceCountedHandle<X>::operator
delete
(
void
*pObj )
305
{
306
aRCHAllocator
.FreeSingle( (
G4ReferenceCountedHandle<void>
*)pObj );
307
}
308
309
#ifdef G4RF_DEBUG
310
template
<
class
X>
311
void
*
G4ReferenceCountedHandle<X>::operator
new
( size_t,
void
*pObj )
312
{
313
return
pObj;
314
}
315
#endif
316
317
#endif // _G4REFERENCECOUNTEDHANDLE_H_
318
Generated on Sat May 25 2013 14:33:17 for Geant4 by
1.8.4