Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4AttributeFilterT.hh
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: 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 
70  enum Config {Interval, SingleValue};
71 
72  typedef std::pair<G4String, Config> Pair;
73  typedef std::vector<Pair> ConfigVect;
74 
75  // Data members
76  G4String fAttName;
77  ConfigVect fConfigVect;
78 
79  // Caching
80  mutable G4bool fFirst;
81  mutable G4bool fWarnedMissingAttribute;
82  mutable G4VAttValueFilter* filter;
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
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
pid_t filter
Definition: tracer.cxx:30
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
void AddInterval(const G4String &)
const XML_Char * name
virtual bool Evaluate(const T &) const
G4String Name() const
Definition: G4VFilter.hh:81
G4bool ExtractAttValue(const T &object, const G4String &name, G4AttValue &attVal)
Definition: G4AttUtils.hh:76
const G4String & GetValue() const
Definition: G4AttValue.hh:64
G4bool ExtractAttDef(const T &object, const G4String &name, G4AttDef &def)
Definition: G4AttUtils.hh:62
G4GLOB_DLL std::ostream G4cout
bool G4bool
Definition: G4Types.hh:79
void AddValue(const G4String &)
void Set(const G4String &name)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
virtual void Print(std::ostream &ostr) const
G4AttributeFilterT(const G4String &name="Unspecified")
const XML_Char int const XML_Char * value
#define G4endl
Definition: G4ios.hh:61
G4VAttValueFilter * GetNewFilter(const G4AttDef &def)