00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
00047 template <typename T, typename ConversionErrorPolicy = G4ConversionFatalError>
00048 class G4DimensionedType : public ConversionErrorPolicy {
00049
00050 public:
00051
00052
00053 G4DimensionedType();
00054 G4DimensionedType(const T& value, const G4String& unit);
00055
00056
00057 virtual ~G4DimensionedType();
00058
00059
00060
00061
00062 T RawValue() const;
00063
00064
00065 G4String Unit() const;
00066
00067
00068 T DimensionedValue() const;
00069
00070
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
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
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