Geant4 Cross Reference |
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 // 27 // Templated class for G4AttValue filters. 28 // 29 // Jane Tinslay, September 2006 30 // 31 #ifndef G4ATTVALUEFILTERT_HH 32 #define G4ATTVALUEFILTERT_HH 33 34 #include "G4AttValue.hh" 35 #include "G4VAttValueFilter.hh" 36 #include "G4ConversionFatalError.hh" 37 #include "G4ConversionUtils.hh" 38 39 #include <utility> 40 41 namespace { 42 43 // Helper classes 44 template <typename T> 45 class IsEqual{ 46 public: 47 IsEqual(const T& value): fValue(value) {}; 48 bool operator()(const std::pair<const G4String, T>& myPair) const 49 { 50 return myPair.second == fValue; 51 } 52 private: 53 T fValue; 54 }; 55 56 template <typename T> 57 class InInterval{ 58 public: 59 InInterval(const T& value): fValue(value) {}; 60 bool operator()(const std::pair<const G4String, std::pair<T, T> >& myPair) const 61 { 62 T min = myPair.second.first; 63 T max = myPair.second.second; 64 return ((fValue > min || fValue == min) && (fValue < max)); 65 } 66 private: 67 T fValue; 68 }; 69 70 } 71 72 template <typename T, typename ConversionErrorPolicy = G4ConversionFatalError> 73 class G4AttValueFilterT : public ConversionErrorPolicy, public G4VAttValueFilter { 74 public: 75 76 // Constructor 77 G4AttValueFilterT(); 78 79 // Destructor 80 virtual ~G4AttValueFilterT(); 81 82 // Filter methods 83 G4bool Accept(const G4AttValue& attVal) const; 84 G4bool GetValidElement(const G4AttValue& input, G4String& interval) const; 85 86 // Print configuration 87 virtual void PrintAll(std::ostream& ostr) const; 88 89 // Reset 90 virtual void Reset(); 91 92 void LoadIntervalElement(const G4String& input); 93 void LoadSingleValueElement(const G4String& input); 94 95 private: 96 97 typedef std::pair<T, T> Pair; 98 typedef typename std::map<G4String, Pair> IntervalMap; 99 typedef std::map<G4String, T> SingleValueMap; 100 101 102 // Data members 103 IntervalMap fIntervalMap; 104 SingleValueMap fSingleValueMap; 105 106 }; 107 108 template <typename T, typename ConversionErrorPolicy> 109 G4AttValueFilterT<T, ConversionErrorPolicy>::G4AttValueFilterT() {} 110 111 template <typename T, typename ConversionErrorPolicy> 112 G4AttValueFilterT<T, ConversionErrorPolicy>::~G4AttValueFilterT() {} 113 114 template <typename T, typename ConversionErrorPolicy> 115 G4bool 116 G4AttValueFilterT<T, ConversionErrorPolicy>::GetValidElement(const G4AttValue& attValue, G4String& element) const 117 { 118 T value{}; 119 120 G4String input = attValue.GetValue(); 121 if (!G4ConversionUtils::Convert(input, value)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?"); 122 123 typename SingleValueMap::const_iterator iterValues = 124 std::find_if(fSingleValueMap.begin(), fSingleValueMap.end(), IsEqual<T>(value)); 125 126 if (iterValues != fSingleValueMap.end()) { 127 element = iterValues->first; 128 return true; 129 } 130 131 typename IntervalMap::const_iterator iterIntervals = 132 std::find_if(fIntervalMap.begin(), fIntervalMap.end(), InInterval<T>(value)); 133 134 if (iterIntervals != fIntervalMap.end()) { 135 element = iterIntervals->first; 136 return true; 137 } 138 139 return false; 140 } 141 142 template <typename T, typename ConversionErrorPolicy> 143 G4bool 144 G4AttValueFilterT<T, ConversionErrorPolicy>::Accept(const G4AttValue& attValue) const 145 { 146 T value{}; 147 148 G4String input = attValue.GetValue(); 149 if (!G4ConversionUtils::Convert(input, value)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?"); 150 151 typename SingleValueMap::const_iterator iterValues = 152 std::find_if(fSingleValueMap.begin(), fSingleValueMap.end(), IsEqual<T>(value)); 153 154 if (iterValues != fSingleValueMap.end()) return true; 155 156 typename IntervalMap::const_iterator iterIntervals = 157 std::find_if(fIntervalMap.begin(), fIntervalMap.end(), InInterval<T>(value)); 158 159 if (iterIntervals != fIntervalMap.end()) return true; 160 161 return false; 162 } 163 164 template <typename T, typename ConversionErrorPolicy> 165 void 166 G4AttValueFilterT<T, ConversionErrorPolicy>::LoadIntervalElement(const G4String& input) 167 { 168 T min{}; 169 T max{}; 170 171 if (!G4ConversionUtils::Convert(input, min, max)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?"); 172 173 fIntervalMap[input] = std::pair<T, T> (min, max); 174 } 175 176 template <typename T, typename ConversionErrorPolicy> 177 void 178 G4AttValueFilterT<T, ConversionErrorPolicy>::LoadSingleValueElement(const G4String& input) 179 { 180 T output{}; 181 182 if (!G4ConversionUtils::Convert(input, output)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?"); 183 184 fSingleValueMap[input] = std::move(output); 185 } 186 187 template <typename T, typename ConversionErrorPolicy> 188 void 189 G4AttValueFilterT<T, ConversionErrorPolicy>::PrintAll(std::ostream& ostr) const 190 { 191 ostr<<"Printing data for filter: "<<Name()<<std::endl; 192 193 ostr<<"Interval data:"<<std::endl; 194 195 typename IntervalMap::const_iterator iterIntervals = fIntervalMap.begin(); 196 197 while (iterIntervals != fIntervalMap.end()) { 198 ostr<<iterIntervals->second.first<<" : "<<iterIntervals->second.second<<std::endl; 199 iterIntervals++; 200 } 201 202 ostr<<"Single value data:"<<std::endl; 203 204 typename SingleValueMap::const_iterator iterValues = fSingleValueMap.begin(); 205 206 while (iterValues != fSingleValueMap.end()) { 207 ostr<<iterValues->second<<std::endl; 208 iterValues++; 209 } 210 } 211 212 template <typename T, typename ConversionErrorPolicy> 213 void 214 G4AttValueFilterT<T, ConversionErrorPolicy>::Reset() 215 { 216 fIntervalMap.clear(); 217 fSingleValueMap.clear(); 218 } 219 220 #endif 221