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