Geant4  10.02.p02
G4TAtomicHitsCollection.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 //
28 //
29 //
30 // $Id: G4TAtomicHitsCollection.hh 93110 2015-11-05 08:37:42Z jmadsen $
31 //
32 //
41 //
42 //
43 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
44 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
45 
46 
47 
48 
49 #ifndef G4TAtomicHitsCollection_h
50 #define G4TAtomicHitsCollection_h 1
51 
52 #include "G4VHitsCollection.hh"
53 #include "G4Allocator.hh"
54 #include "globals.hh"
55 #include "G4Threading.hh"
56 #include "G4AutoLock.hh"
57 #include "G4atomic.hh"
58 
59 #include <deque>
60 #include <type_traits>
61 
62 // class description:
63 //
64 // This is a template class of hits collection and parametrized by
65 // The concrete class of G4VHit. This is a uniform collection for
66 // a particular concrete hit class objects.
67 // An intermediate layer class G4HitsCollection appeared in this
68 // header file is used just for G4Allocator, because G4Allocator
69 // cannot be instansiated with a template class. Thus G4HitsCollection
70 // class MUST NOT be directly used by the user.
71 
72 /*class G4HitsCollection : public G4VHitsCollection
73 {
74  public:
75  G4HitsCollection();
76  G4HitsCollection(G4String detName,G4String colNam);
77  virtual ~G4HitsCollection();
78  G4int operator==(const G4HitsCollection &right) const;
79 
80  protected:
81  void* theCollection;
82 };*/
83 
84 
85 template <class T>
87 {
88 protected:
89  static_assert(std::is_fundamental<T>::value,
90  "G4TAtomicHitsCollection must use fundamental type");
91 
92 public:
93  typedef T base_type;
94  typedef G4atomic<T> value_type;
95  typedef typename std::deque<value_type*> container_type;
96 
97 public:
99 
100 public:
101  // with description
102  G4TAtomicHitsCollection(G4String detName, G4String colNam);
103 
104  // constructor.
105 public:
106  virtual ~G4TAtomicHitsCollection();
108 
109  //inline void *operator new(size_t);
110  //inline void operator delete(void* anHC);
111 
112 public: // with description
113  virtual void DrawAllHits();
114  virtual void PrintAllHits();
115  // These two methods invokes Draw() and Print() methods of all of
116  // hit objects stored in this collection, respectively.
117 
118 public: // with description
119  inline value_type* operator[](size_t i) const
120  {
121  return (*theCollection)[i];
122  }
123  // Returns a pointer to a concrete hit object.
124  inline container_type* GetVector() const
125  {
126  return theCollection;
127  }
128  // Returns a collection vector.
129  inline G4int insert(T* aHit)
130  {
131  G4AutoLock l(&fMutex);
132  theCollection->push_back(aHit);
133  return theCollection->size();
134  }
135  // Insert a hit object. Total number of hit objects stored in this
136  // collection is returned.
137  inline G4int entries() const
138  {
139  G4AutoLock l(&fMutex);
140  return theCollection->size();
141  }
142  // Returns the number of hit objects stored in this collection
143 
144 public:
145  virtual G4VHit* GetHit(size_t i) const
146  {
147  return (*theCollection)[i];
148  }
149  virtual size_t GetSize() const
150  {
151  G4AutoLock l(&fMutex);
152  return theCollection->size();
153  }
154 
155 protected:
156  container_type* theCollection;
157  G4Mutex fMutex;
158 
159 };
160 
161 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
162 template <class T>
164  : theCollection(new container_type), fMutex(G4MUTEX_INITIALIZER)
165 { }
166 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
167 template <class T>
169  G4String colNam)
170  : G4VHitsCollection(detName,colNam),
171  theCollection(new container_type),
172  fMutex(G4MUTEX_INITIALIZER)
173 { }
174 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
176 {
177  for(size_t i = 0; i < theCollection->size(); i++)
178  delete (*theCollection)[i];
179  theCollection->clear();
180  delete theCollection;
181  G4MUTEXDESTROY(fMutex);
182 }
183 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
184 template <class T>
187 {
188  return (collectionName == right.collectionName);
189 }
190 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
191 template <class T>
193 {
194  G4AutoLock l(&fMutex);
195  for(size_t i = 0; i < theCollection->size(); i++)
196  (*theCollection)[i]->Draw();
197 }
198 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
199 template <class T>
201 {
202  G4AutoLock l(&fMutex);
203  for(size_t i = 0; i < theCollection->size(); i++)
204  (*theCollection)[i]->Print();
205 }
206 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
207 
208 #endif
virtual size_t GetSize() const
virtual void PrintAllHits()
G4int operator==(const G4VHitsCollection &right) const
This is an implementation of G4THitsCollection where the underlying type is G4atomic, not just T. A static assert is provided to ensure that T is fundamental. This class should be used in lieu of G4THitsCollection when memory is a concern. Atomics are thread-safe and generally faster that mutexes (as long as the STL implementation is lock-free) but the synchronization does not come without a cost. If performance is the primary concern, use G4THitsCollection in thread-local instances.
virtual G4VHit * GetHit(size_t) const
Definition: G4VHit.hh:48
virtual void DrawAllHits()
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
G4int G4Mutex
Definition: G4Threading.hh:173
#define G4MUTEXDESTROY(mutex)
Definition: G4Threading.hh:178
_Tp G4atomic
This is an friendly implementation of the STL atomic class. This class has the same interface as the ...
Definition: G4atomic.hh:271