Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4ConversionUtils.hh
Go to the documentation of this file.
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: G4ConversionUtils.hh 78955 2014-02-05 09:45:46Z gcosmo $
27 //
28 // Jane Tinslay September 2006
29 //
30 // Conversion utility functions.
31 //
32 #ifndef G4CONVERSIONUTILS_HH
33 #define G4CONVERSIONUTILS_HH
34 
35 #include "globals.hh"
36 #include "G4DimensionedDouble.hh"
38 #include <sstream>
39 
40 namespace G4ConversionUtils
41 {
42  // Generic single value istringstream conversion.
43  // Returns false if conversion failed or if extra characters
44  // exist in input.
45  template <typename Value>
46  G4bool Convert(const G4String& myInput, Value& output)
47  {
48  G4String input(myInput);
49  input = input.strip();
50 
51  std::istringstream is(input);
52  char tester;
53 
54  return ((is >> output) && !is.get(tester));
55  }
56 
57  // Conversion specialisations.
58  template<>
59  inline G4bool Convert(const G4String& myInput, G4DimensionedDouble& output)
60  {
61  G4String input(myInput);
62  input = input.strip();
63 
65  G4String unit;
66 
67  std::istringstream is(input);
68  char tester;
69 
70  if (!(is >> value >> unit) || is.get(tester)) return false;
71 
72  output = G4DimensionedDouble(value, unit);
73 
74  return true;
75  }
76 
77  template<> inline G4bool Convert(const G4String& myInput,
78  G4DimensionedThreeVector& output)
79  {
80  G4String input(myInput);
81  input = input.strip();
82 
83  G4double value1, value2, value3;
84  G4String unit;
85 
86  std::istringstream is(input);
87  char tester;
88 
89  if (!(is >> value1 >> value2 >> value3 >>unit) || is.get(tester)) return false;
90 
91  output = G4DimensionedThreeVector(G4ThreeVector(value1, value2, value3), unit);
92 
93  return true;
94  }
95 
96  template<> inline G4bool Convert(const G4String& myInput, G4ThreeVector& output)
97  {
98  G4String input(myInput);
99  input = input.strip();
100 
101  G4double value1, value2, value3;
102 
103  std::istringstream is(input);
104  char tester;
105 
106  if (!(is >> value1 >> value2 >> value3) || is.get(tester)) return false;
107  output = G4ThreeVector(value1, value2, value3);
108 
109  return true;
110  }
111 
112  // Generic double value istringstream conversion.
113  // Return false if conversion failed or if extra characters
114  // exist in input.
115  template <typename Value> G4bool Convert(const G4String& myInput, Value& value1,
116  Value& value2)
117  {
118  G4String input(myInput);
119  input = input.strip();
120 
121  std::istringstream is(input);
122  char tester;
123 
124  return ((is >> value1 >> value2) && (!is.get(tester)));
125  }
126 
127  // Conversion specialisations.
128  template<> inline G4bool Convert(const G4String& myInput, G4DimensionedDouble& min,
130  {
131  G4String input(myInput);
132  input = input.strip();
133 
134  G4double valueMin, valueMax;
135  G4String unitsMin, unitsMax;
136 
137  std::istringstream is(input);
138  char tester;
139 
140  if (!(is >> valueMin >> unitsMin >> valueMax >> unitsMax) || is.get(tester)) return false;;
141 
142  min = G4DimensionedDouble(valueMin, unitsMin);
143  max = G4DimensionedDouble(valueMax, unitsMax);
144 
145  return true;
146  }
147 
148  template<> inline G4bool Convert(const G4String& myInput, G4DimensionedThreeVector& min,
150  {
151  G4String input(myInput);
152  input = input.strip();
153 
154  G4double valueMinX, valueMinY, valueMinZ;
155  G4double valueMaxX, valueMaxY, valueMaxZ;
156  G4String unitMin, unitMax;
157 
158  std::istringstream is(input);
159  char tester;
160 
161  if (!(is >> valueMinX >> valueMinY >> valueMinZ >> unitMin >> valueMaxX >> valueMaxY >> valueMaxZ >> unitMax)
162  || is.get(tester)) return false;
163 
164  min = G4DimensionedThreeVector(G4ThreeVector(valueMinX, valueMinY, valueMinZ), unitMin);
165  max = G4DimensionedThreeVector(G4ThreeVector(valueMaxX, valueMaxY, valueMaxZ), unitMax);
166 
167  return true;
168  }
169 
170  template<> inline G4bool Convert(const G4String& myInput, G4ThreeVector& min,
171  G4ThreeVector& max)
172  {
173  G4String input(myInput);
174  input = input.strip();
175 
176  G4double valueMinX, valueMinY, valueMinZ;
177  G4double valueMaxX, valueMaxY, valueMaxZ;
178 
179  std::istringstream is(input);
180  char tester;
181 
182  if (!(is >> valueMinX >> valueMinY >> valueMinZ >> valueMaxX >> valueMaxY >> valueMaxZ)
183  || is.get(tester)) return false;
184 
185  min = G4ThreeVector(valueMinX, valueMinY, valueMinZ);
186  max = G4ThreeVector(valueMaxX, valueMaxY, valueMaxZ);
187 
188  return true;
189  }
190 
191 }
192 
193 #endif
G4DimensionedType< G4double > G4DimensionedDouble
CLHEP::Hep3Vector G4ThreeVector
G4String strip(G4int strip_Type=trailing, char c=' ')
G4DimensionedType< G4ThreeVector > G4DimensionedThreeVector
const XML_Char int const XML_Char * value
Definition: expat.h:331
bool G4bool
Definition: G4Types.hh:79
T max(const T t1, const T t2)
brief Return the largest of the two arguments
T min(const T t1, const T t2)
brief Return the smallest of the two arguments
double G4double
Definition: G4Types.hh:76
G4bool Convert(const G4String &myInput, Value &output)