Geant4-11
G4Accumulable.icc
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
27//
28// utilities
29//
30
31//
32// public functions
33//
34
35using namespace G4Accumulables;
36
37//_____________________________________________________________________________
38template <typename T>
39G4Accumulable<T>::G4Accumulable(const G4String& name, T initValue, G4MergeMode mergeMode)
40 : G4VAccumulable(name),
41 fValue(initValue),
42 fInitValue(initValue),
43 fMergeMode(mergeMode),
44 fMergeFunction(GetMergeFunction<T>(mergeMode))
45{}
46
47//_____________________________________________________________________________
48template <typename T>
49G4Accumulable<T>::G4Accumulable(T initValue, G4MergeMode mergeMode)
50 : G4VAccumulable(),
51 fValue(initValue),
52 fInitValue(initValue),
53 fMergeMode(mergeMode),
54 fMergeFunction(GetMergeFunction<T>(mergeMode))
55{}
56
57//_____________________________________________________________________________
58template <typename T>
59G4Accumulable<T>::G4Accumulable(const G4Accumulable& rhs)
60 : G4VAccumulable(rhs),
61 fValue(rhs.fValue),
62 fInitValue(rhs.fInitValue),
63 fMergeMode(rhs.fMergeMode),
64 fMergeFunction(rhs.fMergeFunction)
65{}
66
67//_____________________________________________________________________________
68template <typename T>
69G4Accumulable<T>::G4Accumulable(G4Accumulable&& rhs)
70 : G4VAccumulable(rhs),
71 fValue(std::move(rhs.fValue)),
72 fInitValue(std::move(rhs.fInitValue)),
73 fMergeMode(rhs.fMergeMode),
74 fMergeFunction(rhs.fMergeFunction)
75{}
76
77//_____________________________________________________________________________
78template <typename T>
79G4Accumulable<T>&
80G4Accumulable<T>::operator=(const G4Accumulable<T>& rhs)
81{
82 // check assignment to self
83 if (this == &rhs) return *this;
84
85 // base class assignment
86 G4VAccumulable::operator=(rhs);
87
88 // this class data assignment
89 fValue = rhs.fValue;
90 fInitValue = rhs.fInitValue;
91 fMergeMode = rhs.fMergeMode;
92 fMergeFunction = rhs.fMergeFunction;
93
94 return *this;
95}
96
97//_____________________________________________________________________________
98template <typename T>
99G4Accumulable<T>&
100G4Accumulable<T>::operator=(G4Accumulable<T>&& rhs)
101{
102 // check assignment to self
103 if (this == &rhs) return *this;
104
105 // base class assignment
106 G4VAccumulable::operator=(rhs);
107
108 // this class data assignment
109 fValue = std::move(rhs.fValue);
110 fInitValue = std::move(rhs.fInitValue);
111 fMergeMode = rhs.fMergeMode;
112 fMergeFunction = rhs.fMergeFunction;
113
114 return *this;
115}
116
117//_____________________________________________________________________________
118template <typename T>
119G4Accumulable<T>&
120G4Accumulable<T>::operator+=(const G4Accumulable<T>& rhs)
121{
122 // only update the value from rhs
123 fValue += rhs.fValue;
124 return *this;
125}
126
127//_____________________________________________________________________________
128template <typename T>
129G4Accumulable<T>&
130G4Accumulable<T>::operator*=(const G4Accumulable<T>& rhs)
131{
132 // only update the value from rhs
133 fValue *= rhs.fValue;
134 return *this;
135}
136
137//_____________________________________________________________________________
138template <typename T>
139G4Accumulable<T>&
140G4Accumulable<T>::operator=(const T& value)
141{
142 // only update the value
143 fValue = value;
144 return *this;
145}
146
147//_____________________________________________________________________________
148template <typename T>
149G4Accumulable<T>&
150G4Accumulable<T>::operator+=(const T& value)
151{
152 // only update the value
153 fValue += value;
154 return *this;
155}
156
157//_____________________________________________________________________________
158template <typename T>
159G4Accumulable<T>&
160G4Accumulable<T>::operator*=(const T& value)
161{
162 // only update the value from rhs
163 fValue *= value;
164 return *this;
165}
166
167//_____________________________________________________________________________
168template <typename T>
169G4Accumulable<T>
170G4Accumulable<T>::operator++(int)
171{
172 // postfix increment
173 G4Accumulable<T> temp = *this;
174 fValue++;
175 return temp;
176}
177
178//_____________________________________________________________________________
179template <typename T>
180G4Accumulable<T>&
181G4Accumulable<T>::operator++()
182{
183 // prefix increment
184 fValue++;
185 return *this;
186}
187
188//_____________________________________________________________________________
189template <typename T>
190void G4Accumulable<T>::Merge(const G4VAccumulable& other)
191{
192 // G4cout << "Merging other: " << other.GetName() << " " << static_cast<const G4Accumulable<T>&>(other).fValue << G4endl;
193 // G4cout << " to master: " << fName << " " << fValue << G4endl;
194 fValue = fMergeFunction(fValue, static_cast<const G4Accumulable<T>&>(other).fValue);
195 // G4cout << " new value: " << fName << " " << fValue << G4endl;
196}
197
198//_____________________________________________________________________________
199template <typename T>
200void G4Accumulable<T>::Reset()
201{
202 fValue = fInitValue;
203}
204
205//_____________________________________________________________________________
206template <typename T>
207T G4Accumulable<T>::GetValue() const
208{
209 return fValue;
210}