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