DUDS
Distributed Update of Data from Something
Quantity.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/Quantity.hpp>
11 
12 namespace duds { namespace data {
13 
15  if (unit != q.unit) {
17  }
18  return Quantity(value + q.value, unit);
19 }
20 
22  if (unit != q.unit) {
24  }
25  return Quantity(value - q.value, unit);
26 }
27 
29  if (unit != q.unit) {
31  }
32  value += q.value;
33  return *this;
34 }
35 
37  if (unit != q.unit) {
39  }
40  value -= q.value;
41  return *this;
42 }
43 
45  unit *= q.unit; // do first; may throw
46  value *= q.value;
47  return *this;
48 }
49 
51  unit *= q.unit; // do first; may throw
52  value *= q.value;
53  return *this;
54 }
55 
56 bool Quantity::operator < (const Quantity &q) const {
57  if (unit != q.unit) {
59  }
60  return value < q.value;
61 }
62 
63 bool Quantity::operator > (const Quantity &q) const {
64  if (unit != q.unit) {
66  }
67  return value > q.value;
68 }
69 
70 bool Quantity::operator <= (const Quantity &q) const {
71  if (unit != q.unit) {
73  }
74  return value <= q.value;
75 }
76 
77 bool Quantity::operator >= (const Quantity &q) const {
78  if (unit != q.unit) {
80  }
81  return value >= q.value;
82 }
83 /*
84 ::duds::time::interstellar::Femtoseconds &operator = (
85  ::duds::time::interstellar::Femtoseconds &fs, const Quantity &q
86 ) {
87  if (q.unit != units::Seconds) {
88  DUDS_THROW_EXCEPTION(UnitBadConversion() << BadUnit(q.unit));
89  }
90  fs = duds::time::interstellar::Femtoseconds(
91  duds::time::interstellar::Femtoseconds::rep(
92  q.value * (double)std::femto
93  )
94  );
95  return fs;
96 }
97 
98 ::duds::time::interstellar::Nanoseconds &operator = (
99  ::duds::time::interstellar::Nanoseconds &ns, const Quantity &q
100 ) {
101  if (q.unit != units::Seconds) {
102  DUDS_THROW_EXCEPTION(UnitBadConversion() << BadUnit(q.unit));
103  }
104  ns = duds::time::interstellar::Nanoseconds(
105  duds::time::interstellar::Nanoseconds::rep(
106  q.value * (double)std::nano
107  )
108  );
109  return ns;
110 }
111 */
112 } }
A container for a value and a unit to better describe the value.
Definition: Quantity.hpp:37
Unit unit
The units describing the value.
Definition: Quantity.hpp:45
Indicates that two different Unit objects were used in an operation that requires identical Unit obje...
Definition: Unit.hpp:51
Quantity()=default
The default constructor; expect value and unit to be uninitialized.
bool operator<(const Quantity &q) const
Compares two quantities of the same units.
Definition: Quantity.cpp:56
Quantity & operator-=(const Quantity &q)
Subtracts a quantity from another; they must use the same units.
Definition: Quantity.cpp:36
Quantity & operator+=(const Quantity &q)
Adds two quantities; they must use the same units.
Definition: Quantity.cpp:28
Quantity operator+(const Quantity &q) const
Adds two quantities; they must use the same units.
Definition: Quantity.cpp:14
bool operator>(const Quantity &q) const
Compares two quantities of the same units.
Definition: Quantity.cpp:63
Quantity operator-(const Quantity &q) const
Subtracts a quantity from another; they must use the same units.
Definition: Quantity.cpp:21
bool operator<=(const Quantity &q) const
Compares two quantities of the same units.
Definition: Quantity.cpp:70
Quantity & operator/=(const Quantity &q)
Divides a quantity by another; the units are also divided.
Definition: Quantity.cpp:50
double value
Some value; probably something measured.
Definition: Quantity.hpp:41
Quantity & operator*=(const Quantity &q)
Multiplies two quantities; the units are also multiplied.
Definition: Quantity.cpp:44
#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
bool operator>=(const Quantity &q) const
Compares two quantities of the same units.
Definition: Quantity.cpp:77