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: G4AttributeFilterT.hh,v 1.6 2006/12/13 15:50:02 gunter Exp $ |
---|
27 | // GEANT4 tag $Name: $ |
---|
28 | // |
---|
29 | // Generic attribute filter. |
---|
30 | // |
---|
31 | // Jane Tinslay, May 2006 |
---|
32 | // |
---|
33 | #ifndef G4ATTRIBUTEFILTERT_HH |
---|
34 | #define G4ATTRIBUTEFILTERT_HH |
---|
35 | |
---|
36 | #include "G4AttDef.hh" |
---|
37 | #include "G4AttFilterUtils.hh" |
---|
38 | #include "G4AttUtils.hh" |
---|
39 | #include "G4AttValue.hh" |
---|
40 | #include "G4SmartFilter.hh" |
---|
41 | #include "G4VAttValueFilter.hh" |
---|
42 | #include <sstream> |
---|
43 | #include <vector> |
---|
44 | |
---|
45 | template <typename T> |
---|
46 | class G4AttributeFilterT : public G4SmartFilter<T> { |
---|
47 | |
---|
48 | public: |
---|
49 | |
---|
50 | // Construct with filter name |
---|
51 | G4AttributeFilterT(const G4String& name = "Unspecified"); |
---|
52 | |
---|
53 | // Destructor |
---|
54 | virtual ~G4AttributeFilterT(); |
---|
55 | |
---|
56 | // Evaluate |
---|
57 | virtual bool Evaluate(const T&) const; |
---|
58 | |
---|
59 | // Print configuration |
---|
60 | virtual void Print(std::ostream& ostr) const; |
---|
61 | |
---|
62 | // Clear filter |
---|
63 | virtual void Clear(); |
---|
64 | |
---|
65 | // Configuration functions |
---|
66 | void Set(const G4String& name); |
---|
67 | void AddInterval(const G4String&); |
---|
68 | void AddValue(const G4String&); |
---|
69 | |
---|
70 | private: |
---|
71 | |
---|
72 | enum Config {Interval, SingleValue}; |
---|
73 | |
---|
74 | typedef std::pair<G4String, Config> Pair; |
---|
75 | typedef std::vector<Pair> ConfigVect; |
---|
76 | |
---|
77 | // Data members |
---|
78 | G4String fAttName; |
---|
79 | ConfigVect fConfigVect; |
---|
80 | |
---|
81 | // Caching |
---|
82 | mutable G4bool fFirst; |
---|
83 | mutable G4bool fWarnedMissingAttribute; |
---|
84 | mutable G4VAttValueFilter* filter; |
---|
85 | |
---|
86 | }; |
---|
87 | |
---|
88 | template <typename T> |
---|
89 | G4AttributeFilterT<T>::G4AttributeFilterT(const G4String& name) |
---|
90 | :G4SmartFilter<T>(name) |
---|
91 | ,fAttName("") |
---|
92 | ,fFirst(true) |
---|
93 | ,fWarnedMissingAttribute(false) |
---|
94 | ,filter(0) |
---|
95 | {} |
---|
96 | |
---|
97 | template <typename T> |
---|
98 | G4AttributeFilterT<T>::~G4AttributeFilterT() |
---|
99 | { |
---|
100 | delete filter; |
---|
101 | } |
---|
102 | |
---|
103 | template <typename T> |
---|
104 | G4bool |
---|
105 | G4AttributeFilterT<T>::Evaluate(const T& object) const |
---|
106 | { |
---|
107 | // Return false if attribute name has not been set. Just print one warning. |
---|
108 | if (fAttName.isNull()) { |
---|
109 | |
---|
110 | if (!fWarnedMissingAttribute) { |
---|
111 | std::ostringstream o; |
---|
112 | o<<"Null attribute name"; |
---|
113 | G4Exception("G4AttributeFilterT::Evaluate", "NullAttributeName", JustWarning, o.str().c_str()); |
---|
114 | |
---|
115 | fWarnedMissingAttribute = true; |
---|
116 | } |
---|
117 | |
---|
118 | return false; |
---|
119 | } |
---|
120 | |
---|
121 | if (fFirst) { |
---|
122 | |
---|
123 | fFirst = false; |
---|
124 | |
---|
125 | // Get attribute definition |
---|
126 | G4AttDef attDef; |
---|
127 | |
---|
128 | // Expect definition to exist |
---|
129 | if (!G4AttUtils::ExtractAttDef(object, fAttName, attDef)) { |
---|
130 | std::ostringstream o; |
---|
131 | o <<"Unable to extract attribute definition named "<<fAttName; |
---|
132 | G4Exception |
---|
133 | ("G4AttributeFilterT::Evaluate", "InvalidAttributeDefinition", FatalErrorInArgument, o.str().c_str()); |
---|
134 | } |
---|
135 | |
---|
136 | // Get new G4AttValue filter |
---|
137 | filter = G4AttFilterUtils::GetNewFilter(attDef); |
---|
138 | |
---|
139 | // Load both interval and single valued data. |
---|
140 | typename ConfigVect::const_iterator iter = fConfigVect.begin(); |
---|
141 | |
---|
142 | while (iter != fConfigVect.end()) { |
---|
143 | if (iter->second == G4AttributeFilterT<T>::Interval) {filter->LoadIntervalElement(iter->first);} |
---|
144 | else if (iter->second == G4AttributeFilterT<T>::SingleValue) {filter->LoadSingleValueElement(iter->first);} |
---|
145 | iter++; |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | // Get attribute value |
---|
150 | G4AttValue attVal; |
---|
151 | |
---|
152 | // Expect value to exist |
---|
153 | if (!G4AttUtils::ExtractAttValue(object, fAttName, attVal)) { |
---|
154 | std::ostringstream o; |
---|
155 | o <<"Unable to extract attribute value named "<<fAttName; |
---|
156 | G4Exception |
---|
157 | ("G4AttributeFilterT::Evaluate", "InvalidAttributeValue", FatalErrorInArgument, o.str().c_str()); |
---|
158 | } |
---|
159 | |
---|
160 | if (G4SmartFilter<T>::GetVerbose()) { |
---|
161 | G4cout<<"G4AttributeFilterT processing attribute named "<<fAttName; |
---|
162 | G4cout<<" with value "<<attVal.GetValue()<<G4endl; |
---|
163 | } |
---|
164 | |
---|
165 | // Pass subfilter |
---|
166 | return (filter->Accept(attVal)); |
---|
167 | } |
---|
168 | |
---|
169 | template <typename T> |
---|
170 | void |
---|
171 | G4AttributeFilterT<T>::Clear() |
---|
172 | { |
---|
173 | fConfigVect.clear(); |
---|
174 | if (0 != filter) filter->Reset(); |
---|
175 | } |
---|
176 | |
---|
177 | template <typename T> |
---|
178 | void |
---|
179 | G4AttributeFilterT<T>::Print(std::ostream& ostr) const |
---|
180 | { |
---|
181 | ostr<<"Printing data for G4Attribute filter named: "<<G4VFilter<T>::Name()<<std::endl; |
---|
182 | ostr<<"Filtered attribute name: "<<fAttName<<std::endl; |
---|
183 | ostr<<"Printing sub filter data:"<<std::endl; |
---|
184 | if (0 != filter) filter->PrintAll(ostr); |
---|
185 | } |
---|
186 | |
---|
187 | template <typename T> |
---|
188 | void |
---|
189 | G4AttributeFilterT<T>::Set(const G4String& name) |
---|
190 | { |
---|
191 | fAttName = name; |
---|
192 | } |
---|
193 | |
---|
194 | template <typename T> |
---|
195 | void |
---|
196 | G4AttributeFilterT<T>::AddInterval(const G4String& interval) |
---|
197 | { |
---|
198 | std::pair<G4String, Config> myPair(interval, G4AttributeFilterT<T>::Interval); |
---|
199 | |
---|
200 | typename ConfigVect::iterator iter = std::find(fConfigVect.begin(), fConfigVect.end(), myPair); |
---|
201 | |
---|
202 | if (iter != fConfigVect.end()) { |
---|
203 | std::ostringstream o; |
---|
204 | o <<"Interval "<< interval <<" already exists"<<std::endl; |
---|
205 | G4Exception |
---|
206 | ("G4AttributeFilterT::AddInterval", "InvalidInterval", FatalErrorInArgument, o.str().c_str()); |
---|
207 | } |
---|
208 | |
---|
209 | fConfigVect.push_back(myPair); |
---|
210 | } |
---|
211 | |
---|
212 | template <typename T> |
---|
213 | void |
---|
214 | G4AttributeFilterT<T>::AddValue(const G4String& value) |
---|
215 | { |
---|
216 | std::pair<G4String, Config> myPair(value, G4AttributeFilterT<T>::SingleValue); |
---|
217 | |
---|
218 | typename ConfigVect::iterator iter = std::find(fConfigVect.begin(), fConfigVect.end(), myPair); |
---|
219 | |
---|
220 | if (iter != fConfigVect.end()) { |
---|
221 | std::ostringstream o; |
---|
222 | o <<"Single value "<< value <<" already exists"<<std::endl; |
---|
223 | G4Exception |
---|
224 | ("G4AttributeFilterT::AddValue", "InvalidValue", FatalErrorInArgument, o.str().c_str()); |
---|
225 | } |
---|
226 | fConfigVect.push_back(myPair); |
---|
227 | } |
---|
228 | |
---|
229 | #endif |
---|