Geant4  10.01.p03
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 public:
54  virtual int GetStateID() = 0;
55 protected:
57  {
58  ;
59  }
60  virtual ~G4VTrackStateID()
61  {
62  ;
63  }
64 
65  static int fgLastID;
66 };
67 
68 template<class T>
70  {
71  public:
72  static int GetID()
73  {
74  return fID;
75  }
76 
77  private:
78  static int Create()
79  {
80  fID = fgLastID;
81  fgLastID++;
82  return fID;
83  }
84 
86  {
87  }
89  {
90  ;
91  }
92  static int fID;
93  };
94 
95 template<class T>
97 
99 {
100 public:
102  {
103  }
104  virtual ~G4VTrackState()
105  {
106  }
107  virtual int GetID() = 0;
108 };
109 
110 typedef G4shared_ptr<G4VTrackState> G4VTrackStateHandle;
111 
112 //template<class TrackStateDependent>
113 //class RegisterState : public TrackStateDependent
114 //{
115 //public:
116 // typedef typename TrackStateDependent::State TrackState;
117 //};
118 
119 #ifndef TYPE_WRAPPER
120 #define TYPE_WRAPPER
121 template < typename T>
122 struct type_wrapper
123 {
124  typedef T type;
125 };
126 #endif
127 
129 template<class T>
131 
132 template<class T>
134  {
135  //friend class type_wrapper<T>::type; // works with gcc
137  /*
138  #ifdef WIN32
139  friend typename traits_type::type;
140  #elif defined(__clang__)
141  friend T;
142  #else
143  // friend class traits_type::type;
144  friend class type_wrapper<T>::type;
145  #endif
146  */
147  friend class G4TrackStateDependent<T> ;
148 
149  public:
151  {
152  }
153 
154  virtual int GetID()
155  {
156  return G4TrackStateID<T>::GetID();
157  }
158 
159  static int ID()
160  {
161  return G4TrackStateID<T>::GetID();
162  }
163 
165  G4VTrackState()
166  {
167  }
168 
169  protected:
170 
171  };
172 
173 template<class T>
174  class G4TrackState : public G4TrackStateBase<T>
175  {
176  // friend class type_wrapper<T>::type; // works with gcc
177 
179  /*
180  #ifdef WIN32
181  friend typename traits_type::type;
182  #elif defined(__clang__)
183  friend T;
184  #else
185  friend class type_wrapper<T>::type;
186  //friend class traits_type::type;
187  #endif
188  */
189  friend class G4TrackStateDependent<T> ;
190  public:
191  virtual ~G4TrackState()
192  {
193  }
194 
195  virtual int GetID()
196  {
197  return G4TrackStateID<T>::GetID();
198  }
199 
200  static int ID()
201  {
202  return G4TrackStateID<T>::GetID();
203  }
204 
206  G4TrackStateBase<T>()
207  {
208  }
209 
210  protected:
211  };
212 
214 {
215  std::map<int, G4VTrackStateHandle> fTrackStates;
216  std::map<void*, G4VTrackStateHandle> fMultipleTrackStates;
217 
218 public:
219 
220  void SetTrackState(void* adress, G4VTrackStateHandle state)
221  {
222  fMultipleTrackStates[adress] = state;
223  }
224 
225  G4VTrackStateHandle GetTrackState(void* adress) const
226  {
227  std::map<void*, G4VTrackStateHandle>::const_iterator it =
228  fMultipleTrackStates.find(adress);
229  if (it == fMultipleTrackStates.end())
230  {
231  return G4VTrackStateHandle();
232  }
233  return it->second;
234  }
235 
236  template<class T>
238  {
239  std::map<void*, G4VTrackStateHandle>::const_iterator it =
240  fMultipleTrackStates.find((void*) adress);
241  if (it == fMultipleTrackStates.end())
242  {
243  return G4VTrackStateHandle();
244  }
245  return it->second;
246  }
248  {
249  fTrackStates[state->GetID()] = state;
250  }
251  template<typename T>
253  {
254  std::map<int, G4VTrackStateHandle>::const_iterator it = fTrackStates.find(
256  if (it == fTrackStates.end())
257  {
258  return G4VTrackStateHandle();
259  }
260  return it->second;
261  }
262 };
263 
265 {
266 public:
268  {
269  ;
270  }
272  {
273  ;
274  }
275 
276  virtual void NewTrackState() = 0;
277  virtual void LoadTrackState(G4TrackStateManager&) = 0;
278  virtual void SaveTrackState(G4TrackStateManager&) = 0;
279  virtual G4VTrackStateHandle GetTrackState() const = 0;
280  virtual G4VTrackStateHandle PopTrackState() = 0;
281  virtual void ResetTrackState() = 0;
282 };
283 
284 #define G4TrackStateHandle(T) G4shared_ptr<G4TrackState<T> >
285 
286 template<class OriginalType>
288  {
289 
290  G4shared_ptr<G4VTrackState> output = G4dynamic_pointer_cast<G4VTrackState>(state);
291  return output;
292  }
293 
294 template<class FinalType>
295  G4shared_ptr<G4TrackState<FinalType> > ConvertToConcreteTrackState(G4VTrackStateHandle state)
296  {
297 
298  G4shared_ptr<G4TrackState<FinalType> > output = G4dynamic_pointer_cast<G4TrackState<FinalType> >(state);
299  return output;
300  }
301 
303 template<class T>
305  {
306  public:
307  typedef T ClassType;
309  typedef G4shared_ptr<StateType> StateTypeHandle;
310 
312  { ;}
313  virtual void SetTrackState(G4shared_ptr<StateType> state)
314  {
315  fpTrackState = state;
316  }
317 
319  {
321  fpTrackState.reset();
322  return output;
323  }
324 
326  {
328  return output;
329  }
330 
331  virtual StateTypeHandle GetConcreteTrackState() const
332  {
333  return fpTrackState;
334  }
335 
336  virtual void LoadTrackState(G4TrackStateManager& manager)
337  {
338  fpTrackState = ConvertToConcreteTrackState<ClassType>(manager.GetTrackState(this));
339  if(fpTrackState == 0)
340  {
341  NewTrackState();
342  SaveTrackState(manager);
343  }
344  }
345 
346  virtual void SaveTrackState(G4TrackStateManager& manager)
347  {
349  }
350 
351  virtual void NewTrackState()
352  {
354  }
355 
356  virtual StateTypeHandle CreateTrackState() const
357  {
358  return StateTypeHandle(new StateType());
359  }
360 
361  virtual void ResetTrackState()
362  {
363  fpTrackState.reset();
364  }
365 
366  protected:
369  { ;}
370 
371  StateTypeHandle fpTrackState;
372  };
373 
374 #define RegisterTrackState(CLASS,STATE) \
375  template<> \
376  class G4TrackState<CLASS> : public G4TrackStateBase<CLASS>, public STATE \
377  { \
378  typedef STATE State; \
379  friend class G4TrackStateDependent<CLASS>; \
380  public: \
381  G4TrackState() : G4TrackStateBase<CLASS>(),STATE(){}\
382  virtual ~G4TrackState(){}\
383  virtual int GetID()\
384  {\
385  return G4TrackStateID<CLASS>::GetID();\
386  }\
387  static int ID()\
388  {\
389  return G4TrackStateID<CLASS>::GetID();\
390  }\
391  protected:\
392  };\
393 
394 
395 #endif /* G4TRACKSTATE_HH_ */
G4VTrackStateHandle GetTrackState() const
virtual StateTypeHandle GetConcreteTrackState() const
G4VTrackStateHandle GetTrackState(void *adress) const
void SetTrackState(G4VTrackStateHandle state)
static int ID()
type_wrapper< T > traits_type
#define G4shared_ptr
Definition: G4memory.hh:43
virtual int GetID()=0
virtual G4VTrackStateHandle GetTrackState() const
StateTypeHandle fpTrackState
virtual ~G4VTrackState()
virtual void LoadTrackState(G4TrackStateManager &manager)
virtual int GetStateID()=0
virtual void SetTrackState(G4shared_ptr< StateType > state)
virtual void NewTrackState()=0
void SetTrackState(void *adress, G4VTrackStateHandle state)
G4shared_ptr< G4VTrackState > G4VTrackStateHandle
virtual void ResetTrackState()=0
virtual ~G4TrackStateBase()
static int ID()
static int GetID()
Definition: G4TrackState.hh:72
static int Create()
Definition: G4TrackState.hh:78
virtual int GetID()
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:47
virtual ~G4VTrackStateID()
Definition: G4TrackState.hh:60
virtual ~G4TrackState()
std::map< void *, G4VTrackStateHandle > fMultipleTrackStates
type_wrapper< T > traits_type
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:65
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:92
G4shared_ptr< G4TrackState< FinalType > > ConvertToConcreteTrackState(G4VTrackStateHandle state)