Geant4 Cross Reference |
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 // Author: Mathieu Karamitros 27 28 // The code is developed in the framework of the ESA AO7146 29 // 30 // We would be very happy hearing from you, send us your feedback! :) 31 // 32 // In order for Geant4-DNA to be maintained and still open-source, 33 // article citations are crucial. 34 // If you use Geant4-DNA chemistry and you publish papers about your software, 35 // in addition to the general paper on Geant4-DNA: 36 // 37 // Int. J. Model. Simul. Sci. Comput. 1 (2010) 157–178 38 // 39 // we would be very happy if you could please also cite the following 40 // reference papers on chemistry: 41 // 42 // J. Comput. Phys. 274 (2014) 841-882 43 // Prog. Nucl. Sci. Tec. 2 (2011) 503-508 44 45 #ifndef G4TRACKSTATE_HH_ 46 #define G4TRACKSTATE_HH_ 47 48 #include <map> 49 #include "G4memory.hh" 50 51 //------------------------------------------------------------------------------ 52 53 class G4VTrackStateID 54 { 55 protected: 56 static int fgLastID; 57 static int Create(); 58 59 G4VTrackStateID() {} 60 virtual ~G4VTrackStateID() = default; 61 }; 62 63 //------------------------------------------------------------------------------ 64 65 template<class T> 66 class G4TrackStateID: public G4VTrackStateID 67 { 68 public: 69 static int GetID() { return fID; } 70 71 private: 72 static const int fID; 73 74 G4TrackStateID() {} 75 ~G4TrackStateID() override = default; 76 }; 77 78 template<class T> 79 const int G4TrackStateID<T>::fID (G4VTrackStateID::Create()); 80 81 //------------------------------------------------------------------------------ 82 83 class G4VTrackState 84 { 85 public: 86 G4VTrackState() = default; 87 virtual ~G4VTrackState() = default; 88 virtual int GetID() = 0; 89 }; 90 91 //------------------------------------------------------------------------------ 92 93 using G4VTrackStateHandle = std::shared_ptr<G4VTrackState>; 94 95 //------------------------------------------------------------------------------ 96 //! 97 template<class T> 98 class G4TrackStateDependent; 99 100 template<class T> 101 class G4TrackStateBase : public G4VTrackState 102 { 103 public: 104 ~G4TrackStateBase() override = default; 105 106 int GetID() override { 107 return G4TrackStateID<T>::GetID(); 108 } 109 110 static int ID() { 111 return G4TrackStateID<T>::GetID(); 112 } 113 114 protected: 115 G4TrackStateBase() : G4VTrackState() {} 116 }; 117 118 //------------------------------------------------------------------------------ 119 120 template<class T> 121 class G4TrackState : public G4TrackStateBase<T> 122 { 123 /* 124 // friend T; // works in c++11 125 */ 126 friend class G4TrackStateDependent<T>; //! 127 128 public: 129 virtual ~G4TrackState() = default; 130 131 static int ID() { 132 return G4TrackStateID<T>::GetID(); 133 } 134 135 G4TrackState() : G4TrackStateBase<T>() {} 136 }; 137 138 //------------------------------------------------------------------------------ 139 140 141 class G4TrackStateManager 142 { 143 std::map<int, G4VTrackStateHandle> fTrackStates; 144 std::map<void*, G4VTrackStateHandle> fMultipleTrackStates; 145 146 public: 147 148 void SetTrackState(void* adress, G4VTrackStateHandle state) 149 { 150 fMultipleTrackStates[adress] = std::move(state); 151 } 152 153 G4VTrackStateHandle GetTrackState(void* adress) const 154 { 155 auto it = 156 fMultipleTrackStates.find(adress); 157 if (it == fMultipleTrackStates.end()) 158 { 159 return G4VTrackStateHandle(); 160 } 161 return it->second; 162 } 163 164 template<class T> 165 G4VTrackStateHandle GetTrackState(T* adress) const 166 { 167 auto it = 168 fMultipleTrackStates.find((void*)adress); 169 if (it == fMultipleTrackStates.end()) 170 { 171 return G4VTrackStateHandle(); 172 } 173 return it->second; 174 } 175 176 void SetTrackState(G4VTrackStateHandle state) 177 { 178 fTrackStates[state->GetID()] = state; 179 } 180 181 template<typename T> 182 G4VTrackStateHandle GetTrackState() const 183 { 184 auto it = 185 fTrackStates.find(G4TrackStateID<T>::GetID()); 186 if (it == fTrackStates.end()) 187 { 188 return G4VTrackStateHandle(); 189 } 190 return it->second; 191 } 192 }; 193 194 //------------------------------------------------------------------------------ 195 196 class G4VTrackStateDependent 197 { 198 public: 199 G4VTrackStateDependent() = default; 200 virtual ~G4VTrackStateDependent() = default; 201 202 virtual void NewTrackState() = 0; 203 virtual void LoadTrackState(G4TrackStateManager&) = 0; 204 virtual void SaveTrackState(G4TrackStateManager&) = 0; 205 virtual G4VTrackStateHandle GetTrackState() const = 0; 206 virtual G4VTrackStateHandle PopTrackState() = 0; 207 virtual void ResetTrackState() = 0; 208 }; 209 210 #define G4TrackStateHandle(T) G4shared_ptr<G4TrackState<T> > 211 212 template<class OriginalType> 213 G4shared_ptr<G4VTrackState> 214 ConvertToAbstractTrackState(G4shared_ptr<G4TrackState<OriginalType> > state) 215 { 216 217 G4shared_ptr<G4VTrackState> output = 218 G4dynamic_pointer_cast<G4VTrackState>(state); 219 return output; 220 } 221 222 template<class FinalType> 223 G4shared_ptr<G4TrackState<FinalType> > 224 ConvertToConcreteTrackState(G4VTrackStateHandle state) 225 { 226 227 G4shared_ptr<G4TrackState<FinalType> > output = 228 G4dynamic_pointer_cast<G4TrackState<FinalType>>(state); 229 return output; 230 } 231 232 //------------------------------------------------------------------------------ 233 //! 234 template<class T> 235 class G4TrackStateDependent : public G4VTrackStateDependent 236 { 237 public: 238 using ClassType = T; 239 using StateType = G4TrackState<T>; 240 using StateTypeHandle = std::shared_ptr<StateType>; 241 242 ~G4TrackStateDependent() override = default; 243 244 virtual void SetTrackState(G4shared_ptr<StateType> state) 245 { 246 fpTrackState = std::move(state); 247 } 248 249 G4VTrackStateHandle PopTrackState() override 250 { 251 G4VTrackStateHandle output = 252 G4dynamic_pointer_cast<G4VTrackState>(fpTrackState); 253 fpTrackState.reset(); 254 return output; 255 } 256 257 G4VTrackStateHandle GetTrackState() const override 258 { 259 G4VTrackStateHandle output = 260 G4dynamic_pointer_cast<G4VTrackState>(fpTrackState); 261 return output; 262 } 263 264 virtual StateTypeHandle GetConcreteTrackState() const 265 { 266 return fpTrackState; 267 } 268 269 void LoadTrackState(G4TrackStateManager& manager) override 270 { 271 fpTrackState = 272 ConvertToConcreteTrackState<ClassType>(manager.GetTrackState(this)); 273 if (fpTrackState == nullptr) 274 { 275 NewTrackState(); 276 SaveTrackState(manager); 277 } 278 } 279 280 void SaveTrackState(G4TrackStateManager& manager) override 281 { 282 manager.SetTrackState(this, ConvertToAbstractTrackState(fpTrackState)); 283 } 284 285 void NewTrackState() override 286 { 287 fpTrackState = StateTypeHandle(new StateType()); 288 } 289 290 virtual StateTypeHandle CreateTrackState() const 291 { 292 return StateTypeHandle(new StateType()); 293 } 294 295 void ResetTrackState() override 296 { 297 fpTrackState.reset(); 298 } 299 300 protected: 301 G4TrackStateDependent() 302 : G4VTrackStateDependent() 303 {} 304 305 StateTypeHandle fpTrackState; 306 }; 307 308 //------------------------------------------------------------------------------ 309 310 #if __cplusplus > 199711L 311 #define RegisterTrackState(CLASS,STATE) \ 312 template<> \ 313 class G4TrackState<CLASS> : public G4TrackStateBase<CLASS>, \ 314 public CLASS::STATE \ 315 { \ 316 friend class G4TrackStateDependent<CLASS>; \ 317 using CLASS::STATE::STATE; \ 318 public: \ 319 typedef CLASS::STATE State; \ 320 G4TrackState() : G4TrackStateBase<CLASS>(), CLASS::STATE(){}\ 321 virtual ~G4TrackState(){}\ 322 virtual int GetID()\ 323 {\ 324 return G4TrackStateID<CLASS>::GetID();\ 325 }\ 326 static int ID()\ 327 {\ 328 return G4TrackStateID<CLASS>::GetID();\ 329 }\ 330 protected:\ 331 }; 332 #else 333 #define RegisterTrackState(CLASS,STATE) \ 334 template<> \ 335 class G4TrackState<CLASS> : public G4TrackStateBase<CLASS>, \ 336 public CLASS::STATE \ 337 { \ 338 friend class G4TrackStateDependent<CLASS>; \ 339 public: \ 340 typedef CLASS::STATE State; \ 341 G4TrackState() : G4TrackStateBase<CLASS>(), CLASS::STATE(){}\ 342 virtual ~G4TrackState(){}\ 343 virtual int GetID()\ 344 {\ 345 return G4TrackStateID<CLASS>::GetID();\ 346 }\ 347 static int ID()\ 348 {\ 349 return G4TrackStateID<CLASS>::GetID();\ 350 }\ 351 protected:\ 352 }; 353 #endif 354 355 #endif /* G4TRACKSTATE_HH_ */ 356