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