[830] | 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: G4DimensionedType.hh,v 1.3 2006/12/13 15:44:09 gunter Exp $ |
---|
[1228] | 27 | // GEANT4 tag $Name: geant4-09-03 $ |
---|
[830] | 28 | // |
---|
| 29 | // Generic dimensioned type. |
---|
| 30 | // |
---|
| 31 | // Jane Tinslay, September 2006 |
---|
| 32 | // |
---|
| 33 | #ifndef G4DIMENSIONEDTYPE_HH |
---|
| 34 | #define G4DIMENSIONEDTYPE_HH |
---|
| 35 | |
---|
| 36 | #include "globals.hh" |
---|
| 37 | #include "G4ConversionFatalError.hh" |
---|
| 38 | #include "G4String.hh" |
---|
| 39 | #include "G4UnitsTable.hh" |
---|
| 40 | #include <ostream> |
---|
| 41 | |
---|
| 42 | namespace { |
---|
| 43 | |
---|
| 44 | // Helper class |
---|
| 45 | class HasName{ |
---|
| 46 | public: |
---|
| 47 | HasName(const G4String& name): fName(name) {}; |
---|
| 48 | bool operator()(const G4UnitDefinition* value) const |
---|
| 49 | { |
---|
| 50 | return ((value->GetName() == fName) || (value->GetSymbol() == fName)); |
---|
| 51 | } |
---|
| 52 | private: |
---|
| 53 | G4String fName; |
---|
| 54 | }; |
---|
| 55 | |
---|
| 56 | // Get unit value from input string. Return value indicates if |
---|
| 57 | // conversion was successful. |
---|
| 58 | G4bool GetUnitValue(const G4String& unit, G4double& value) |
---|
| 59 | { |
---|
| 60 | // Get units table |
---|
| 61 | G4UnitsTable& unitTable = G4UnitDefinition::GetUnitsTable(); |
---|
| 62 | if (unitTable.size() == 0) G4UnitDefinition::BuildUnitsTable(); |
---|
| 63 | |
---|
| 64 | // Iterate over unit lists, searching for unit match |
---|
| 65 | G4UnitsTable::const_iterator iterTable = unitTable.begin(); |
---|
| 66 | |
---|
| 67 | HasName myUnit(unit); |
---|
| 68 | G4bool gotUnit(false); |
---|
| 69 | |
---|
| 70 | while (!gotUnit && (iterTable != unitTable.end())) { |
---|
| 71 | G4UnitsContainer unitContainer = (*iterTable)->GetUnitsList(); |
---|
| 72 | |
---|
| 73 | G4UnitsContainer::const_iterator iterUnits = |
---|
| 74 | std::find_if(unitContainer.begin(), unitContainer.end(), myUnit); |
---|
| 75 | |
---|
| 76 | if (iterUnits != unitContainer.end()) { |
---|
| 77 | value = (*iterUnits)->GetValue(); |
---|
| 78 | gotUnit = true; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | iterTable++; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | return gotUnit; |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | // Default error handling done through G4ConversionFatalError |
---|
| 89 | template <typename T, typename ConversionErrorPolicy = G4ConversionFatalError> |
---|
| 90 | class G4DimensionedType : public ConversionErrorPolicy { |
---|
| 91 | |
---|
| 92 | public: |
---|
| 93 | |
---|
| 94 | // Constructors |
---|
| 95 | G4DimensionedType(); |
---|
| 96 | G4DimensionedType(const T& value, const G4String& unit); |
---|
| 97 | |
---|
| 98 | // Destructor |
---|
| 99 | virtual ~G4DimensionedType(); |
---|
| 100 | |
---|
| 101 | // Accessors |
---|
| 102 | |
---|
| 103 | // Raw, undimensioned value |
---|
| 104 | T RawValue() const; |
---|
| 105 | |
---|
| 106 | // Unit string |
---|
| 107 | G4String Unit() const; |
---|
| 108 | |
---|
| 109 | // Dimensioned value - rawValue*converted unit |
---|
| 110 | T DimensionedValue() const; |
---|
| 111 | |
---|
| 112 | // Operators |
---|
| 113 | T operator()() const; |
---|
| 114 | bool operator == (const G4DimensionedType<T>& rhs) const; |
---|
| 115 | bool operator != (const G4DimensionedType<T>& rhs) const; |
---|
| 116 | bool operator < (const G4DimensionedType<T>& rhs) const; |
---|
| 117 | bool operator > (const G4DimensionedType<T>& rhs) const; |
---|
| 118 | |
---|
| 119 | private: |
---|
| 120 | |
---|
| 121 | // Data members |
---|
| 122 | T fValue; |
---|
| 123 | G4String fUnit; |
---|
| 124 | T fDimensionedValue; |
---|
| 125 | |
---|
| 126 | }; |
---|
| 127 | |
---|
| 128 | template <typename T, typename ConversionErrorPolicy> |
---|
| 129 | G4DimensionedType<T, ConversionErrorPolicy>::G4DimensionedType() |
---|
| 130 | :fValue(0) |
---|
| 131 | ,fUnit("Undefined") |
---|
| 132 | ,fDimensionedValue(0) |
---|
| 133 | {} |
---|
| 134 | |
---|
| 135 | template <typename T, typename ConversionErrorPolicy> |
---|
| 136 | G4DimensionedType<T, ConversionErrorPolicy>::G4DimensionedType(const T& value, const G4String& unit) |
---|
| 137 | :fValue(value) |
---|
| 138 | ,fUnit(unit) |
---|
| 139 | { |
---|
| 140 | G4double unitValue(0); |
---|
| 141 | |
---|
| 142 | // Convert unit string to unit value |
---|
| 143 | if (!GetUnitValue(unit, unitValue)) ConversionErrorPolicy::ReportError(unit, "Invalid unit"); |
---|
| 144 | |
---|
| 145 | fDimensionedValue = value*unitValue; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | template <typename T, typename ConversionErrorPolicy> |
---|
| 149 | G4DimensionedType<T, ConversionErrorPolicy>::~G4DimensionedType() {} |
---|
| 150 | |
---|
| 151 | template <typename T, typename ConversionErrorPolicy> |
---|
| 152 | T |
---|
| 153 | G4DimensionedType<T, ConversionErrorPolicy>::RawValue() const |
---|
| 154 | { |
---|
| 155 | return fValue; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | template <typename T, typename ConversionErrorPolicy> |
---|
| 159 | G4String |
---|
| 160 | G4DimensionedType<T, ConversionErrorPolicy>::Unit() const |
---|
| 161 | { |
---|
| 162 | return fUnit; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | template <typename T, typename ConversionErrorPolicy> |
---|
| 166 | T |
---|
| 167 | G4DimensionedType<T, ConversionErrorPolicy>::DimensionedValue() const |
---|
| 168 | { |
---|
| 169 | return fDimensionedValue; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | template <typename T, typename ConversionErrorPolicy> |
---|
| 173 | T |
---|
| 174 | G4DimensionedType<T, ConversionErrorPolicy>::operator()() const |
---|
| 175 | { |
---|
| 176 | return fDimensionedValue; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | template <typename T, typename ConversionErrorPolicy> |
---|
| 180 | bool |
---|
| 181 | G4DimensionedType<T, ConversionErrorPolicy>::operator == (const G4DimensionedType<T>& rhs) const |
---|
| 182 | { |
---|
| 183 | return fDimensionedValue == rhs.fDimensionedValue; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | template <typename T, typename ConversionErrorPolicy> |
---|
| 187 | bool |
---|
| 188 | G4DimensionedType<T, ConversionErrorPolicy>::operator != (const G4DimensionedType<T>& rhs) const |
---|
| 189 | { |
---|
| 190 | return fDimensionedValue != rhs.fDimensionedValue; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | template <typename T, typename ConversionErrorPolicy> |
---|
| 194 | bool |
---|
| 195 | G4DimensionedType<T, ConversionErrorPolicy>::operator < (const G4DimensionedType<T>& rhs) const |
---|
| 196 | { |
---|
| 197 | return fDimensionedValue < rhs.fDimensionedValue; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | template <typename T, typename ConversionErrorPolicy> |
---|
| 201 | bool |
---|
| 202 | G4DimensionedType<T, ConversionErrorPolicy>::operator > (const G4DimensionedType<T>& rhs) const |
---|
| 203 | { |
---|
| 204 | return fDimensionedValue > rhs.fDimensionedValue; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | template <typename M> |
---|
| 208 | std::ostream& operator << (std::ostream& os, const G4DimensionedType<M>& obj) { |
---|
| 209 | os << obj.RawValue()<<" "<<obj.Unit(); |
---|
| 210 | return os; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | #endif |
---|