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