Geant4 Cross Reference |
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 // Generic dimensioned type. 28 // 29 // Jane Tinslay, September 2006 30 // 31 #ifndef G4DIMENSIONEDTYPE_HH 32 #define G4DIMENSIONEDTYPE_HH 33 34 #include "globals.hh" 35 #include "G4ConversionFatalError.hh" 36 #include "G4String.hh" 37 #include "G4UnitsTable.hh" 38 #include <ostream> 39 40 namespace G4DimensionedTypeUtils 41 { 42 G4bool GetUnitValue(const G4String& unit, G4double& value); 43 } 44 45 // Default error handling done through G4ConversionFatalError 46 template <typename T, typename ConversionErrorPolicy = G4ConversionFatalError> 47 class G4DimensionedType : public ConversionErrorPolicy { 48 49 public: 50 51 // Constructors 52 G4DimensionedType(); 53 G4DimensionedType(const T& value, const G4String& unit); 54 55 // Destructor 56 virtual ~G4DimensionedType(); 57 58 // Accessors 59 60 // Raw, undimensioned value 61 T RawValue() const; 62 63 // Unit string 64 G4String Unit() const; 65 66 // Dimensioned value - rawValue*converted unit 67 T DimensionedValue() const; 68 69 // Operators 70 T operator()() const; 71 bool operator == (const G4DimensionedType<T>& rhs) const; 72 bool operator != (const G4DimensionedType<T>& rhs) const; 73 bool operator < (const G4DimensionedType<T>& rhs) const; 74 bool operator > (const G4DimensionedType<T>& rhs) const; 75 76 private: 77 78 // Data members 79 T fValue; 80 G4String fUnit; 81 T fDimensionedValue; 82 83 }; 84 85 template <typename T, typename ConversionErrorPolicy> 86 G4DimensionedType<T, ConversionErrorPolicy>::G4DimensionedType() 87 :fValue(0) 88 ,fUnit("Undefined") 89 ,fDimensionedValue(0) 90 {} 91 92 template <typename T, typename ConversionErrorPolicy> 93 G4DimensionedType<T, ConversionErrorPolicy>::G4DimensionedType(const T& value, const G4String& unit) 94 :fValue(value) 95 ,fUnit(unit) 96 { 97 G4double unitValue(0); 98 99 // Convert unit string to unit value 100 if (!G4DimensionedTypeUtils::GetUnitValue(unit, unitValue)) ConversionErrorPolicy::ReportError(unit, "Invalid unit"); 101 102 fDimensionedValue = value*unitValue; 103 } 104 105 template <typename T, typename ConversionErrorPolicy> 106 G4DimensionedType<T, ConversionErrorPolicy>::~G4DimensionedType() = default; 107 108 template <typename T, typename ConversionErrorPolicy> 109 T 110 G4DimensionedType<T, ConversionErrorPolicy>::RawValue() const 111 { 112 return fValue; 113 } 114 115 template <typename T, typename ConversionErrorPolicy> 116 G4String 117 G4DimensionedType<T, ConversionErrorPolicy>::Unit() const 118 { 119 return fUnit; 120 } 121 122 template <typename T, typename ConversionErrorPolicy> 123 T 124 G4DimensionedType<T, ConversionErrorPolicy>::DimensionedValue() const 125 { 126 return fDimensionedValue; 127 } 128 129 template <typename T, typename ConversionErrorPolicy> 130 T 131 G4DimensionedType<T, ConversionErrorPolicy>::operator()() const 132 { 133 return fDimensionedValue; 134 } 135 136 template <typename T, typename ConversionErrorPolicy> 137 bool 138 G4DimensionedType<T, ConversionErrorPolicy>::operator == (const G4DimensionedType<T>& rhs) const 139 { 140 return fDimensionedValue == rhs.fDimensionedValue; 141 } 142 143 template <typename T, typename ConversionErrorPolicy> 144 bool 145 G4DimensionedType<T, ConversionErrorPolicy>::operator != (const G4DimensionedType<T>& rhs) const 146 { 147 return fDimensionedValue != rhs.fDimensionedValue; 148 } 149 150 template <typename T, typename ConversionErrorPolicy> 151 bool 152 G4DimensionedType<T, ConversionErrorPolicy>::operator < (const G4DimensionedType<T>& rhs) const 153 { 154 return fDimensionedValue < rhs.fDimensionedValue; 155 } 156 157 template <typename T, typename ConversionErrorPolicy> 158 bool 159 G4DimensionedType<T, ConversionErrorPolicy>::operator > (const G4DimensionedType<T>& rhs) const 160 { 161 return fDimensionedValue > rhs.fDimensionedValue; 162 } 163 164 template <typename M> 165 std::ostream& operator << (std::ostream& os, const G4DimensionedType<M>& obj) { 166 os << obj.RawValue()<<" "<<obj.Unit(); 167 return os; 168 } 169 170 #endif 171