File indexing completed on 2025-01-18 09:57:56
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 #ifndef G4ATTVALUEFILTERT_HH
0032 #define G4ATTVALUEFILTERT_HH
0033
0034 #include "G4AttValue.hh"
0035 #include "G4VAttValueFilter.hh"
0036 #include "G4ConversionFatalError.hh"
0037 #include "G4ConversionUtils.hh"
0038
0039 namespace {
0040
0041
0042 template <typename T>
0043 class IsEqual{
0044 public:
0045 IsEqual(const T& value): fValue(value) {};
0046 bool operator()(const std::pair<const G4String, T>& myPair) const
0047 {
0048 return myPair.second == fValue;
0049 }
0050 private:
0051 T fValue;
0052 };
0053
0054 template <typename T>
0055 class InInterval{
0056 public:
0057 InInterval(const T& value): fValue(value) {};
0058 bool operator()(const std::pair<const G4String, std::pair<T, T> >& myPair) const
0059 {
0060 T min = myPair.second.first;
0061 T max = myPair.second.second;
0062 return ((fValue > min || fValue == min) && (fValue < max));
0063 }
0064 private:
0065 T fValue;
0066 };
0067
0068 }
0069
0070 template <typename T, typename ConversionErrorPolicy = G4ConversionFatalError>
0071 class G4AttValueFilterT : public ConversionErrorPolicy, public G4VAttValueFilter {
0072 public:
0073
0074
0075 G4AttValueFilterT();
0076
0077
0078 virtual ~G4AttValueFilterT();
0079
0080
0081 G4bool Accept(const G4AttValue& attVal) const;
0082 G4bool GetValidElement(const G4AttValue& input, G4String& interval) const;
0083
0084
0085 virtual void PrintAll(std::ostream& ostr) const;
0086
0087
0088 virtual void Reset();
0089
0090 void LoadIntervalElement(const G4String& input);
0091 void LoadSingleValueElement(const G4String& input);
0092
0093 private:
0094
0095 typedef std::pair<T, T> Pair;
0096 typedef typename std::map<G4String, Pair> IntervalMap;
0097 typedef std::map<G4String, T> SingleValueMap;
0098
0099
0100
0101 IntervalMap fIntervalMap;
0102 SingleValueMap fSingleValueMap;
0103
0104 };
0105
0106 template <typename T, typename ConversionErrorPolicy>
0107 G4AttValueFilterT<T, ConversionErrorPolicy>::G4AttValueFilterT() {}
0108
0109 template <typename T, typename ConversionErrorPolicy>
0110 G4AttValueFilterT<T, ConversionErrorPolicy>::~G4AttValueFilterT() {}
0111
0112 template <typename T, typename ConversionErrorPolicy>
0113 G4bool
0114 G4AttValueFilterT<T, ConversionErrorPolicy>::GetValidElement(const G4AttValue& attValue, G4String& element) const
0115 {
0116 T value{};
0117
0118 G4String input = attValue.GetValue();
0119 if (!G4ConversionUtils::Convert(input, value)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?");
0120
0121 typename SingleValueMap::const_iterator iterValues =
0122 std::find_if(fSingleValueMap.begin(), fSingleValueMap.end(), IsEqual<T>(value));
0123
0124 if (iterValues != fSingleValueMap.end()) {
0125 element = iterValues->first;
0126 return true;
0127 }
0128
0129 typename IntervalMap::const_iterator iterIntervals =
0130 std::find_if(fIntervalMap.begin(), fIntervalMap.end(), InInterval<T>(value));
0131
0132 if (iterIntervals != fIntervalMap.end()) {
0133 element = iterIntervals->first;
0134 return true;
0135 }
0136
0137 return false;
0138 }
0139
0140 template <typename T, typename ConversionErrorPolicy>
0141 G4bool
0142 G4AttValueFilterT<T, ConversionErrorPolicy>::Accept(const G4AttValue& attValue) const
0143 {
0144 T value{};
0145
0146 G4String input = attValue.GetValue();
0147 if (!G4ConversionUtils::Convert(input, value)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?");
0148
0149 typename SingleValueMap::const_iterator iterValues =
0150 std::find_if(fSingleValueMap.begin(), fSingleValueMap.end(), IsEqual<T>(value));
0151
0152 if (iterValues != fSingleValueMap.end()) return true;
0153
0154 typename IntervalMap::const_iterator iterIntervals =
0155 std::find_if(fIntervalMap.begin(), fIntervalMap.end(), InInterval<T>(value));
0156
0157 if (iterIntervals != fIntervalMap.end()) return true;
0158
0159 return false;
0160 }
0161
0162 template <typename T, typename ConversionErrorPolicy>
0163 void
0164 G4AttValueFilterT<T, ConversionErrorPolicy>::LoadIntervalElement(const G4String& input)
0165 {
0166 T min{};
0167 T max{};
0168
0169 if (!G4ConversionUtils::Convert(input, min, max)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?");
0170
0171 std::pair<T, T> myPair(min, max);
0172 fIntervalMap[input] = myPair;
0173 }
0174
0175 template <typename T, typename ConversionErrorPolicy>
0176 void
0177 G4AttValueFilterT<T, ConversionErrorPolicy>::LoadSingleValueElement(const G4String& input)
0178 {
0179 T output{};
0180
0181 if (!G4ConversionUtils::Convert(input, output)) ConversionErrorPolicy::ReportError(input, "Invalid format. Was the input data formatted correctly ?");
0182
0183 fSingleValueMap[input] = output;
0184 }
0185
0186 template <typename T, typename ConversionErrorPolicy>
0187 void
0188 G4AttValueFilterT<T, ConversionErrorPolicy>::PrintAll(std::ostream& ostr) const
0189 {
0190 ostr<<"Printing data for filter: "<<Name()<<std::endl;
0191
0192 ostr<<"Interval data:"<<std::endl;
0193
0194 typename IntervalMap::const_iterator iterIntervals = fIntervalMap.begin();
0195
0196 while (iterIntervals != fIntervalMap.end()) {
0197 ostr<<iterIntervals->second.first<<" : "<<iterIntervals->second.second<<std::endl;
0198 iterIntervals++;
0199 }
0200
0201 ostr<<"Single value data:"<<std::endl;
0202
0203 typename SingleValueMap::const_iterator iterValues = fSingleValueMap.begin();
0204
0205 while (iterValues != fSingleValueMap.end()) {
0206 ostr<<iterValues->second<<std::endl;
0207 iterValues++;
0208 }
0209 }
0210
0211 template <typename T, typename ConversionErrorPolicy>
0212 void
0213 G4AttValueFilterT<T, ConversionErrorPolicy>::Reset()
0214 {
0215 fIntervalMap.clear();
0216 fSingleValueMap.clear();
0217 }
0218
0219 #endif