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 G4ATTRIBUTEFILTERT_HH
0032 #define G4ATTRIBUTEFILTERT_HH
0033
0034 #include "G4AttDef.hh"
0035 #include "G4AttFilterUtils.hh"
0036 #include "G4AttUtils.hh"
0037 #include "G4AttValue.hh"
0038 #include "G4SmartFilter.hh"
0039 #include "G4VAttValueFilter.hh"
0040 #include <vector>
0041
0042 template <typename T>
0043 class G4AttributeFilterT : public G4SmartFilter<T> {
0044
0045 public:
0046
0047
0048 G4AttributeFilterT(const G4String& name = "Unspecified");
0049
0050
0051 virtual ~G4AttributeFilterT();
0052
0053
0054 virtual bool Evaluate(const T&) const;
0055
0056
0057 virtual void Print(std::ostream& ostr) const;
0058
0059
0060 virtual void Clear();
0061
0062
0063 void Set(const G4String& name);
0064 void AddInterval(const G4String&);
0065 void AddValue(const G4String&);
0066
0067 private:
0068
0069 enum Config {Interval, SingleValue};
0070
0071 typedef std::pair<G4String, Config> Pair;
0072 typedef std::vector<Pair> ConfigVect;
0073
0074
0075 G4String fAttName;
0076 ConfigVect fConfigVect;
0077
0078
0079 mutable G4bool fFirst;
0080 mutable G4VAttValueFilter* filter;
0081
0082 };
0083
0084 template <typename T>
0085 G4AttributeFilterT<T>::G4AttributeFilterT(const G4String& name)
0086 :G4SmartFilter<T>(name)
0087 ,fAttName("")
0088 ,fFirst(true)
0089 ,filter(0)
0090 {}
0091
0092 template <typename T>
0093 G4AttributeFilterT<T>::~G4AttributeFilterT()
0094 {
0095 delete filter;
0096 }
0097
0098 template <typename T>
0099 G4bool
0100 G4AttributeFilterT<T>::Evaluate(const T& object) const
0101 {
0102
0103 if (fAttName.empty()) return true;
0104
0105
0106 if (fConfigVect.size() == 0) return true;
0107
0108 if (fFirst) {
0109
0110 fFirst = false;
0111
0112
0113 G4AttDef attDef;
0114
0115
0116 if (!G4AttUtils::ExtractAttDef(object, fAttName, attDef)) {
0117 static G4bool warnedUnableToExtract = false;
0118 if (!warnedUnableToExtract) {
0119 G4ExceptionDescription ed;
0120 ed <<"Unable to extract attribute definition named "<<fAttName<<'\n'
0121 << "Available attributes:\n"
0122 << *object.GetAttDefs();
0123 G4Exception
0124 ("G4AttributeFilterT::Evaluate", "modeling0102", JustWarning, ed, "Invalid attribute definition");
0125 warnedUnableToExtract = true;
0126 }
0127 return false;
0128 }
0129
0130
0131 filter = G4AttFilterUtils::GetNewFilter(attDef);
0132
0133
0134 typename ConfigVect::const_iterator iter = fConfigVect.begin();
0135
0136 while (iter != fConfigVect.end()) {
0137 if (iter->second == G4AttributeFilterT<T>::Interval) {filter->LoadIntervalElement(iter->first);}
0138 else if (iter->second == G4AttributeFilterT<T>::SingleValue) {filter->LoadSingleValueElement(iter->first);}
0139 iter++;
0140 }
0141 }
0142
0143
0144 G4AttValue attVal;
0145
0146
0147 if (!G4AttUtils::ExtractAttValue(object, fAttName, attVal)) {
0148 static G4bool warnedUnableToExtract = false;
0149 if (!warnedUnableToExtract) {
0150 G4ExceptionDescription ed;
0151 ed <<"Unable to extract attribute definition named "<<fAttName<<'\n'
0152 << "Available attributes:\n"
0153 << *object.GetAttDefs();
0154 G4Exception
0155 ("G4AttributeFilterT::Evaluate", "modeling0103", JustWarning, ed, "InvalidAttributeValue");
0156 warnedUnableToExtract = true;
0157 }
0158 return false;
0159 }
0160
0161 if (G4SmartFilter<T>::GetVerbose()) {
0162 G4cout<<"G4AttributeFilterT processing attribute named "<<fAttName;
0163 G4cout<<" with value "<<attVal.GetValue()<<G4endl;
0164 }
0165
0166
0167 return (filter->Accept(attVal));
0168 }
0169
0170 template <typename T>
0171 void
0172 G4AttributeFilterT<T>::Clear()
0173 {
0174 fConfigVect.clear();
0175 if (0 != filter) filter->Reset();
0176 }
0177
0178 template <typename T>
0179 void
0180 G4AttributeFilterT<T>::Print(std::ostream& ostr) const
0181 {
0182 ostr<<"Printing data for G4Attribute filter named: "<<G4VFilter<T>::Name()<<std::endl;
0183 ostr<<"Filtered attribute name: "<<fAttName<<std::endl;
0184 ostr<<"Printing sub filter data:"<<std::endl;
0185 if (0 != filter) filter->PrintAll(ostr);
0186 }
0187
0188 template <typename T>
0189 void
0190 G4AttributeFilterT<T>::Set(const G4String& name)
0191 {
0192 fAttName = name;
0193 }
0194
0195 template <typename T>
0196 void
0197 G4AttributeFilterT<T>::AddInterval(const G4String& interval)
0198 {
0199 std::pair<G4String, Config> myPair(interval, G4AttributeFilterT<T>::Interval);
0200
0201 typename ConfigVect::iterator iter = std::find(fConfigVect.begin(), fConfigVect.end(), myPair);
0202
0203 if (iter != fConfigVect.end()) {
0204 G4ExceptionDescription ed;
0205 ed <<"Interval "<< interval <<" already exists";
0206 G4Exception
0207 ("G4AttributeFilterT::AddInterval", "modeling0104", JustWarning, ed);
0208 return;
0209 }
0210
0211 fConfigVect.push_back(myPair);
0212 }
0213
0214 template <typename T>
0215 void
0216 G4AttributeFilterT<T>::AddValue(const G4String& value)
0217 {
0218 std::pair<G4String, Config> myPair(value, G4AttributeFilterT<T>::SingleValue);
0219
0220 typename ConfigVect::iterator iter = std::find(fConfigVect.begin(), fConfigVect.end(), myPair);
0221
0222 if (iter != fConfigVect.end()) {
0223 G4ExceptionDescription ed;
0224 ed <<"Single value "<< value <<" already exists";
0225 G4Exception
0226 ("G4AttributeFilterT::AddValue", "modeling0105", JustWarning, ed);
0227 return;
0228 }
0229 fConfigVect.push_back(myPair);
0230 }
0231
0232 #endif