Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4TAtomicHitsMap.hh
이 파일의 문서화 페이지로 가기
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: G4TAtomicHitsMap.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 G4TAtomicHitsMap_h
50 #define G4TAtomicHitsMap_h 1
51 
52 #include "G4THitsCollection.hh"
53 #include "G4THitsMap.hh"
54 #include "globals.hh"
55 #include "G4atomic.hh"
56 #include "G4Threading.hh"
57 #include "G4AutoLock.hh"
58 
59 #include <map>
60 #include <type_traits>
61 
62 // class description:
63 //
64 // This is a template class of hits map 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 G4HitsMap appeared in this
68 // header file is used just for G4Allocator, because G4Allocator
69 // cannot be instansiated with a template class. Thus G4HitsMap
70 // class MUST NOT be directly used by the user.
71 
72 template <typename T>
74 {
75 protected:
76  static_assert(std::is_fundamental<T>::value,
77  "G4TAtomicHitsMap must use fundamental type");
78 
79 public:
80  typedef G4atomic<T> value_type;
82  typedef typename std::map<G4int, mapped_type> container_type;
83  typedef typename container_type::iterator iterator;
84  typedef typename container_type::const_iterator const_iterator;
85 
86 public:
88 
89 public: // with description
90  G4TAtomicHitsMap(G4String detName, G4String colNam);
91  // constructor.
92 
93 public:
94  virtual ~G4TAtomicHitsMap();
98 
99 public: // with description
100  virtual void DrawAllHits();
101  virtual void PrintAllHits();
102  // These two methods invokes Draw() and Print() methods of all of
103  // hit objects stored in this map, respectively.
104 
105 public: // with description
106  inline value_type* operator[](G4int key) const;
107 
108  // Returns a pointer to a concrete hit object.
109  inline container_type* GetMap() const
110  { return theCollection; }
111  // Returns a collection map.
112  inline G4int add(const G4int & key, value_type*& aHit) const;
113  inline G4int add(const G4int & key, T& aHit) const;
114  // Insert a hit object. Total number of hit objects stored in this
115  // map is returned.
116  inline G4int set(const G4int & key, value_type*& aHit) const;
117  inline G4int set(const G4int & key, T& aHit) const;
118  // Overwrite a hit object. Total number of hit objects stored in this
119  // map is returned.
120  inline G4int entries() const
121  {
122  return theCollection->size();
123  }
124  // Returns the number of hit objects stored in this map
125  inline void clear();
126 
127 public:
128  virtual G4VHit* GetHit(size_t) const {return 0;}
129  virtual size_t GetSize() const
130  {
131  return theCollection->size();
132  }
133 
134 public:
135  iterator begin() { return theCollection->begin(); }
136  iterator end() { return theCollection->end(); }
137 
138  const_iterator begin() const { return theCollection->begin(); }
139  const_iterator end() const { return theCollection->end(); }
140 
141  const_iterator cbegin() const { return theCollection->cbegin(); }
142  const_iterator cend() const { return theCollection->cend(); }
143 
144  iterator find(G4int p) { return theCollection->find(p); }
145  const_iterator find(G4int p) const { return theCollection->find(p); }
146 
147 private:
149  mutable G4Mutex fMutex;
150 
151 };
152 
153 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
154 template <typename T>
156  : theCollection(new container_type)
157 { }
158 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
159 template <typename T>
161  G4String colNam)
162  : G4VHitsCollection(detName,colNam),
163  theCollection(new container_type)
164 { }
165 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
166 template <typename T>
168 {
169  for(auto itr = theCollection->begin(); itr != theCollection->end(); itr++)
170  delete itr->second;
171 
172  delete theCollection;
173 }
174 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
175 template <typename T>
177 {
178  return (collectionName == right.collectionName);
179 }
180 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
181 template <typename T>
184 {
185  for(auto itr = rhs.GetMap()->begin(); itr != rhs.GetMap()->end(); itr++)
186  add(itr->first, *(itr->second));
187 
188  return (G4TAtomicHitsMap<T>&)(*this);
189 }
190 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
191 template <typename T>
194 {
195  for(auto itr = rhs.GetMap()->begin(); itr != rhs.GetMap()->end(); itr++)
196  add(itr->first, *(itr->second));
197 
198  return (G4TAtomicHitsMap<T>&)(*this);
199 }
200 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
201 template <typename T>
202 inline typename G4TAtomicHitsMap<T>::value_type*
204 {
205  if(theCollection->find(key) != theCollection->end())
206  return theCollection->find(key)->second;
207  else
208  {
209  G4AutoLock l(&fMutex);
210  if(theCollection->find(key) == theCollection->end())
211  {
212  value_type* ptr = new value_type;
213  (*theCollection)[key] = ptr;
214  return ptr;
215  } else
216  return theCollection->find(key)->second;
217  }
218 }
219 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
220 template <typename T>
221 inline G4int
222 G4TAtomicHitsMap<T>::add(const G4int& key, value_type*& aHit) const
223 {
224  if(theCollection->find(key) != theCollection->end())
225  *(*theCollection)[key] += *aHit;
226  else
227  {
228  G4AutoLock l(&fMutex);
229  (*theCollection)[key] = aHit;
230  }
231  G4AutoLock l(&fMutex);
232  return theCollection->size();
233 }
234 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
235 template <typename T>
236 inline G4int
237 G4TAtomicHitsMap<T>::add(const G4int& key, T& aHit) const
238 {
239 
240  if(theCollection->find(key) != theCollection->end())
241  *(*theCollection)[key] += aHit;
242  else
243  {
244  value_type* hit = new value_type;
245  *hit = aHit;
246  G4AutoLock l(&fMutex);
247  (*theCollection)[key] = hit;
248  }
249  G4AutoLock l(&fMutex);
250  return theCollection->size();
251 }
252 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
253 template <typename T>
254 inline G4int
255 G4TAtomicHitsMap<T>::set(const G4int& key, value_type*& aHit) const
256 {
257  if(theCollection->find(key) != theCollection->end())
258  delete (*theCollection)[key]->second;
259 
260  (*theCollection)[key] = aHit;
261  G4AutoLock l(&fMutex);
262  return theCollection->size();
263 }
264 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
265 template <typename T>
266 inline G4int
267 G4TAtomicHitsMap<T>::set(const G4int& key, T& aHit) const
268 {
269  if(theCollection->find(key) != theCollection->end())
270  *(*theCollection)[key] = aHit;
271  else
272  {
273  value_type* hit = new value_type;
274  *hit = aHit;
275  (*theCollection)[key] = hit;
276  }
277  G4AutoLock l(&fMutex);
278  return theCollection->size();
279 }
280 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
281 template <typename T>
283 { }
284 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
285 template <typename T>
287 {
288  G4cout << "G4TAtomicHitsMap " << SDname << " / " << collectionName << " --- "
289  << entries() << " entries" << G4endl;
290 }
291 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
292 template <typename T>
294 {
295  G4AutoLock l(&fMutex);
296 
297  for(auto itr = theCollection->begin(); itr != theCollection->end(); itr++)
298  delete itr->second;
299 
300  theCollection->clear();
301 
302 }
303 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
304 
305 #endif
container_type::const_iterator const_iterator
container_type * GetMap() const
virtual ~G4TAtomicHitsMap()
#define G4endl
Definition: G4ios.hh:61
iterator find(G4int p)
const char * p
Definition: xmltok.h:285
Definition: G4VHit.hh:48
container_type::iterator iterator
value_type * operator[](G4int key) const
container_type * theCollection
G4atomic< T > value_type
const_iterator end() const
std::map< G4int, mapped_type > container_type
value_type * mapped_type
virtual G4VHit * GetHit(size_t) const
virtual void PrintAllHits()
const XML_Char int const XML_Char * value
Definition: expat.h:331
virtual void DrawAllHits()
virtual size_t GetSize() const
G4int entries() const
const_iterator find(G4int p) const
G4TAtomicHitsMap< T > & operator+=(const G4TAtomicHitsMap< T > &right) const
int G4int
Definition: G4Types.hh:78
const_iterator cbegin() const
const_iterator begin() const
G4int operator==(const G4TAtomicHitsMap< T > &right) const
_Tp G4atomic
Definition: G4atomic.hh:265
G4GLOB_DLL std::ostream G4cout
G4int add(const G4int &key, value_type *&aHit) const
const_iterator cend() const
G4int set(const G4int &key, value_type *&aHit) const
std::map< G4int, _Tp * > * GetMap() const
Definition: G4THitsMap.hh:117
Definition of the G4atomic class.
std::mutex G4Mutex
Definition: G4Threading.hh:84