Geant4-11
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//
27// Jane Tinslay September 2006
28//
29// Conversion utility functions.
30//
31#ifndef G4CONVERSIONUTILS_HH
32#define G4CONVERSIONUTILS_HH
33
34#include "globals.hh"
37#include <sstream>
38
40{
41 // Generic single value istringstream conversion.
42 // Returns false if conversion failed or if extra characters
43 // exist in input.
44 template <typename Value>
45 G4bool Convert(const G4String& myInput, Value& output)
46 {
47 G4String input = G4StrUtil::strip_copy(myInput);
48
49 std::istringstream is(input);
50 char tester;
51
52 return ((is >> output) && !is.get(tester));
53 }
54
55 // Conversion specialisations.
56 template<>
57 inline G4bool Convert(const G4String& myInput, G4DimensionedDouble& output)
58 {
59 G4String input = G4StrUtil::strip_copy(myInput);
60
61 G4double value;
62 G4String unit;
63
64 std::istringstream is(input);
65 char tester;
66
67 if (!(is >> value >> unit) || is.get(tester)) return false;
68
69 output = G4DimensionedDouble(value, unit);
70
71 return true;
72 }
73
74 template<> inline G4bool Convert(const G4String& myInput,
76 {
77 G4String input = G4StrUtil::strip_copy(myInput);
78
79 G4double value1, value2, value3;
80 G4String unit;
81
82 std::istringstream is(input);
83 char tester;
84
85 if (!(is >> value1 >> value2 >> value3 >>unit) || is.get(tester)) return false;
86
87 output = G4DimensionedThreeVector(G4ThreeVector(value1, value2, value3), unit);
88
89 return true;
90 }
91
92 template<> inline G4bool Convert(const G4String& myInput, G4ThreeVector& output)
93 {
94 G4String input = G4StrUtil::strip_copy(myInput);
95
96 G4double value1, value2, value3;
97
98 std::istringstream is(input);
99 char tester;
100
101 if (!(is >> value1 >> value2 >> value3) || is.get(tester)) return false;
102 output = G4ThreeVector(value1, value2, value3);
103
104 return true;
105 }
106
107 // Generic double value istringstream conversion.
108 // Return false if conversion failed or if extra characters
109 // exist in input.
110 template <typename Value> G4bool Convert(const G4String& myInput, Value& value1,
111 Value& value2)
112 {
113 G4String input = G4StrUtil::strip_copy(myInput);
114
115 std::istringstream is(input);
116 char tester;
117
118 return ((is >> value1 >> value2) && (!is.get(tester)));
119 }
120
121 // Conversion specialisations.
122 template<> inline G4bool Convert(const G4String& myInput, G4DimensionedDouble& min,
124 {
125 G4String input = G4StrUtil::strip_copy(myInput);
126
127 G4double valueMin, valueMax;
128 G4String unitsMin, unitsMax;
129
130 std::istringstream is(input);
131 char tester;
132
133 if (!(is >> valueMin >> unitsMin >> valueMax >> unitsMax) || is.get(tester)) return false;;
134
135 min = G4DimensionedDouble(valueMin, unitsMin);
136 max = G4DimensionedDouble(valueMax, unitsMax);
137
138 return true;
139 }
140
141 template<> inline G4bool Convert(const G4String& myInput, G4DimensionedThreeVector& min,
143 {
144 G4String input = G4StrUtil::strip_copy(myInput);
145
146 G4double valueMinX, valueMinY, valueMinZ;
147 G4double valueMaxX, valueMaxY, valueMaxZ;
148 G4String unitMin, unitMax;
149
150 std::istringstream is(input);
151 char tester;
152
153 if (!(is >> valueMinX >> valueMinY >> valueMinZ >> unitMin >> valueMaxX >> valueMaxY >> valueMaxZ >> unitMax)
154 || is.get(tester)) return false;
155
156 min = G4DimensionedThreeVector(G4ThreeVector(valueMinX, valueMinY, valueMinZ), unitMin);
157 max = G4DimensionedThreeVector(G4ThreeVector(valueMaxX, valueMaxY, valueMaxZ), unitMax);
158
159 return true;
160 }
161
162 template<> inline G4bool Convert(const G4String& myInput, G4ThreeVector& min,
164 {
165 G4String input = G4StrUtil::strip_copy(myInput);
166
167 G4double valueMinX, valueMinY, valueMinZ;
168 G4double valueMaxX, valueMaxY, valueMaxZ;
169
170 std::istringstream is(input);
171 char tester;
172
173 if (!(is >> valueMinX >> valueMinY >> valueMinZ >> valueMaxX >> valueMaxY >> valueMaxZ)
174 || is.get(tester)) return false;
175
176 min = G4ThreeVector(valueMinX, valueMinY, valueMinZ);
177 max = G4ThreeVector(valueMaxX, valueMaxY, valueMaxZ);
178
179 return true;
180 }
181
182}
183
184#endif
G4DimensionedType< G4double > G4DimensionedDouble
G4DimensionedType< G4ThreeVector > G4DimensionedThreeVector
CLHEP::Hep3Vector G4ThreeVector
double G4double
Definition: G4Types.hh:83
bool G4bool
Definition: G4Types.hh:86
G4bool Convert(const G4String &myInput, Value &output)
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
G4String strip_copy(G4String str, char c=' ')
Return copy of string with leading and trailing characters removed.