G4DimensionedType.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 // Generic dimensioned type.
00029 //
00030 // Jane Tinslay, September 2006
00031 //
00032 #ifndef G4DIMENSIONEDTYPE_HH
00033 #define G4DIMENSIONEDTYPE_HH
00034 
00035 #include "globals.hh"
00036 #include "G4ConversionFatalError.hh"
00037 #include "G4String.hh"
00038 #include "G4UnitsTable.hh"
00039 #include <ostream>
00040 
00041 namespace G4DimensionedTypeUtils
00042 {
00043   G4bool GetUnitValue(const G4String& unit, G4double& value);
00044 }
00045 
00046 // Default error handling done through G4ConversionFatalError
00047 template <typename T, typename ConversionErrorPolicy = G4ConversionFatalError>
00048 class G4DimensionedType : public ConversionErrorPolicy {
00049 
00050 public:
00051 
00052   // Constructors
00053   G4DimensionedType();
00054   G4DimensionedType(const T& value, const G4String& unit);
00055 
00056   // Destructor
00057   virtual ~G4DimensionedType();
00058 
00059   // Accessors
00060 
00061   // Raw, undimensioned value
00062   T RawValue() const;
00063   
00064   // Unit string
00065   G4String Unit() const;
00066   
00067   // Dimensioned value - rawValue*converted unit
00068   T DimensionedValue() const;
00069 
00070   // Operators
00071   T operator()() const;
00072   bool operator == (const G4DimensionedType<T>& rhs) const;
00073   bool operator != (const G4DimensionedType<T>& rhs) const;
00074   bool operator < (const G4DimensionedType<T>& rhs) const;
00075   bool operator > (const G4DimensionedType<T>& rhs) const;
00076 
00077 private:
00078 
00079   // Data members
00080   T fValue;
00081   G4String fUnit;
00082   T fDimensionedValue;
00083 
00084 };
00085 
00086 template <typename T, typename ConversionErrorPolicy>
00087 G4DimensionedType<T, ConversionErrorPolicy>::G4DimensionedType()
00088   :fValue(0)
00089   ,fUnit("Undefined")
00090   ,fDimensionedValue(0) 
00091 {}
00092 
00093 template <typename T, typename ConversionErrorPolicy>
00094 G4DimensionedType<T, ConversionErrorPolicy>::G4DimensionedType(const T& value, const G4String& unit)
00095   :fValue(value)
00096   ,fUnit(unit)
00097 {
00098   G4double unitValue(0);
00099 
00100   // Convert unit string to unit value
00101   if (!G4DimensionedTypeUtils::GetUnitValue(unit, unitValue)) ConversionErrorPolicy::ReportError(unit, "Invalid unit");
00102 
00103   fDimensionedValue = value*unitValue;
00104 }
00105 
00106 template <typename T, typename ConversionErrorPolicy>
00107 G4DimensionedType<T, ConversionErrorPolicy>::~G4DimensionedType() {}
00108 
00109 template <typename T, typename ConversionErrorPolicy>
00110 T
00111 G4DimensionedType<T, ConversionErrorPolicy>::RawValue() const
00112 {
00113   return fValue;
00114 }
00115 
00116 template <typename T, typename ConversionErrorPolicy>
00117 G4String
00118 G4DimensionedType<T, ConversionErrorPolicy>::Unit() const
00119 {
00120   return fUnit;
00121 }
00122 
00123 template <typename T, typename ConversionErrorPolicy>
00124 T
00125 G4DimensionedType<T, ConversionErrorPolicy>::DimensionedValue() const
00126 {
00127   return fDimensionedValue;
00128 }
00129 
00130 template <typename T, typename ConversionErrorPolicy>
00131 T 
00132 G4DimensionedType<T, ConversionErrorPolicy>::operator()() const 
00133 {
00134   return fDimensionedValue;
00135 }
00136 
00137 template <typename T, typename ConversionErrorPolicy>
00138 bool
00139 G4DimensionedType<T, ConversionErrorPolicy>::operator == (const G4DimensionedType<T>& rhs) const 
00140 {
00141   return fDimensionedValue == rhs.fDimensionedValue;
00142 }
00143 
00144 template <typename T, typename ConversionErrorPolicy>
00145 bool
00146 G4DimensionedType<T, ConversionErrorPolicy>::operator != (const G4DimensionedType<T>& rhs) const 
00147 {
00148   return fDimensionedValue != rhs.fDimensionedValue;
00149 }
00150 
00151 template <typename T, typename ConversionErrorPolicy>
00152 bool
00153 G4DimensionedType<T, ConversionErrorPolicy>::operator < (const G4DimensionedType<T>& rhs) const 
00154 {
00155   return fDimensionedValue < rhs.fDimensionedValue;
00156 }
00157 
00158 template <typename T, typename ConversionErrorPolicy>
00159 bool
00160 G4DimensionedType<T, ConversionErrorPolicy>::operator > (const G4DimensionedType<T>& rhs) const 
00161 {
00162   return fDimensionedValue > rhs.fDimensionedValue;
00163 }
00164 
00165 template <typename M>
00166 std::ostream& operator << (std::ostream& os, const G4DimensionedType<M>& obj) {
00167   os << obj.RawValue()<<" "<<obj.Unit();
00168   return os;
00169 }
00170 
00171 #endif

Generated on Mon May 27 17:48:01 2013 for Geant4 by  doxygen 1.4.7