CompoundUnitElement.h
1 /**************************************************************************************
2 Copyright 2015 Applied Research Associates, Inc.
3 Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 this file except in compliance with the License. You may obtain a copy of the License
5 at:
6 http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software distributed under
8 the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9 CONDITIONS OF ANY KIND, either express or implied. See the License for the
10 specific language governing permissions and limitations under the License.
11 **************************************************************************************/
12 
13 //----------------------------------------------------------------------------
19 //----------------------------------------------------------------------------
20 #pragma once
21 
22 #include <biogears/cdm/utils/unitconversion/SnapValue.h>
23 
24 namespace biogears {
26 public:
27  // The use of CSnapValue as the exponent type allows for fractional exponents
28  // without resulting in "almost integer" exponents. Why do we want fractional
29  // exponents? Well, it allows you to take roots of quantities.
30  // For example, if I have a square area given in units of acres and I want
31  // to know the length of the square, I can compute the square root with the result
32  // being expressed in units of acres^[1/2]. This is a perfectly legitimate unit of
33  // distance and can be converted to feet, meters, etc by our existing algorithm. The
34  // obvious point that one might make in response is that since any "square-rootable"
35  // unit must ultimately have dimensions with even exponents, why not just convert the
36  // unit to its expansion into fundamental units and divide those exponents, which must
37  // be even, by two? Well, that would either require collusion with client code to do
38  // the value conversion, or would require maintaining a separate fudge-factor within
39  // the CompoundUnit structure itself. This seems like a hack. After all, since any
40  // unit can be squared and cubed, it should be possible to un-square or un-cube any
41  // unit whose dimensions are perfect squares and cubes, respectively.
43 
44  // Need a parameterless constructor to be used in a vector
46  {
47  // nothing
48  }
49 
50  CCompoundUnitElement(int unitId, ExponentType exponent = 1.0, int prefixID = -1)
51  : m_iUnitID(unitId)
52  , m_CExponent(exponent)
53  , m_iPrefixID(prefixID){
54  // nothing
55  };
56 
58  : m_iUnitID(src.m_iUnitID)
60  , m_iPrefixID(src.m_iPrefixID){
61  // nothing
62  };
63 
65  {
66  if (this != &rhs) {
67  m_iUnitID = rhs.m_iUnitID;
70  }
71  return *this;
72  };
73 
74  // Setters/Getters
75  void SetUnitID(int unitID)
76  {
77  m_iUnitID = unitID;
78  };
79 
80  int GetUnitID() const
81  {
82  return m_iUnitID;
83  }
84 
85  void SetExponent(const ExponentType& exponent)
86  {
87  m_CExponent = exponent;
88  };
89 
90  void AddExponent(const ExponentType& exponent)
91  {
92  m_CExponent += exponent;
93  }
94 
95  void MultExponent(const ExponentType& exppwr)
96  {
97  m_CExponent *= exppwr;
98  }
99 
100  void SubtractExponent(const ExponentType& exponent)
101  {
102  m_CExponent -= exponent;
103  }
104 
105  const ExponentType& GetExponent() const
106  {
107  return m_CExponent;
108  }
109 
110  void SetPrefixID(int prefixID)
111  {
112  m_iPrefixID = prefixID;
113  }
114 
115  int GetPrefixID() const
116  {
117  return m_iPrefixID;
118  }
119 
120  // http://support.microsoft.com/kb/168958 says we need to define operators < and ==
121  // for this if we want to export the vector of these contained in CompoundUnit. It
122  // even says we can just return true if there's no sensible interpretation of
123  // "operator<" for this class.
124  bool operator<(const CCompoundUnitElement& ref) const
125  {
126  return true; // Dummy implementation
127  }
128 
129  bool operator==(const CCompoundUnitElement& ref) const
130  {
131  return ((m_iUnitID == ref.m_iUnitID) && (m_CExponent == ref.m_CExponent) && (m_iPrefixID == ref.m_iPrefixID));
132  }
133 
134  // Convenience method for moving a CompoundUnitElement from the numerator to
135  // the denominator, or vice versa
136  void Invert()
137  {
138  m_CExponent *= -1;
139  }
140 
141  double GetBigness() const;
142  double GetBias() const;
143  bool IsDecibel() const;
144 
145 private:
147  ExponentType m_CExponent;
149 };
150 }
CCompoundUnitElement(int unitId, ExponentType exponent=1.0, int prefixID=-1)
Definition: CompoundUnitElement.h:50
CCompoundUnitElement & operator=(const CCompoundUnitElement &rhs)
Definition: CompoundUnitElement.h:64
void SetUnitID(int unitID)
Definition: CompoundUnitElement.h:75
void SetExponent(const ExponentType &exponent)
Definition: CompoundUnitElement.h:85
void SetPrefixID(int prefixID)
Definition: CompoundUnitElement.h:110
double GetBias() const
Definition: CompoundUnitElement.cpp:41
double GetBigness() const
Definition: CompoundUnitElement.cpp:23
int m_iUnitID
Definition: CompoundUnitElement.h:146
void Invert()
Definition: CompoundUnitElement.h:136
bool IsDecibel() const
Definition: CompoundUnitElement.cpp:49
int m_iPrefixID
Definition: CompoundUnitElement.h:148
CCompoundUnitElement(const CCompoundUnitElement &src)
Definition: CompoundUnitElement.h:57
int GetUnitID() const
Definition: CompoundUnitElement.h:80
bool operator==(const CCompoundUnitElement &ref) const
Definition: CompoundUnitElement.h:129
void MultExponent(const ExponentType &exppwr)
Definition: CompoundUnitElement.h:95
CSnapValue ExponentType
Definition: CompoundUnitElement.h:42
int GetPrefixID() const
Definition: CompoundUnitElement.h:115
Definition: CompoundUnitElement.h:25
ExponentType m_CExponent
Definition: CompoundUnitElement.h:147
void SubtractExponent(const ExponentType &exponent)
Definition: CompoundUnitElement.h:100
Definition: SnapValue.h:46
CCompoundUnitElement()
Definition: CompoundUnitElement.h:45
const ExponentType & GetExponent() const
Definition: CompoundUnitElement.h:105
Definition: SEElectricalCircuit.h:18
void AddExponent(const ExponentType &exponent)
Definition: CompoundUnitElement.h:90
bool operator<(const CCompoundUnitElement &ref) const
Definition: CompoundUnitElement.h:124