G4ConversionUtils.hh

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 // $Id$
00027 //
00028 // Jane Tinslay September 2006
00029 //
00030 // Conversion utility functions.
00031 //
00032 #ifndef G4CONVERSIONUTILS_HH
00033 #define G4CONVERSIONUTILS_HH
00034 
00035 #include "globals.hh"
00036 #include "G4DimensionedDouble.hh"
00037 #include "G4DimensionedThreeVector.hh"
00038 #include <sstream>
00039 
00040 namespace G4ConversionUtils 
00041 {
00042   // Generic single value istringstream conversion.
00043   // Returns false if conversion failed or if extra characters
00044   // exist in input.
00045   template <typename Value>
00046   G4bool Convert(const G4String& myInput, Value& output) 
00047   {
00048     G4String input(myInput);
00049     input = input.strip();
00050 
00051     std::istringstream is(input);
00052     char tester;
00053     
00054     return ((is >> output) && !is.get(tester));
00055   }
00056   
00057   // Conversion specialisations.
00058   template<>
00059   inline G4bool Convert(const G4String& myInput, G4DimensionedDouble& output) 
00060   {
00061     G4String input(myInput);
00062     input = input.strip();
00063 
00064     G4double value;
00065     G4String unit;
00066     
00067     std::istringstream is(input);
00068     char tester;
00069     
00070     if (!(is >> value >> unit) || is.get(tester)) return false;
00071 
00072     output = G4DimensionedDouble(value, unit);
00073     
00074     return true;      
00075   }
00076   
00077   template<> inline G4bool Convert(const G4String& myInput, 
00078                                    G4DimensionedThreeVector& output) 
00079   {
00080     G4String input(myInput);
00081     input = input.strip();
00082 
00083     G4double value1, value2, value3;
00084     G4String unit;
00085     
00086     std::istringstream is(input);
00087     char tester;
00088     
00089     if (!(is >> value1 >> value2 >> value3 >>unit) || is.get(tester)) return false;
00090 
00091     output = G4DimensionedThreeVector(G4ThreeVector(value1, value2, value3), unit);
00092     
00093     return true;      
00094   }
00095   
00096   template<> inline G4bool Convert(const G4String& myInput, G4ThreeVector& output) 
00097   {
00098     G4String input(myInput);
00099     input = input.strip();
00100 
00101     G4double value1, value2, value3;
00102     
00103     std::istringstream is(input);
00104     char tester;
00105 
00106     if (!(is >> value1 >> value2 >> value3) || is.get(tester)) return false;
00107     output = G4ThreeVector(value1, value2, value3);
00108 
00109     return true;
00110   }
00111   
00112   // Generic double value istringstream conversion.
00113   // Return false if conversion failed or if extra characters
00114   // exist in input.
00115   template <typename Value> G4bool Convert(const G4String& myInput, Value& value1, 
00116                                            Value& value2) 
00117   {
00118     G4String input(myInput);
00119     input = input.strip();
00120 
00121     std::istringstream is(input);
00122     char tester;
00123     
00124     return ((is >> value1 >> value2) && (!is.get(tester)));
00125   }
00126   
00127   // Conversion specialisations.
00128   template<> inline G4bool Convert(const G4String& myInput, G4DimensionedDouble& min, 
00129                                    G4DimensionedDouble& max) 
00130   {
00131     G4String input(myInput);
00132     input = input.strip();
00133 
00134     G4double valueMin, valueMax;  
00135     G4String unitsMin, unitsMax;
00136     
00137     std::istringstream is(input);
00138     char tester;
00139     
00140     if (!(is >> valueMin >> unitsMin >> valueMax >> unitsMax) || is.get(tester)) return false;;
00141     
00142     min = G4DimensionedDouble(valueMin, unitsMin);
00143     max = G4DimensionedDouble(valueMax, unitsMax);
00144     
00145     return true;      
00146   }
00147   
00148   template<> inline G4bool Convert(const G4String& myInput, G4DimensionedThreeVector& min, 
00149                                    G4DimensionedThreeVector& max) 
00150   {   
00151     G4String input(myInput);
00152     input = input.strip();
00153    
00154     G4double valueMinX, valueMinY, valueMinZ;
00155     G4double valueMaxX, valueMaxY, valueMaxZ;
00156     G4String unitMin, unitMax;
00157     
00158     std::istringstream is(input);
00159     char tester;
00160     
00161     if (!(is >> valueMinX >> valueMinY >> valueMinZ >> unitMin >> valueMaxX >> valueMaxY >> valueMaxZ >> unitMax)
00162         || is.get(tester)) return false;
00163     
00164       min = G4DimensionedThreeVector(G4ThreeVector(valueMinX, valueMinY, valueMinZ), unitMin);
00165       max = G4DimensionedThreeVector(G4ThreeVector(valueMaxX, valueMaxY, valueMaxZ), unitMax);
00166       
00167       return true;      
00168   }
00169   
00170   template<> inline G4bool Convert(const G4String& myInput, G4ThreeVector& min, 
00171                                    G4ThreeVector& max) 
00172   {      
00173     G4String input(myInput);
00174     input = input.strip();
00175 
00176     G4double valueMinX, valueMinY, valueMinZ;
00177     G4double valueMaxX, valueMaxY, valueMaxZ;
00178     
00179     std::istringstream is(input);
00180     char tester;
00181 
00182     if (!(is >> valueMinX >> valueMinY >> valueMinZ >> valueMaxX >> valueMaxY >> valueMaxZ)
00183         || is.get(tester)) return false;
00184     
00185     min = G4ThreeVector(valueMinX, valueMinY, valueMinZ);
00186     max = G4ThreeVector(valueMaxX, valueMaxY, valueMaxZ);
00187     
00188     return true;      
00189   }
00190 
00191 }
00192 
00193 #endif

Generated on Mon May 27 17:47:57 2013 for Geant4 by  doxygen 1.4.7