G4AttValueFilterT.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 // Templated class for G4AttValue filters.
00029 //
00030 // Jane Tinslay, September 2006
00031 //
00032 #ifndef G4ATTVALUEFILTERT_HH
00033 #define G4ATTVALUEFILTERT_HH
00034 
00035 #include "G4VAttValueFilter.hh"
00036 #include "G4ConversionFatalError.hh"
00037 #include "G4ConversionUtils.hh"
00038 
00039 namespace {
00040   
00041   // Helper classes
00042   template <typename T>
00043   class IsEqual{
00044   public:
00045     IsEqual(const T& value): fValue(value) {};
00046     bool operator()(const std::pair<const G4String, T>& myPair) const
00047     {
00048       return myPair.second == fValue;
00049     }
00050   private:
00051     T fValue;
00052   };
00053   
00054   template <typename T>
00055   class InInterval{
00056   public:
00057     InInterval(const T& value): fValue(value) {};
00058     bool operator()(const std::pair<const G4String, std::pair<T, T> >& myPair) const
00059     {
00060       T min = myPair.second.first;
00061       T max = myPair.second.second;
00062       return ((fValue > min || fValue == min) && (fValue < max));
00063     }
00064   private:
00065     T fValue;
00066   };
00067 
00068 }
00069 
00070 template <typename T, typename ConversionErrorPolicy = G4ConversionFatalError>
00071 class G4AttValueFilterT : public ConversionErrorPolicy, public G4VAttValueFilter {
00072 public:
00073 
00074   // Constructor
00075   G4AttValueFilterT();
00076 
00077   // Destructor
00078   virtual ~G4AttValueFilterT();
00079 
00080   // Filter methods
00081   G4bool Accept(const G4AttValue& attVal) const;
00082   G4bool GetValidElement(const G4AttValue& input, G4String& interval) const;
00083 
00084   // Print configuration
00085   virtual void PrintAll(std::ostream& ostr) const;
00086   
00087   // Reset 
00088   virtual void Reset();
00089 
00090   void LoadIntervalElement(const G4String& input);
00091   void LoadSingleValueElement(const G4String& input);
00092 
00093 private:
00094 
00095   typedef std::pair<T, T> Pair;
00096   typedef typename std::map<G4String, Pair> IntervalMap;
00097   typedef std::map<G4String, T> SingleValueMap;
00098 
00099 
00100   // Data members  
00101   IntervalMap fIntervalMap;
00102   SingleValueMap fSingleValueMap;
00103   
00104 };
00105 
00106 template <typename T, typename ConversionErrorPolicy> 
00107 G4AttValueFilterT<T, ConversionErrorPolicy>::G4AttValueFilterT() {}
00108 
00109 template <typename T, typename ConversionErrorPolicy> 
00110 G4AttValueFilterT<T, ConversionErrorPolicy>::~G4AttValueFilterT() {}
00111 
00112 template <typename T, typename ConversionErrorPolicy>
00113 G4bool 
00114 G4AttValueFilterT<T, ConversionErrorPolicy>::GetValidElement(const G4AttValue& attValue, G4String& element) const 
00115 {
00116   T value;
00117   
00118   G4String input = attValue.GetValue();
00119   if (!G4ConversionUtils::Convert(input, value)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?"); 
00120   
00121   typename SingleValueMap::const_iterator iterValues = 
00122     std::find_if(fSingleValueMap.begin(), fSingleValueMap.end(), IsEqual<T>(value));
00123 
00124   if (iterValues != fSingleValueMap.end()) {
00125     element = iterValues->first;
00126     return true;
00127   }
00128   
00129   typename IntervalMap::const_iterator iterIntervals = 
00130     std::find_if(fIntervalMap.begin(), fIntervalMap.end(), InInterval<T>(value));
00131 
00132   if (iterIntervals != fIntervalMap.end()) {
00133     element = iterIntervals->first;
00134     return true;
00135   }
00136 
00137   return false;
00138 }
00139 
00140 template <typename T, typename ConversionErrorPolicy>
00141 G4bool 
00142 G4AttValueFilterT<T, ConversionErrorPolicy>::Accept(const G4AttValue& attValue) const 
00143 {
00144   T value;
00145 
00146   G4String input = attValue.GetValue();
00147   if (!G4ConversionUtils::Convert(input, value)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?"); 
00148   
00149   typename SingleValueMap::const_iterator iterValues = 
00150     std::find_if(fSingleValueMap.begin(), fSingleValueMap.end(), IsEqual<T>(value));
00151 
00152   if (iterValues != fSingleValueMap.end()) return true;
00153   
00154   typename IntervalMap::const_iterator iterIntervals = 
00155     std::find_if(fIntervalMap.begin(), fIntervalMap.end(), InInterval<T>(value));
00156 
00157   if (iterIntervals != fIntervalMap.end()) return true;
00158 
00159   return false;
00160 }
00161 
00162 template <typename T, typename ConversionErrorPolicy>
00163 void 
00164 G4AttValueFilterT<T, ConversionErrorPolicy>::LoadIntervalElement(const G4String& input) 
00165 {
00166   T min;
00167   T max;
00168 
00169   if (!G4ConversionUtils::Convert(input, min, max)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?");
00170 
00171   std::pair<T, T> myPair(min, max);
00172   fIntervalMap[input] = myPair;
00173 }
00174 
00175 template <typename T, typename ConversionErrorPolicy>
00176 void 
00177 G4AttValueFilterT<T, ConversionErrorPolicy>::LoadSingleValueElement(const G4String& input) 
00178 {
00179   T output;
00180   
00181   if (!G4ConversionUtils::Convert(input, output)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?");
00182 
00183   fSingleValueMap[input] = output;
00184 }
00185 
00186 template <typename T, typename ConversionErrorPolicy>
00187 void 
00188 G4AttValueFilterT<T, ConversionErrorPolicy>::PrintAll(std::ostream& ostr) const
00189 {
00190   ostr<<"Printing data for filter: "<<Name()<<std::endl;
00191 
00192   ostr<<"Interval data:"<<std::endl;
00193 
00194   typename IntervalMap::const_iterator iterIntervals = fIntervalMap.begin();
00195  
00196   while (iterIntervals != fIntervalMap.end()) {
00197     ostr<<iterIntervals->second.first<<" : "<<iterIntervals->second.second<<std::endl;
00198     iterIntervals++;
00199   }
00200 
00201   ostr<<"Single value data:"<<std::endl;
00202 
00203   typename SingleValueMap::const_iterator iterValues = fSingleValueMap.begin();
00204  
00205   while (iterValues != fSingleValueMap.end()) {
00206     ostr<<iterValues->second<<std::endl;
00207     iterValues++;
00208   }
00209 }
00210 
00211 template <typename T, typename ConversionErrorPolicy>
00212 void 
00213 G4AttValueFilterT<T, ConversionErrorPolicy>::Reset() 
00214 {
00215   fIntervalMap.clear();
00216   fSingleValueMap.clear();
00217 }
00218 
00219 #endif

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