Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4AttributeFilterT.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 //
26 // $Id: G4AttributeFilterT.hh 66870 2013-01-14 23:38:59Z adotti $
27 //
28 // Generic attribute filter.
29 //
30 // Jane Tinslay, May 2006
31 //
32 #ifndef G4ATTRIBUTEFILTERT_HH
33 #define G4ATTRIBUTEFILTERT_HH
34 
35 #include "G4AttDef.hh"
36 #include "G4AttFilterUtils.hh"
37 #include "G4AttUtils.hh"
38 #include "G4AttValue.hh"
39 #include "G4SmartFilter.hh"
40 #include "G4VAttValueFilter.hh"
41 #include <vector>
42 
43 template <typename T>
44 class G4AttributeFilterT : public G4SmartFilter<T> {
45 
46 public:
47 
48  // Construct with filter name
49  G4AttributeFilterT(const G4String& name = "Unspecified");
50 
51  // Destructor
52  virtual ~G4AttributeFilterT();
53 
54  // Evaluate
55  virtual bool Evaluate(const T&) const;
56 
57  // Print configuration
58  virtual void Print(std::ostream& ostr) const;
59 
60  // Clear filter
61  virtual void Clear();
62 
63  // Configuration functions
64  void Set(const G4String& name);
65  void AddInterval(const G4String&);
66  void AddValue(const G4String&);
67 
68 private:
69 
71 
72  typedef std::pair<G4String, Config> Pair;
73  typedef std::vector<Pair> ConfigVect;
74 
75  // Data members
78 
79  // Caching
80  mutable G4bool fFirst;
83 
84 };
85 
86 template <typename T>
88  :G4SmartFilter<T>(name)
89  ,fAttName("")
90  ,fFirst(true)
91  ,fWarnedMissingAttribute(false)
92  ,filter(0)
93 {}
94 
95 template <typename T>
97 {
98  delete filter;
99 }
100 
101 template <typename T>
102 G4bool
103 G4AttributeFilterT<T>::Evaluate(const T& object) const
104 {
105  // Return false if attribute name has not been set. Just print one warning.
106  if (fAttName.isNull()) {
107 
108  if (!fWarnedMissingAttribute) {
109  G4Exception("G4AttributeFilterT::Evaluate", "modeling0101", JustWarning, "Null attribute name");
110  fWarnedMissingAttribute = true;
111  }
112 
113  return false;
114  }
115 
116  if (fFirst) {
117 
118  fFirst = false;
119 
120  // Get attribute definition
121  G4AttDef attDef;
122 
123  // Expect definition to exist
124  if (!G4AttUtils::ExtractAttDef(object, fAttName, attDef)) {
125  static G4bool warnedUnableToExtract = false;
126  if (!warnedUnableToExtract) {
128  ed <<"Unable to extract attribute definition named "<<fAttName;
130  ("G4AttributeFilterT::Evaluate", "modeling0102", JustWarning, ed, "Invalid attribute definition");
131  G4cout << "Available attributes:\n"
132  << object.GetAttDefs();
133  warnedUnableToExtract = true;
134  }
135  return false;
136  }
137 
138  // Get new G4AttValue filter
139  filter = G4AttFilterUtils::GetNewFilter(attDef);
140 
141  // Load both interval and single valued data.
142  typename ConfigVect::const_iterator iter = fConfigVect.begin();
143 
144  while (iter != fConfigVect.end()) {
145  if (iter->second == G4AttributeFilterT<T>::Interval) {filter->LoadIntervalElement(iter->first);}
146  else if (iter->second == G4AttributeFilterT<T>::SingleValue) {filter->LoadSingleValueElement(iter->first);}
147  iter++;
148  }
149  }
150 
151  // Get attribute value
152  G4AttValue attVal;
153 
154  // Expect value to exist
155  if (!G4AttUtils::ExtractAttValue(object, fAttName, attVal)) {
156  static G4bool warnedUnableToExtract = false;
157  if (!warnedUnableToExtract) {
159  ed <<"Unable to extract attribute value named "<<fAttName;
161  ("G4AttributeFilterT::Evaluate", "modeling0103", JustWarning, ed, "InvalidAttributeValue");
162  G4cout << "Available attributes:\n"
163  << object.GetAttDefs();
164  warnedUnableToExtract = true;
165  }
166  return false;
167  }
168 
170  G4cout<<"G4AttributeFilterT processing attribute named "<<fAttName;
171  G4cout<<" with value "<<attVal.GetValue()<<G4endl;
172  }
173 
174  // Pass subfilter
175  return (filter->Accept(attVal));
176 }
177 
178 template <typename T>
179 void
181 {
182  fConfigVect.clear();
183  if (0 != filter) filter->Reset();
184 }
185 
186 template <typename T>
187 void
188 G4AttributeFilterT<T>::Print(std::ostream& ostr) const
189 {
190  ostr<<"Printing data for G4Attribute filter named: "<<G4VFilter<T>::Name()<<std::endl;
191  ostr<<"Filtered attribute name: "<<fAttName<<std::endl;
192  ostr<<"Printing sub filter data:"<<std::endl;
193  if (0 != filter) filter->PrintAll(ostr);
194 }
195 
196 template <typename T>
197 void
199 {
200  fAttName = name;
201 }
202 
203 template <typename T>
204 void
206 {
207  std::pair<G4String, Config> myPair(interval, G4AttributeFilterT<T>::Interval);
208 
209  typename ConfigVect::iterator iter = std::find(fConfigVect.begin(), fConfigVect.end(), myPair);
210 
211  if (iter != fConfigVect.end()) {
213  ed <<"Interval "<< interval <<" already exists";
215  ("G4AttributeFilterT::AddInterval", "modeling0104", JustWarning, ed);
216  return;
217  }
218 
219  fConfigVect.push_back(myPair);
220 }
221 
222 template <typename T>
223 void
225 {
226  std::pair<G4String, Config> myPair(value, G4AttributeFilterT<T>::SingleValue);
227 
228  typename ConfigVect::iterator iter = std::find(fConfigVect.begin(), fConfigVect.end(), myPair);
229 
230  if (iter != fConfigVect.end()) {
232  ed <<"Single value "<< value <<" already exists";
234  ("G4AttributeFilterT::AddValue", "modeling0105", JustWarning, ed);
235  return;
236  }
237  fConfigVect.push_back(myPair);
238 }
239 
240 #endif
const XML_Char * name
Definition: expat.h:151
G4VAttValueFilter * filter
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:45
void AddInterval(const G4String &)
#define G4endl
Definition: G4ios.hh:61
G4VAttValueFilter * GetNewFilter(const G4AttDef &def)
void AddValue(const G4String &)
bool G4bool
Definition: G4Types.hh:79
const XML_Char int const XML_Char * value
Definition: expat.h:331
std::vector< Pair > ConfigVect
G4String Name() const
Definition: G4VFilter.hh:81
std::pair< G4String, Config > Pair
virtual bool Evaluate(const T &) const
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
G4AttributeFilterT(const G4String &name="Unspecified")
G4GLOB_DLL std::ostream G4cout
void Set(const G4String &name)
G4bool ExtractAttValue(const T &object, const G4String &name, G4AttValue &attVal)
Definition: G4AttUtils.hh:76
G4bool ExtractAttDef(const T &object, const G4String &name, G4AttDef &def)
Definition: G4AttUtils.hh:62
virtual void Print(std::ostream &ostr) const
const G4String & GetValue() const
Definition: G4AttValue.hh:64