Geant4  10.02
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, kara@cenbg.in2p3.fr
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 "G4memory.hh"
49 #include <map>
50 
52 {
53 protected:
55  virtual ~G4VTrackStateID(){;}
56  static int fgLastID;
57 };
58 
59 template<class T>
61  {
62  public:
63  static int GetID(){ return fID; }
64 
65  private:
66  static int Create()
67  {
68  fID = fgLastID;
69  fgLastID++;
70  return fID;
71  }
72 
75  static int fID;
76  };
77 
78 template<class T>
80 
82 {
83 public:
85  virtual ~G4VTrackState(){}
86  virtual int GetID() = 0;
87 };
88 
89 typedef G4shared_ptr<G4VTrackState> G4VTrackStateHandle;
90 
91 #ifndef TYPE_WRAPPER
92 #define TYPE_WRAPPER
93 template < typename T>
94 struct type_wrapper
95 {
96  typedef T type;
97 };
98 #endif
99 
101 template<class T>
103 
104 template<class T>
106  {
107  public:
108  virtual ~G4TrackStateBase(){}
109 
110  virtual int GetID()
111  {
112  return G4TrackStateID<T>::GetID();
113  }
114 
115  static int ID()
116  {
117  return G4TrackStateID<T>::GetID();
118  }
119 
120  protected:
122  };
123 
124 template<class T>
125  class G4TrackState : public G4TrackStateBase<T>
126  {
127 /*
128  typedef type_wrapper<T> traits_type;
129 
130  #ifdef WIN32
131  friend typename traits_type::type;
132  #elif defined(__clang__)
133  friend T;
134  #else
135  friend class type_wrapper<T>::type; // works with gcc
136  //friend class traits_type::type;
137  #endif
138 
139  // friend T; // works in c++11
140 */
141  friend class G4TrackStateDependent<T> ;
142  public:
143  virtual ~G4TrackState()
144  {
145  }
146 
147  static int ID()
148  {
149  return G4TrackStateID<T>::GetID();
150  }
151 
153  {
154  }
155 
156  protected:
157  };
158 
160 {
161  std::map<int, G4VTrackStateHandle> fTrackStates;
162  std::map<void*, G4VTrackStateHandle> fMultipleTrackStates;
163 
164 public:
165 
166  void SetTrackState(void* adress, G4VTrackStateHandle state)
167  {
168  fMultipleTrackStates[adress] = state;
169  }
170 
171  G4VTrackStateHandle GetTrackState(void* adress) const
172  {
173  std::map<void*, G4VTrackStateHandle>::const_iterator it =
174  fMultipleTrackStates.find(adress);
175  if (it == fMultipleTrackStates.end())
176  {
177  return G4VTrackStateHandle();
178  }
179  return it->second;
180  }
181 
182  template<class T>
184  {
185  std::map<void*, G4VTrackStateHandle>::const_iterator it =
186  fMultipleTrackStates.find((void*) adress);
187  if (it == fMultipleTrackStates.end())
188  {
189  return G4VTrackStateHandle();
190  }
191  return it->second;
192  }
194  {
195  fTrackStates[state->GetID()] = state;
196  }
197  template<typename T>
199  {
200  std::map<int, G4VTrackStateHandle>::const_iterator it = fTrackStates.find(
202  if (it == fTrackStates.end())
203  {
204  return G4VTrackStateHandle();
205  }
206  return it->second;
207  }
208 };
209 
211 {
212 public:
214  {;}
216  {;}
217 
218  virtual void NewTrackState() = 0;
219  virtual void LoadTrackState(G4TrackStateManager&) = 0;
220  virtual void SaveTrackState(G4TrackStateManager&) = 0;
221  virtual G4VTrackStateHandle GetTrackState() const = 0;
222  virtual G4VTrackStateHandle PopTrackState() = 0;
223  virtual void ResetTrackState() = 0;
224 };
225 
226 #define G4TrackStateHandle(T) G4shared_ptr<G4TrackState<T> >
227 
228 template<class OriginalType>
230  {
231 
232  G4shared_ptr<G4VTrackState> output = G4dynamic_pointer_cast<G4VTrackState>(state);
233  return output;
234  }
235 
236 template<class FinalType>
237  G4shared_ptr<G4TrackState<FinalType> > ConvertToConcreteTrackState(G4VTrackStateHandle state)
238  {
239 
240  G4shared_ptr<G4TrackState<FinalType> > output = G4dynamic_pointer_cast<G4TrackState<FinalType> >(state);
241  return output;
242  }
243 
245 template<class T>
247  {
248  public:
249  typedef T ClassType;
251  typedef G4shared_ptr<StateType> StateTypeHandle;
252 
254  { ;}
255  virtual void SetTrackState(G4shared_ptr<StateType> state)
256  {
257  fpTrackState = state;
258  }
259 
261  {
263  fpTrackState.reset();
264  return output;
265  }
266 
268  {
270  return output;
271  }
272 
273  virtual StateTypeHandle GetConcreteTrackState() const
274  {
275  return fpTrackState;
276  }
277 
278  virtual void LoadTrackState(G4TrackStateManager& manager)
279  {
280  fpTrackState = ConvertToConcreteTrackState<ClassType>(manager.GetTrackState(this));
281  if(fpTrackState == nullptr)
282  {
283  NewTrackState();
284  SaveTrackState(manager);
285  }
286  }
287 
288  virtual void SaveTrackState(G4TrackStateManager& manager)
289  {
291  }
292 
293  virtual void NewTrackState()
294  {
296  }
297 
298  virtual StateTypeHandle CreateTrackState() const
299  {
300  return StateTypeHandle(new StateType());
301  }
302 
303  virtual void ResetTrackState()
304  {
305  fpTrackState.reset();
306  }
307 
308  protected:
311  { ;}
312 
313  StateTypeHandle fpTrackState;
314  };
315 
316 #if __cplusplus > 199711L
317 #define RegisterTrackState(CLASS,STATE) \
318  template<> \
319  class G4TrackState<CLASS> : public G4TrackStateBase<CLASS>, \
320  public CLASS::STATE \
321  { \
322  friend class G4TrackStateDependent<CLASS>; \
323  using CLASS::STATE::STATE; \
324  public: \
325  typedef CLASS::STATE State; \
326  G4TrackState() : G4TrackStateBase<CLASS>(), CLASS::STATE(){}\
327  virtual ~G4TrackState(){}\
328  virtual int GetID()\
329  {\
330  return G4TrackStateID<CLASS>::GetID();\
331  }\
332  static int ID()\
333  {\
334  return G4TrackStateID<CLASS>::GetID();\
335  }\
336  protected:\
337  };
338 #else
339 #define RegisterTrackState(CLASS,STATE) \
340  template<> \
341  class G4TrackState<CLASS> : public G4TrackStateBase<CLASS>, \
342  public CLASS::STATE \
343  { \
344  friend class G4TrackStateDependent<CLASS>; \
345  public: \
346  typedef CLASS::STATE State; \
347  G4TrackState() : G4TrackStateBase<CLASS>(), CLASS::STATE(){}\
348  virtual ~G4TrackState(){}\
349  virtual int GetID()\
350  {\
351  return G4TrackStateID<CLASS>::GetID();\
352  }\
353  static int ID()\
354  {\
355  return G4TrackStateID<CLASS>::GetID();\
356  }\
357  protected:\
358  };
359 #endif
360 
361 #endif /* G4TRACKSTATE_HH_ */
G4VTrackStateHandle GetTrackState() const
virtual StateTypeHandle GetConcreteTrackState() const
G4VTrackStateHandle GetTrackState(void *adress) const
void SetTrackState(G4VTrackStateHandle state)
static int ID()
#define G4shared_ptr
Definition: G4memory.hh:32
virtual int GetID()=0
virtual G4VTrackStateHandle GetTrackState() const
StateTypeHandle fpTrackState
virtual ~G4VTrackState()
Definition: G4TrackState.hh:85
virtual void LoadTrackState(G4TrackStateManager &manager)
virtual void SetTrackState(G4shared_ptr< StateType > state)
virtual void NewTrackState()=0
void SetTrackState(void *adress, G4VTrackStateHandle state)
G4shared_ptr< G4VTrackState > G4VTrackStateHandle
Definition: G4TrackState.hh:89
virtual void ResetTrackState()=0
virtual ~G4TrackStateBase()
static int ID()
static int GetID()
Definition: G4TrackState.hh:63
static int Create()
Definition: G4TrackState.hh:66
virtual void ResetTrackState()
virtual void NewTrackState()
std::map< int, G4VTrackStateHandle > fTrackStates
virtual G4VTrackStateHandle GetTrackState() const =0
virtual void LoadTrackState(G4TrackStateManager &)=0
virtual ~G4VTrackStateDependent()
G4VTrackStateHandle GetTrackState(T *adress) const
virtual ~G4TrackStateDependent()
#define G4dynamic_pointer_cast
Definition: G4memory.hh:36
virtual ~G4VTrackStateID()
Definition: G4TrackState.hh:55
virtual ~G4TrackState()
std::map< void *, G4VTrackStateHandle > fMultipleTrackStates
G4shared_ptr< StateType > StateTypeHandle
virtual G4VTrackStateHandle PopTrackState()
G4shared_ptr< G4VTrackState > ConvertToAbstractTrackState(G4shared_ptr< G4TrackState< OriginalType > > state)
G4TrackState< T > StateType
static int fgLastID
Definition: G4TrackState.hh:56
virtual void SaveTrackState(G4TrackStateManager &manager)
virtual int GetID()
virtual void SaveTrackState(G4TrackStateManager &)=0
virtual StateTypeHandle CreateTrackState() const
virtual G4VTrackStateHandle PopTrackState()=0
static int fID
Definition: G4TrackState.hh:75
G4shared_ptr< G4TrackState< FinalType > > ConvertToConcreteTrackState(G4VTrackStateHandle state)