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