Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4AttValueFilterT.hh
이 파일의 문서화 페이지로 가기
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 108059 2017-12-19 15:13:34Z gcosmo $
27 //
28 // Templated class for G4AttValue filters.
29 //
30 // Jane Tinslay, September 2006
31 //
32 #ifndef G4ATTVALUEFILTERT_HH
33 #define G4ATTVALUEFILTERT_HH
34 
35 #include "G4AttValue.hh"
36 #include "G4VAttValueFilter.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
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
104 
105 };
106 
107 template <typename T, typename ConversionErrorPolicy>
109 
110 template <typename T, typename ConversionErrorPolicy>
112 
113 template <typename T, typename ConversionErrorPolicy>
114 G4bool
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
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
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
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
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
215 {
216  fIntervalMap.clear();
217  fSingleValueMap.clear();
218 }
219 
220 #endif
T max(const T t1, const T t2)
brief Return the largest of the two arguments
virtual void Reset()
std::map< G4String, Pair > IntervalMap
G4bool Convert(const G4String &myInput, Value &output)
SingleValueMap fSingleValueMap
bool G4bool
Definition: G4Types.hh:79
const XML_Char int const XML_Char * value
Definition: expat.h:331
void LoadSingleValueElement(const G4String &input)
virtual void PrintAll(std::ostream &ostr) const
G4bool GetValidElement(const G4AttValue &input, G4String &interval) const
std::map< G4String, T > SingleValueMap
G4bool Accept(const G4AttValue &attVal) const
void LoadIntervalElement(const G4String &input)
std::pair< T, T > Pair
G4int first(char) const
T min(const T t1, const T t2)
brief Return the smallest of the two arguments
const G4String & GetValue() const
Definition: G4AttValue.hh:64