[834] | 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: G4AttValueFilterT.hh,v 1.3 2006/12/13 15:50:00 gunter Exp $ |
---|
[954] | 27 | // GEANT4 tag $Name: $ |
---|
[834] | 28 | // |
---|
| 29 | // Templated class for G4AttValue filters. |
---|
| 30 | // |
---|
| 31 | // Jane Tinslay, September 2006 |
---|
| 32 | // |
---|
| 33 | #ifndef G4ATTVALUEFILTERT_HH |
---|
| 34 | #define G4ATTVALUEFILTERT_HH |
---|
| 35 | |
---|
| 36 | #include "G4VAttValueFilter.hh" |
---|
| 37 | #include "G4ConversionFatalError.hh" |
---|
| 38 | #include "G4ConversionUtils.hh" |
---|
| 39 | |
---|
| 40 | namespace { |
---|
| 41 | |
---|
| 42 | // Helper classes |
---|
| 43 | template <typename T> |
---|
| 44 | class IsEqual{ |
---|
| 45 | public: |
---|
| 46 | IsEqual(const T& value): fValue(value) {}; |
---|
| 47 | bool operator()(const std::pair<const G4String, T>& myPair) const |
---|
| 48 | { |
---|
| 49 | return myPair.second == fValue; |
---|
| 50 | } |
---|
| 51 | private: |
---|
| 52 | T fValue; |
---|
| 53 | }; |
---|
| 54 | |
---|
| 55 | template <typename T> |
---|
| 56 | class InInterval{ |
---|
| 57 | public: |
---|
| 58 | InInterval(const T& value): fValue(value) {}; |
---|
| 59 | bool operator()(const std::pair<const G4String, std::pair<T, T> >& myPair) const |
---|
| 60 | { |
---|
| 61 | T min = myPair.second.first; |
---|
| 62 | T max = myPair.second.second; |
---|
| 63 | return ((fValue > min || fValue == min) && (fValue < max)); |
---|
| 64 | } |
---|
| 65 | private: |
---|
| 66 | T fValue; |
---|
| 67 | }; |
---|
| 68 | |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | template <typename T, typename ConversionErrorPolicy = G4ConversionFatalError> |
---|
| 72 | class G4AttValueFilterT : public ConversionErrorPolicy, public G4VAttValueFilter { |
---|
| 73 | public: |
---|
| 74 | |
---|
| 75 | // Constructor |
---|
| 76 | G4AttValueFilterT(); |
---|
| 77 | |
---|
| 78 | // Destructor |
---|
| 79 | virtual ~G4AttValueFilterT(); |
---|
| 80 | |
---|
| 81 | // Filter methods |
---|
| 82 | G4bool Accept(const G4AttValue& attVal) const; |
---|
| 83 | G4bool GetValidElement(const G4AttValue& input, G4String& interval) const; |
---|
| 84 | |
---|
| 85 | // Print configuration |
---|
| 86 | virtual void PrintAll(std::ostream& ostr) const; |
---|
| 87 | |
---|
| 88 | // Reset |
---|
| 89 | virtual void Reset(); |
---|
| 90 | |
---|
| 91 | void LoadIntervalElement(const G4String& input); |
---|
| 92 | void LoadSingleValueElement(const G4String& input); |
---|
| 93 | |
---|
| 94 | private: |
---|
| 95 | |
---|
| 96 | typedef std::pair<T, T> Pair; |
---|
| 97 | typedef typename std::map<G4String, Pair> IntervalMap; |
---|
| 98 | typedef std::map<G4String, T> SingleValueMap; |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | // Data members |
---|
| 102 | IntervalMap fIntervalMap; |
---|
| 103 | SingleValueMap fSingleValueMap; |
---|
| 104 | |
---|
| 105 | }; |
---|
| 106 | |
---|
| 107 | template <typename T, typename ConversionErrorPolicy> |
---|
| 108 | G4AttValueFilterT<T, ConversionErrorPolicy>::G4AttValueFilterT() {} |
---|
| 109 | |
---|
| 110 | template <typename T, typename ConversionErrorPolicy> |
---|
| 111 | G4AttValueFilterT<T, ConversionErrorPolicy>::~G4AttValueFilterT() {} |
---|
| 112 | |
---|
| 113 | template <typename T, typename ConversionErrorPolicy> |
---|
| 114 | G4bool |
---|
| 115 | G4AttValueFilterT<T, ConversionErrorPolicy>::GetValidElement(const G4AttValue& attValue, G4String& element) const |
---|
| 116 | { |
---|
| 117 | T value; |
---|
| 118 | |
---|
| 119 | G4String input = attValue.GetValue(); |
---|
| 120 | if (!G4ConversionUtils::Convert(input, value)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?"); |
---|
| 121 | |
---|
| 122 | typename SingleValueMap::const_iterator iterValues = |
---|
| 123 | std::find_if(fSingleValueMap.begin(), fSingleValueMap.end(), IsEqual<T>(value)); |
---|
| 124 | |
---|
| 125 | if (iterValues != fSingleValueMap.end()) { |
---|
| 126 | element = iterValues->first; |
---|
| 127 | return true; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | typename IntervalMap::const_iterator iterIntervals = |
---|
| 131 | std::find_if(fIntervalMap.begin(), fIntervalMap.end(), InInterval<T>(value)); |
---|
| 132 | |
---|
| 133 | if (iterIntervals != fIntervalMap.end()) { |
---|
| 134 | element = iterIntervals->first; |
---|
| 135 | return true; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | return false; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | template <typename T, typename ConversionErrorPolicy> |
---|
| 142 | G4bool |
---|
| 143 | G4AttValueFilterT<T, ConversionErrorPolicy>::Accept(const G4AttValue& attValue) const |
---|
| 144 | { |
---|
| 145 | T value; |
---|
| 146 | |
---|
| 147 | G4String input = attValue.GetValue(); |
---|
| 148 | if (!G4ConversionUtils::Convert(input, value)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?"); |
---|
| 149 | |
---|
| 150 | typename SingleValueMap::const_iterator iterValues = |
---|
| 151 | std::find_if(fSingleValueMap.begin(), fSingleValueMap.end(), IsEqual<T>(value)); |
---|
| 152 | |
---|
| 153 | if (iterValues != fSingleValueMap.end()) return true; |
---|
| 154 | |
---|
| 155 | typename IntervalMap::const_iterator iterIntervals = |
---|
| 156 | std::find_if(fIntervalMap.begin(), fIntervalMap.end(), InInterval<T>(value)); |
---|
| 157 | |
---|
| 158 | if (iterIntervals != fIntervalMap.end()) return true; |
---|
| 159 | |
---|
| 160 | return false; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | template <typename T, typename ConversionErrorPolicy> |
---|
| 164 | void |
---|
| 165 | G4AttValueFilterT<T, ConversionErrorPolicy>::LoadIntervalElement(const G4String& input) |
---|
| 166 | { |
---|
| 167 | T min; |
---|
| 168 | T max; |
---|
| 169 | |
---|
| 170 | if (!G4ConversionUtils::Convert(input, min, max)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?"); |
---|
| 171 | |
---|
| 172 | std::pair<T, T> myPair(min, max); |
---|
| 173 | fIntervalMap[input] = myPair; |
---|
| 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] = 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 |
---|