G4AttributeFilterT.hh

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 // $Id$
00027 //
00028 // Generic attribute filter.
00029 //
00030 // Jane Tinslay, May 2006
00031 //
00032 #ifndef G4ATTRIBUTEFILTERT_HH
00033 #define G4ATTRIBUTEFILTERT_HH
00034 
00035 #include "G4AttDef.hh"
00036 #include "G4AttFilterUtils.hh"
00037 #include "G4AttUtils.hh"
00038 #include "G4AttValue.hh"
00039 #include "G4SmartFilter.hh"
00040 #include "G4VAttValueFilter.hh"
00041 #include <vector>
00042 
00043 template <typename T>
00044 class G4AttributeFilterT : public G4SmartFilter<T> {
00045 
00046 public:
00047  
00048   // Construct with filter name
00049   G4AttributeFilterT(const G4String& name = "Unspecified");
00050   
00051   // Destructor
00052   virtual ~G4AttributeFilterT();
00053 
00054   // Evaluate
00055   virtual bool Evaluate(const T&) const;
00056 
00057   // Print configuration
00058   virtual void Print(std::ostream& ostr) const;
00059 
00060   // Clear filter
00061   virtual void Clear();
00062 
00063   // Configuration functions
00064   void Set(const G4String& name);
00065   void AddInterval(const G4String&);
00066   void AddValue(const G4String&);
00067 
00068 private:
00069 
00070   enum Config {Interval, SingleValue};
00071 
00072   typedef std::pair<G4String, Config> Pair;
00073   typedef std::vector<Pair> ConfigVect;
00074 
00075   // Data members
00076   G4String fAttName;
00077   ConfigVect fConfigVect;
00078 
00079   // Caching
00080   mutable G4bool fFirst;
00081   mutable G4bool fWarnedMissingAttribute;
00082   mutable G4VAttValueFilter* filter;
00083 
00084 };
00085 
00086 template <typename T>
00087 G4AttributeFilterT<T>::G4AttributeFilterT(const G4String& name)
00088   :G4SmartFilter<T>(name)
00089   ,fAttName("")
00090   ,fFirst(true)
00091   ,fWarnedMissingAttribute(false)
00092   ,filter(0)
00093 {}
00094 
00095 template <typename T>
00096 G4AttributeFilterT<T>::~G4AttributeFilterT() 
00097 {
00098   delete filter;
00099 }
00100 
00101 template <typename T>
00102 G4bool
00103 G4AttributeFilterT<T>::Evaluate(const T& object) const
00104 {
00105   // Return false if attribute name has not been set. Just print one warning.
00106   if (fAttName.isNull()) {
00107 
00108     if (!fWarnedMissingAttribute) {
00109       G4Exception("G4AttributeFilterT::Evaluate", "modeling0101", JustWarning, "Null attribute name");
00110       fWarnedMissingAttribute = true;
00111     }
00112     
00113     return false;
00114   }
00115   
00116   if (fFirst) {
00117 
00118     fFirst = false;
00119 
00120     // Get attribute definition
00121     G4AttDef attDef;
00122     
00123     // Expect definition to exist    
00124     if (!G4AttUtils::ExtractAttDef(object, fAttName, attDef)) {
00125       static G4bool warnedUnableToExtract = false;
00126       if (!warnedUnableToExtract) {
00127         G4ExceptionDescription ed;
00128         ed <<"Unable to extract attribute definition named "<<fAttName;
00129         G4Exception
00130           ("G4AttributeFilterT::Evaluate", "modeling0102", JustWarning, ed, "Invalid attribute definition");
00131         G4cout << "Available attributes:\n"
00132                << object.GetAttDefs();
00133         warnedUnableToExtract = true;
00134       }
00135       return false;
00136     }
00137     
00138     // Get new G4AttValue filter
00139     filter = G4AttFilterUtils::GetNewFilter(attDef);
00140 
00141     // Load both interval and single valued data.
00142     typename ConfigVect::const_iterator iter = fConfigVect.begin();
00143     
00144     while (iter != fConfigVect.end()) {
00145       if (iter->second == G4AttributeFilterT<T>::Interval) {filter->LoadIntervalElement(iter->first);}
00146       else if (iter->second == G4AttributeFilterT<T>::SingleValue) {filter->LoadSingleValueElement(iter->first);}
00147       iter++;
00148     }
00149   }
00150 
00151   // Get attribute value
00152   G4AttValue attVal;
00153 
00154   // Expect value to exist
00155   if (!G4AttUtils::ExtractAttValue(object, fAttName, attVal)) {
00156     static G4bool warnedUnableToExtract = false;
00157     if (!warnedUnableToExtract) {
00158       G4ExceptionDescription ed;
00159       ed <<"Unable to extract attribute value named "<<fAttName;
00160       G4Exception
00161         ("G4AttributeFilterT::Evaluate", "modeling0103", JustWarning, ed, "InvalidAttributeValue");
00162       G4cout << "Available attributes:\n"
00163              << object.GetAttDefs();
00164       warnedUnableToExtract = true;
00165     }
00166     return false;
00167   }
00168 
00169   if (G4SmartFilter<T>::GetVerbose()) {
00170     G4cout<<"G4AttributeFilterT processing attribute named "<<fAttName;
00171     G4cout<<" with value "<<attVal.GetValue()<<G4endl;
00172   }
00173 
00174   // Pass subfilter
00175   return (filter->Accept(attVal));
00176 }
00177 
00178 template <typename T>
00179 void
00180 G4AttributeFilterT<T>::Clear()
00181 {
00182   fConfigVect.clear();
00183   if (0 != filter) filter->Reset();
00184 }
00185 
00186 template <typename T>
00187 void
00188 G4AttributeFilterT<T>::Print(std::ostream& ostr) const
00189 {
00190   ostr<<"Printing data for G4Attribute filter named: "<<G4VFilter<T>::Name()<<std::endl;
00191   ostr<<"Filtered attribute name: "<<fAttName<<std::endl;
00192   ostr<<"Printing sub filter data:"<<std::endl;
00193   if (0 != filter) filter->PrintAll(ostr);
00194 }
00195 
00196 template <typename T>
00197 void
00198 G4AttributeFilterT<T>::Set(const G4String& name)
00199 {
00200   fAttName = name;
00201 }
00202 
00203 template <typename T>
00204 void
00205 G4AttributeFilterT<T>::AddInterval(const G4String& interval)
00206 {
00207   std::pair<G4String, Config> myPair(interval, G4AttributeFilterT<T>::Interval);
00208 
00209   typename ConfigVect::iterator iter = std::find(fConfigVect.begin(), fConfigVect.end(), myPair);
00210   
00211   if (iter != fConfigVect.end()) {
00212     G4ExceptionDescription ed;
00213     ed <<"Interval "<< interval <<" already exists";
00214     G4Exception
00215       ("G4AttributeFilterT::AddInterval", "modeling0104", JustWarning, ed);
00216     return;
00217   }
00218 
00219   fConfigVect.push_back(myPair);
00220 }
00221 
00222 template <typename T>
00223 void
00224 G4AttributeFilterT<T>::AddValue(const G4String& value)
00225 {
00226   std::pair<G4String, Config> myPair(value, G4AttributeFilterT<T>::SingleValue);
00227 
00228   typename ConfigVect::iterator iter = std::find(fConfigVect.begin(), fConfigVect.end(), myPair);
00229   
00230   if (iter != fConfigVect.end()) {
00231     G4ExceptionDescription ed;
00232     ed <<"Single value "<< value <<" already exists";
00233     G4Exception
00234       ("G4AttributeFilterT::AddValue", "modeling0105", JustWarning, ed);
00235     return;
00236   }
00237   fConfigVect.push_back(myPair);
00238 }
00239 
00240 #endif

Generated on Mon May 27 17:47:41 2013 for Geant4 by  doxygen 1.4.7