DUDS
Distributed Update of Data from Something
Unit.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2017 Jeff Jackowski
9  */
10 #include <duds/data/Unit.hpp>
11 #include <duds/general/Errors.hpp>
12 
13 namespace duds { namespace data {
14 
15 void Unit::setAmpere(int e) {
16  int v = general::SignExtend<4>(e);
17  if (v != e) {
19  BadUnit("Ampere"));
20  }
21  u = (u & 0xFFFFFFF0) | v;
22 }
23 
24 void Unit::setCandela(int e) {
25  int v = general::SignExtend<4>(e);
26  if (v != e) {
28  BadUnit("Candela"));
29  }
30  u = (u & 0xFFFFFF0F) | (v << 4);
31 }
32 
33 void Unit::setKelvin(int e) {
34  int v = general::SignExtend<4>(e);
35  if (v != e) {
37  BadUnit("Kelvin"));
38  }
39  u = (u & 0xFFFFF0FF) | (v << 8);
40 }
41 
42 void Unit::setKilogram(int e) {
43  int v = general::SignExtend<4>(e);
44  if (v != e) {
46  BadUnit("Kilogram"));
47  }
48  u = (u & 0xFFFF0FFF) | (v << 12);
49 }
50 
51 void Unit::setMeter(int e) {
52  int v = general::SignExtend<4>(e);
53  if (v != e) {
55  BadUnit("Meter"));
56  }
57  u = (u & 0xFFF0FFFF) | (v << 16);
58 }
59 
60 void Unit::setMole(int e) {
61  int v = general::SignExtend<4>(e);
62  if (v != e) {
64  BadUnit("Mole"));
65  }
66  u = (u & 0xFF0FFFFF) | (v << 20);
67 }
68 
69 void Unit::setSecond(int e) {
70  int v = general::SignExtend<4>(e);
71  if (v != e) {
73  BadUnit("Second"));
74  }
75  u = (u & 0xF0FFFFFF) | (v << 24);
76 }
77 
78 void Unit::setRadian(int e) {
79  int v = general::SignExtend<2>(e);
80  if (v != e) {
82  BadUnit("Radian"));
83  }
84  u = (u & 0xCFFFFFFF) | (v << 28);
85 }
86 
87 void Unit::setSteradian(int e) {
88  int v = general::SignExtend<2>(e);
89  if (v != e) {
91  BadUnit("Steradian"));
92  }
93  u = (u & 0x3FFFFFFF) | (v << 30);
94 }
95 
96 Unit::Unit(int A, int cd, int K, int kg, int m, int mol, int s,
97 int rad, int sr) {
98  setAmpere(A);
99  setCandela(cd);
100  setKelvin(K);
101  setKilogram(kg);
102  setMeter(m);
103  setMole(mol);
104  setSecond(s);
105  setRadian(rad);
106  setSteradian(sr);
107 }
108 
109 const Unit Unit::operator * (const Unit &U) const {
110  // the result
111  Unit r;
112  // set the result; may fail with exception
113  r.setAmpere(ampere() + U.ampere());
114  r.setCandela(candela() + U.candela());
115  r.setKelvin(kelvin() + U.kelvin());
116  r.setKilogram(kilogram() + U.kilogram());
117  r.setMeter(meter() + U.meter());
118  r.setMole(mole() + U.mole());
119  r.setSecond(second() + U.second());
120  r.setRadian(radian() + U.radian());
121  r.setSteradian(steradian() + U.steradian());
122  // return the result
123  return r;
124 }
125 
126 const Unit Unit::operator / (const Unit &U) const {
127  // the result
128  Unit r;
129  // set the result; may fail with exception
130  r.setAmpere(ampere() - U.ampere());
131  r.setCandela(candela() - U.candela());
132  r.setKelvin(kelvin() - U.kelvin());
133  r.setKilogram(kilogram() - U.kilogram());
134  r.setMeter(meter() - U.meter());
135  r.setMole(mole() - U.mole());
136  r.setSecond(second() - U.second());
137  r.setRadian(radian() - U.radian());
138  r.setSteradian(steradian() - U.steradian());
139  // return the result
140  return r;
141 }
142 
144  // a temporary
145  Unit n(*this * U); // may throw
146  // keep the new value
147  u = n.value();
148  return *this;
149 }
150 
152  // a temporary
153  Unit n(*this / U); // may throw
154  // keep the new value
155  u = n.value();
156  return *this;
157 }
158 
159 } }
Indicates that a value is beyond the range allowed by the Unit or ExtendedUnit class.
Definition: Unit.hpp:35
int cd() const
Returns the exponent for the candela (luminous intensity) dimension.
Definition: Unit.hpp:312
int ampere() const
Returns the exponent for the ampere (current) dimension.
Definition: Unit.hpp:240
int m() const
Returns the exponent for the meter (distance) dimension.
Definition: Unit.hpp:330
int rad() const
Returns the exponent for radians (angle).
Definition: Unit.hpp:348
const Unit operator*(const Unit &U) const
Combines two units into a new unit.
Definition: Unit.cpp:109
int second() const
Returns the exponent for the second (time) dimension.
Definition: Unit.hpp:276
void setSteradian(int e)
Sets the exponent for steradians (solid angle).
Definition: Unit.cpp:87
int kilogram() const
Returns the exponent for the kilogram (mass) dimension.
Definition: Unit.hpp:258
int steradian() const
Returns the exponent for steradians (solid angle).
Definition: Unit.hpp:288
int sr() const
Returns the exponent for steradians (solid angle).
Definition: Unit.hpp:354
void setKilogram(int e)
Sets the exponent for the kilogram (mass) dimension.
Definition: Unit.cpp:42
void setSecond(int e)
Sets the exponent for the second (time) dimension.
Definition: Unit.cpp:69
int radian() const
Returns the exponent for radians (angle).
Definition: Unit.hpp:282
std::int32_t value() const
Returns the internal exponent fields.
Definition: Unit.hpp:200
int meter() const
Returns the exponent for the meter (distance) dimension.
Definition: Unit.hpp:264
int mol() const
Returns the exponent for the mole (amount of substance) dimension.
Definition: Unit.hpp:336
int K() const
Returns the exponent for the kelvin (thermodynamic temperature) dimension.
Definition: Unit.hpp:318
void setAmpere(int e)
Sets the exponent for the ampere (current) dimension.
Definition: Unit.cpp:15
Unit & operator/=(const Unit &U)
Combines two units into a new unit.
Definition: Unit.cpp:151
void setKelvin(int e)
Sets the exponent for the kelvin (thermodynamic temperature) dimension.
Definition: Unit.cpp:33
Unit()=default
Construct the same as a std::int32_t; expect the value to be uninitalized.
int kg() const
Returns the exponent for the kilogram (mass) dimension.
Definition: Unit.hpp:324
Represents an SI unit, either base or derived.
Definition: Unit.hpp:178
boost::error_info< struct Info_unitname, std::string > BadUnit
The name of the bad unit.
Definition: Unit.hpp:45
void setMeter(int e)
Sets the exponent for the meter (distance) dimension.
Definition: Unit.cpp:51
boost::error_info< struct Info_unitexp, int > BadUnitExponent
The out-of-range exponent.
Definition: Unit.hpp:40
int kelvin() const
Returns the exponent for the kelvin (thermodynamic temperature) dimension.
Definition: Unit.hpp:252
int mole() const
Returns the exponent for the mole (amount of substance) dimension.
Definition: Unit.hpp:270
int A() const
Returns the exponent for the ampere (current) dimension.
Definition: Unit.hpp:306
int candela() const
Returns the exponent for the candela (luminous intensity) dimension.
Definition: Unit.hpp:246
void setMole(int e)
Sets the exponent for the mole (amount of substance) dimension.
Definition: Unit.cpp:60
Unit & operator*=(const Unit &U)
Combines two units into a new unit.
Definition: Unit.cpp:143
std::int32_t u
Stores the exponent fields.
Definition: Unit.hpp:182
General errors.
int s() const
Returns the exponent for the second (time) dimension.
Definition: Unit.hpp:342
void setRadian(int e)
Sets the exponent for radians (angle).
Definition: Unit.cpp:78
#define DUDS_THROW_EXCEPTION(x)
Works like BOOST_THROW_EXCEPTION, but includes a stack trace if DUDS_ERRORS_VERBOSE is defined...
Definition: Errors.hpp:48
void setCandela(int e)
Sets the exponent for the candela (luminous intensity) dimension.
Definition: Unit.cpp:24
const Unit operator/(const Unit &U) const
Combines two units into a new unit.
Definition: Unit.cpp:126