Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4TrackState.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 // 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 
54 {
55 protected:
56  static int fgLastID;
57  static int Create();
58 
60  virtual ~G4VTrackStateID() {}
61 };
62 
63 //------------------------------------------------------------------------------
64 
65 template<class T>
67 {
68 public:
69  static int GetID() { return fID; }
70 
71 private:
72  static const int fID;
73 
74  G4TrackStateID() {}
75  ~G4TrackStateID() {}
76 };
77 
78 template<class T>
80 
81 //------------------------------------------------------------------------------
82 
84 {
85 public:
87  virtual ~G4VTrackState() {}
88  virtual int GetID() = 0;
89 };
90 
91 //------------------------------------------------------------------------------
92 
93 typedef G4shared_ptr<G4VTrackState> G4VTrackStateHandle;
94 
95 //------------------------------------------------------------------------------
97 template<class T>
99 
100 template<class T>
102 {
103 public:
104  virtual ~G4TrackStateBase() {}
105 
106  virtual int GetID() {
107  return G4TrackStateID<T>::GetID();
108  }
109 
110  static int ID() {
111  return G4TrackStateID<T>::GetID();
112  }
113 
114 protected:
116 };
117 
118 //------------------------------------------------------------------------------
119 
120 template<class T>
122 {
123  /*
124  // friend T; // works in c++11
125  */
126  friend class G4TrackStateDependent<T>;
127 
128 public:
129  virtual ~G4TrackState() {}
130 
131  static int ID() {
132  return G4TrackStateID<T>::GetID();
133  }
134 
136 };
137 
138 //------------------------------------------------------------------------------
139 
140 
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] = state;
151  }
152 
153  G4VTrackStateHandle GetTrackState(void* adress) const
154  {
155  std::map<void*, G4VTrackStateHandle>::const_iterator 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  std::map<void*, G4VTrackStateHandle>::const_iterator 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  std::map<int, G4VTrackStateHandle>::const_iterator 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 
197 {
198 public:
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>
215 {
216 
217  G4shared_ptr<G4VTrackState> output =
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 =
229  return output;
230 }
231 
232 //------------------------------------------------------------------------------
234 template<class T>
236 {
237 public:
238  typedef T ClassType;
240  typedef G4shared_ptr<StateType> StateTypeHandle;
241 
243 
244  virtual void SetTrackState(G4shared_ptr<StateType> state)
245  {
246  fpTrackState = state;
247  }
248 
249  virtual G4VTrackStateHandle PopTrackState()
250  {
251  G4VTrackStateHandle output =
253  fpTrackState.reset();
254  return output;
255  }
256 
257  virtual G4VTrackStateHandle GetTrackState() const
258  {
259  G4VTrackStateHandle output =
261  return output;
262  }
263 
265  {
266  return fpTrackState;
267  }
268 
269  virtual void LoadTrackState(G4TrackStateManager& manager)
270  {
271  fpTrackState =
272  ConvertToConcreteTrackState<ClassType>(manager.GetTrackState(this));
273  if (fpTrackState == nullptr)
274  {
275  NewTrackState();
276  SaveTrackState(manager);
277  }
278  }
279 
280  virtual void SaveTrackState(G4TrackStateManager& manager)
281  {
283  }
284 
285  virtual void NewTrackState()
286  {
288  }
289 
291  {
292  return StateTypeHandle(new StateType());
293  }
294 
295  virtual void ResetTrackState()
296  {
297  fpTrackState.reset();
298  }
299 
300 protected:
303  {}
304 
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_ */
G4VTrackStateHandle GetTrackState() const
G4VTrackStateHandle GetTrackState(void *adress) const
void SetTrackState(G4VTrackStateHandle state)
virtual ~G4TrackStateDependent()
static int ID()
#define G4shared_ptr
Definition: G4memory.hh:32
virtual int GetID()=0
virtual void SetTrackState(G4shared_ptr< StateType > state)
virtual ~G4VTrackState()
Definition: G4TrackState.hh:87
virtual void NewTrackState()=0
void SetTrackState(void *adress, G4VTrackStateHandle state)
G4shared_ptr< G4VTrackState > G4VTrackStateHandle
Definition: G4TrackState.hh:93
G4TrackState< T > StateType
virtual void ResetTrackState()=0
virtual ~G4TrackStateBase()
static int ID()
static int GetID()
Definition: G4TrackState.hh:69
virtual StateTypeHandle GetConcreteTrackState() const
virtual G4VTrackStateHandle GetTrackState() const =0
static int Create()
Definition: G4TrackState.cc:38
virtual void ResetTrackState()
StateTypeHandle fpTrackState
virtual void LoadTrackState(G4TrackStateManager &)=0
virtual ~G4VTrackStateDependent()
virtual void NewTrackState()
G4VTrackStateHandle GetTrackState(T *adress) const
#define G4dynamic_pointer_cast
Definition: G4memory.hh:36
virtual ~G4VTrackStateID()
Definition: G4TrackState.hh:60
virtual ~G4TrackState()
G4shared_ptr< G4VTrackState > ConvertToAbstractTrackState(G4shared_ptr< G4TrackState< OriginalType > > state)
static int fgLastID
Definition: G4TrackState.hh:56
virtual int GetID()
virtual void SaveTrackState(G4TrackStateManager &)=0
virtual G4VTrackStateHandle PopTrackState()
virtual G4VTrackStateHandle GetTrackState() const
virtual void SaveTrackState(G4TrackStateManager &manager)
virtual void LoadTrackState(G4TrackStateManager &manager)
virtual G4VTrackStateHandle PopTrackState()=0
G4shared_ptr< StateType > StateTypeHandle
G4shared_ptr< G4TrackState< FinalType > > ConvertToConcreteTrackState(G4VTrackStateHandle state)
virtual StateTypeHandle CreateTrackState() const