File indexing completed on 2025-01-18 09:59:02
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049 #ifndef G4REFERENCECOUNTEDHANDLE_HH
0050 #define G4REFERENCECOUNTEDHANDLE_HH 1
0051
0052 #include "G4Allocator.hh"
0053 #include "G4Types.hh"
0054
0055 template <class X>
0056 class G4CountedObject;
0057
0058 template <class X>
0059 class G4ReferenceCountedHandle
0060 {
0061 public:
0062 inline G4ReferenceCountedHandle(X* rep = nullptr);
0063
0064
0065 inline G4ReferenceCountedHandle(const G4ReferenceCountedHandle<X>& right);
0066
0067
0068 inline ~G4ReferenceCountedHandle();
0069
0070
0071 inline G4ReferenceCountedHandle<X>& operator=(
0072 const G4ReferenceCountedHandle<X>& right);
0073
0074
0075 inline G4ReferenceCountedHandle<X>& operator=(X* objPtr);
0076
0077
0078 inline unsigned int Count() const;
0079
0080
0081 inline X* operator->() const;
0082
0083
0084
0085
0086
0087 inline G4bool operator!() const;
0088
0089
0090 inline operator bool() const;
0091
0092
0093 inline X* operator()() const;
0094
0095
0096
0097
0098
0099
0100 inline void* operator new(std::size_t);
0101
0102
0103 inline void operator delete(void* pObj);
0104
0105
0106 private:
0107 G4CountedObject<X>* fObj = nullptr;
0108
0109 };
0110
0111 extern G4GLOB_DLL G4Allocator<G4ReferenceCountedHandle<void>>*& aRCHAllocator();
0112
0113 template <class X>
0114 class G4CountedObject
0115 {
0116 friend class G4ReferenceCountedHandle<X>;
0117
0118 public:
0119 G4CountedObject(X* pObj = nullptr);
0120
0121
0122 ~G4CountedObject();
0123
0124
0125 inline void AddRef();
0126
0127
0128 inline void Release();
0129
0130
0131
0132
0133
0134
0135 inline void* operator new(std::size_t);
0136
0137
0138 inline void operator delete(void* pObj);
0139
0140
0141 private:
0142 unsigned int fCount = 0;
0143
0144 X* fRep = nullptr;
0145
0146 };
0147
0148 extern G4GLOB_DLL G4Allocator<G4CountedObject<void>>*&
0149 aCountedObjectAllocator();
0150
0151
0152
0153 template <class X>
0154 G4CountedObject<X>::G4CountedObject(X* pObj)
0155 : fRep(pObj)
0156 {
0157 if(pObj != nullptr)
0158 fCount = 1;
0159 }
0160
0161 template <class X>
0162 G4CountedObject<X>::~G4CountedObject()
0163 {
0164 delete fRep;
0165 }
0166
0167 template <class X>
0168 void G4CountedObject<X>::AddRef()
0169 {
0170 ++fCount;
0171 }
0172
0173 template <class X>
0174 void G4CountedObject<X>::Release()
0175 {
0176 if(--fCount == 0)
0177 delete this;
0178 }
0179
0180 template <class X>
0181 void* G4CountedObject<X>::operator new(std::size_t)
0182 {
0183 if(aCountedObjectAllocator() == nullptr)
0184 aCountedObjectAllocator() = new G4Allocator<G4CountedObject<void>>;
0185 return ((void*) aCountedObjectAllocator()->MallocSingle());
0186 }
0187
0188 template <class X>
0189 void G4CountedObject<X>::operator delete(void* pObj)
0190 {
0191 aCountedObjectAllocator()->FreeSingle((G4CountedObject<void>*) pObj);
0192 }
0193
0194
0195
0196 template <class X>
0197 G4ReferenceCountedHandle<X>::G4ReferenceCountedHandle(X* rep)
0198 {
0199 if(rep != nullptr)
0200 fObj = new G4CountedObject<X>(rep);
0201 }
0202
0203 template <class X>
0204 G4ReferenceCountedHandle<X>::G4ReferenceCountedHandle(
0205 const G4ReferenceCountedHandle<X>& right)
0206 : fObj(right.fObj)
0207 {
0208 fObj->AddRef();
0209 }
0210
0211 template <class X>
0212 G4ReferenceCountedHandle<X>::~G4ReferenceCountedHandle()
0213 {
0214 if(fObj != nullptr)
0215 fObj->Release();
0216 }
0217
0218 template <class X>
0219 G4ReferenceCountedHandle<X>& G4ReferenceCountedHandle<X>::operator=(
0220 const G4ReferenceCountedHandle<X>& right)
0221 {
0222 if(fObj != right.fObj)
0223 {
0224 if(fObj != nullptr)
0225 fObj->Release();
0226 this->fObj = right.fObj;
0227 fObj->AddRef();
0228 }
0229 return *this;
0230 }
0231
0232 template <class X>
0233 G4ReferenceCountedHandle<X>& G4ReferenceCountedHandle<X>::operator=(X* objPtr)
0234 {
0235 if(fObj != nullptr)
0236 fObj->Release();
0237 this->fObj = new G4CountedObject<X>(objPtr);
0238 return *this;
0239 }
0240
0241 template <class X>
0242 unsigned int G4ReferenceCountedHandle<X>::Count() const
0243 {
0244 return ((fObj != nullptr) ? fObj->fCount : 0);
0245 }
0246
0247 template <class X>
0248 X* G4ReferenceCountedHandle<X>::operator->() const
0249 {
0250 return ((fObj != nullptr) ? fObj->fRep : 0);
0251 }
0252
0253 template <class X>
0254 G4bool G4ReferenceCountedHandle<X>::operator!() const
0255 {
0256 return fObj == nullptr;
0257 }
0258
0259 template <class X>
0260 G4ReferenceCountedHandle<X>::operator bool() const
0261 {
0262 return fObj != nullptr;
0263 }
0264
0265 template <class X>
0266 X* G4ReferenceCountedHandle<X>::operator()() const
0267 {
0268 return ((fObj != nullptr) ? fObj->fRep : nullptr);
0269 }
0270
0271 template <class X>
0272 void* G4ReferenceCountedHandle<X>::operator new(std::size_t)
0273 {
0274 if(aRCHAllocator() == nullptr)
0275 aRCHAllocator() = new G4Allocator<G4ReferenceCountedHandle<void>>;
0276 return ((void*) aRCHAllocator()->MallocSingle());
0277 }
0278
0279 template <class X>
0280 void G4ReferenceCountedHandle<X>::operator delete(void* pObj)
0281 {
0282 aRCHAllocator()->FreeSingle((G4ReferenceCountedHandle<void>*) pObj);
0283 }
0284
0285 #endif