source: trunk/source/visualization/modeling/include/G4AttributeFilterT.hh @ 1348

Last change on this file since 1348 was 1348, checked in by garnier, 13 years ago

update

File size: 7.1 KB
Line 
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.7 2010/12/11 16:41:11 allison 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
45template <typename T>
46class G4AttributeFilterT : public G4SmartFilter<T> {
47
48public:
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
70private:
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
88template <typename T>
89G4AttributeFilterT<T>::G4AttributeFilterT(const G4String& name)
90  :G4SmartFilter<T>(name)
91  ,fAttName("")
92  ,fFirst(true)
93  ,fWarnedMissingAttribute(false)
94  ,filter(0)
95{}
96
97template <typename T>
98G4AttributeFilterT<T>::~G4AttributeFilterT() 
99{
100  delete filter;
101}
102
103template <typename T>
104G4bool
105G4AttributeFilterT<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      static G4bool warnedUnableToExtract = false;
131      if (!warnedUnableToExtract) {
132        std::ostringstream o;
133        o <<"Unable to extract attribute definition named "<<fAttName;
134        G4Exception
135          ("G4AttributeFilterT::Evaluate", "InvalidAttributeDefinition", JustWarning, o.str().c_str());
136        G4cout << "Available attributes:\n"
137               << object.GetAttDefs();
138        warnedUnableToExtract = true;
139      }
140      return false;
141    }
142   
143    // Get new G4AttValue filter
144    filter = G4AttFilterUtils::GetNewFilter(attDef);
145
146    // Load both interval and single valued data.
147    typename ConfigVect::const_iterator iter = fConfigVect.begin();
148   
149    while (iter != fConfigVect.end()) {
150      if (iter->second == G4AttributeFilterT<T>::Interval) {filter->LoadIntervalElement(iter->first);}
151      else if (iter->second == G4AttributeFilterT<T>::SingleValue) {filter->LoadSingleValueElement(iter->first);}
152      iter++;
153    }
154  }
155
156  // Get attribute value
157  G4AttValue attVal;
158
159  // Expect value to exist
160  if (!G4AttUtils::ExtractAttValue(object, fAttName, attVal)) {
161    static G4bool warnedUnableToExtract = false;
162    if (!warnedUnableToExtract) {
163      std::ostringstream o;
164      o <<"Unable to extract attribute value named "<<fAttName;
165      G4Exception
166        ("G4AttributeFilterT::Evaluate", "InvalidAttributeValue", JustWarning, o.str().c_str());
167    }
168    G4cout << "Available attributes:\n"
169           << object.GetAttDefs();
170    warnedUnableToExtract = true;
171    return false;
172  }
173
174  if (G4SmartFilter<T>::GetVerbose()) {
175    G4cout<<"G4AttributeFilterT processing attribute named "<<fAttName;
176    G4cout<<" with value "<<attVal.GetValue()<<G4endl;
177  }
178
179  // Pass subfilter
180  return (filter->Accept(attVal));
181}
182
183template <typename T>
184void
185G4AttributeFilterT<T>::Clear()
186{
187  fConfigVect.clear();
188  if (0 != filter) filter->Reset();
189}
190
191template <typename T>
192void
193G4AttributeFilterT<T>::Print(std::ostream& ostr) const
194{
195  ostr<<"Printing data for G4Attribute filter named: "<<G4VFilter<T>::Name()<<std::endl;
196  ostr<<"Filtered attribute name: "<<fAttName<<std::endl;
197  ostr<<"Printing sub filter data:"<<std::endl;
198  if (0 != filter) filter->PrintAll(ostr);
199}
200
201template <typename T>
202void
203G4AttributeFilterT<T>::Set(const G4String& name)
204{
205  fAttName = name;
206}
207
208template <typename T>
209void
210G4AttributeFilterT<T>::AddInterval(const G4String& interval)
211{
212  std::pair<G4String, Config> myPair(interval, G4AttributeFilterT<T>::Interval);
213
214  typename ConfigVect::iterator iter = std::find(fConfigVect.begin(), fConfigVect.end(), myPair);
215 
216  if (iter != fConfigVect.end()) {
217    std::ostringstream o;
218    o <<"Interval "<< interval <<" already exists"<<std::endl;
219    G4Exception
220      ("G4AttributeFilterT::AddInterval", "InvalidInterval", FatalErrorInArgument, o.str().c_str());
221  }
222
223  fConfigVect.push_back(myPair);
224}
225
226template <typename T>
227void
228G4AttributeFilterT<T>::AddValue(const G4String& value)
229{
230  std::pair<G4String, Config> myPair(value, G4AttributeFilterT<T>::SingleValue);
231
232  typename ConfigVect::iterator iter = std::find(fConfigVect.begin(), fConfigVect.end(), myPair);
233 
234  if (iter != fConfigVect.end()) {
235    std::ostringstream o;
236    o <<"Single value "<< value <<" already exists"<<std::endl;
237    G4Exception
238      ("G4AttributeFilterT::AddValue", "InvalidValue", FatalErrorInArgument, o.str().c_str());
239  }
240  fConfigVect.push_back(myPair);
241}
242
243#endif
Note: See TracBrowser for help on using the repository browser.