Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:58:06

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 //
0027 // Generic dimensioned type.
0028 //
0029 // Jane Tinslay, September 2006
0030 //
0031 #ifndef G4DIMENSIONEDTYPE_HH
0032 #define G4DIMENSIONEDTYPE_HH
0033 
0034 #include "globals.hh"
0035 #include "G4ConversionFatalError.hh"
0036 #include "G4String.hh"
0037 #include "G4UnitsTable.hh"
0038 #include <ostream>
0039 
0040 namespace G4DimensionedTypeUtils
0041 {
0042   G4bool GetUnitValue(const G4String& unit, G4double& value);
0043 }
0044 
0045 // Default error handling done through G4ConversionFatalError
0046 template <typename T, typename ConversionErrorPolicy = G4ConversionFatalError>
0047 class G4DimensionedType : public ConversionErrorPolicy {
0048 
0049 public:
0050 
0051   // Constructors
0052   G4DimensionedType();
0053   G4DimensionedType(const T& value, const G4String& unit);
0054 
0055   // Destructor
0056   virtual ~G4DimensionedType();
0057 
0058   // Accessors
0059 
0060   // Raw, undimensioned value
0061   T RawValue() const;
0062   
0063   // Unit string
0064   G4String Unit() const;
0065   
0066   // Dimensioned value - rawValue*converted unit
0067   T DimensionedValue() const;
0068 
0069   // Operators
0070   T operator()() const;
0071   bool operator == (const G4DimensionedType<T>& rhs) const;
0072   bool operator != (const G4DimensionedType<T>& rhs) const;
0073   bool operator < (const G4DimensionedType<T>& rhs) const;
0074   bool operator > (const G4DimensionedType<T>& rhs) const;
0075 
0076 private:
0077 
0078   // Data members
0079   T fValue;
0080   G4String fUnit;
0081   T fDimensionedValue;
0082 
0083 };
0084 
0085 template <typename T, typename ConversionErrorPolicy>
0086 G4DimensionedType<T, ConversionErrorPolicy>::G4DimensionedType()
0087   :fValue(0)
0088   ,fUnit("Undefined")
0089   ,fDimensionedValue(0) 
0090 {}
0091 
0092 template <typename T, typename ConversionErrorPolicy>
0093 G4DimensionedType<T, ConversionErrorPolicy>::G4DimensionedType(const T& value, const G4String& unit)
0094   :fValue(value)
0095   ,fUnit(unit)
0096 {
0097   G4double unitValue(0);
0098 
0099   // Convert unit string to unit value
0100   if (!G4DimensionedTypeUtils::GetUnitValue(unit, unitValue)) ConversionErrorPolicy::ReportError(unit, "Invalid unit");
0101 
0102   fDimensionedValue = value*unitValue;
0103 }
0104 
0105 template <typename T, typename ConversionErrorPolicy>
0106 G4DimensionedType<T, ConversionErrorPolicy>::~G4DimensionedType() = default;
0107 
0108 template <typename T, typename ConversionErrorPolicy>
0109 T
0110 G4DimensionedType<T, ConversionErrorPolicy>::RawValue() const
0111 {
0112   return fValue;
0113 }
0114 
0115 template <typename T, typename ConversionErrorPolicy>
0116 G4String
0117 G4DimensionedType<T, ConversionErrorPolicy>::Unit() const
0118 {
0119   return fUnit;
0120 }
0121 
0122 template <typename T, typename ConversionErrorPolicy>
0123 T
0124 G4DimensionedType<T, ConversionErrorPolicy>::DimensionedValue() const
0125 {
0126   return fDimensionedValue;
0127 }
0128 
0129 template <typename T, typename ConversionErrorPolicy>
0130 T 
0131 G4DimensionedType<T, ConversionErrorPolicy>::operator()() const 
0132 {
0133   return fDimensionedValue;
0134 }
0135 
0136 template <typename T, typename ConversionErrorPolicy>
0137 bool
0138 G4DimensionedType<T, ConversionErrorPolicy>::operator == (const G4DimensionedType<T>& rhs) const 
0139 {
0140   return fDimensionedValue == rhs.fDimensionedValue;
0141 }
0142 
0143 template <typename T, typename ConversionErrorPolicy>
0144 bool
0145 G4DimensionedType<T, ConversionErrorPolicy>::operator != (const G4DimensionedType<T>& rhs) const 
0146 {
0147   return fDimensionedValue != rhs.fDimensionedValue;
0148 }
0149 
0150 template <typename T, typename ConversionErrorPolicy>
0151 bool
0152 G4DimensionedType<T, ConversionErrorPolicy>::operator < (const G4DimensionedType<T>& rhs) const 
0153 {
0154   return fDimensionedValue < rhs.fDimensionedValue;
0155 }
0156 
0157 template <typename T, typename ConversionErrorPolicy>
0158 bool
0159 G4DimensionedType<T, ConversionErrorPolicy>::operator > (const G4DimensionedType<T>& rhs) const 
0160 {
0161   return fDimensionedValue > rhs.fDimensionedValue;
0162 }
0163 
0164 template <typename M>
0165 std::ostream& operator << (std::ostream& os, const G4DimensionedType<M>& obj) {
0166   os << obj.RawValue()<<" "<<obj.Unit();
0167   return os;
0168 }
0169 
0170 #endif