DUDS
Distributed Update of Data from Something
Quantity.hpp
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 #ifndef QUANTITY_HPP
11 #define QUANTITY_HPP
12 
13 #include <duds/data/Units.hpp>
14 #include <duds/general/Errors.hpp>
16 
17 namespace duds { namespace data {
18 
37 struct Quantity {
41  double value;
49  Quantity() = default;
53  Quantity(const Quantity &) = default;
59  constexpr Quantity(const double &v, const Unit &u) : value(v), unit(u) { }
66  template <class Duration>
67  Quantity(const Duration &d) : unit(duds::data::units::Second) {
68  std::chrono::duration<double> sec =
69  std::chrono::duration_cast<std::chrono::seconds>(d);
70  value = sec.count();
71  }
76  Quantity operator + (const Quantity &q) const;
81  Quantity operator - (const Quantity &q) const;
86  Quantity operator * (const Quantity &q) const {
87  return Quantity(value * q.value, unit * q.unit);
88  }
92  Quantity operator * (double s) const {
93  return Quantity(value * s, unit);
94  }
99  Quantity operator / (const Quantity &q) const {
100  return Quantity(value / q.value, unit / q.unit);
101  }
105  Quantity operator / (double s) const {
106  return Quantity(value / s, unit);
107  }
112  Quantity &operator += (const Quantity &q);
117  Quantity &operator -= (const Quantity &q);
122  Quantity &operator *= (const Quantity &q);
126  Quantity &operator *= (double s) {
127  value *= s;
128  return *this;
129  }
134  Quantity &operator /= (const Quantity &q);
138  Quantity &operator /= (double s) {
139  value /= s;
140  return *this;
141  }
146  bool operator < (const Quantity &q) const;
151  bool operator > (const Quantity &q) const;
156  bool operator <= (const Quantity &q) const;
161  bool operator >= (const Quantity &q) const;
165  bool operator == (const Quantity &q) const {
166  return (unit == q.unit) && (value == q.value);
167  }
171  bool operator != (const Quantity &q) const {
172  return (unit != q.unit) || (value != q.value);
173  }
183  template <class Duration>
184  void toDuration(Duration &d) const {
185  if (unit != duds::data::units::Second) {
187  }
188  d = Duration(typename Duration::rep(value *
189  (double)(Duration::period::num) /
190  (double)(Duration::period::den)
191  ) );
192  }
202  template <class Duration>
203  Duration toDuration() const {
204  if (unit != duds::data::units::Second) {
206  }
207  return Duration(typename Duration::rep(value *
208  (double)(Duration::period::num) /
209  (double)(Duration::period::den)
210  ) );
211  }
212 private:
213  // serialization support
215  template <class A>
216  void serialize(A &a, const unsigned int) {
217  a & BOOST_SERIALIZATION_NVP(value);
218  a & BOOST_SERIALIZATION_NVP(unit);
219  }
220 };
221 
225 inline Quantity operator * (double s, const Quantity &q) {
226  return q * s;
227 }
228 
232 inline Quantity operator / (double s, const Quantity &q) {
233  return Quantity(s / q.value, q.unit);
234 }
235 
236 /*
237  * Assigns a duration in femtoseconds.
238  * @param q The quantity to convert; must be in seconds.
239  * @throw UnitBadConversion The quantity is not in seconds.
240  * /
241 ::duds::time::interstellar::Femtoseconds &operator = (
242  ::duds::time::interstellar::Femtoseconds &fs, const Quantity &q
243 );
244 
245 / **
246  * Assigns a duration in nanoseconds.
247  * @param q The quantity to convert; must be in seconds.
248  * @throw UnitBadConversion The quantity is not in seconds.
249  * /
250 ::duds::time::interstellar::Nanoseconds &operator = (
251  ::duds::time::interstellar::Nanoseconds &ns, const Quantity &q
252 );
253 */
254 } }
255 
256 #endif // #ifndef QUANTITY_HPP
A container for a value and a unit to better describe the value.
Definition: Quantity.hpp:37
Duration toDuration() const
Returns a duration with the seconds stored in this quantity.
Definition: Quantity.hpp:203
Unit unit
The units describing the value.
Definition: Quantity.hpp:45
Quantity operator*(const Quantity &q) const
Multiplies two quantities; the units are also multiplied.
Definition: Quantity.hpp:86
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
constexpr Quantity(const double &v, const Unit &u)
Constructs a new Quantity with the given values.
Definition: Quantity.hpp:59
Quantity & operator-=(const Quantity &q)
Subtracts a quantity from another; they must use the same units.
Definition: Quantity.cpp:36
bool operator!=(const Quantity &q) const
Compares two quantities for inequality.
Definition: Quantity.hpp:171
Quantity & operator+=(const Quantity &q)
Adds two quantities; they must use the same units.
Definition: Quantity.cpp:28
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
Quantity operator+(const Quantity &q) const
Adds two quantities; they must use the same units.
Definition: Quantity.cpp:14
A conversion between units was attempted that cannot be performed.
Definition: Unit.hpp:61
void toDuration(Duration &d) const
Sets a duration to the seconds stored in this quantity.
Definition: Quantity.hpp:184
bool operator>(const Quantity &q) const
Compares two quantities of the same units.
Definition: Quantity.cpp:63
Quantity(const Duration &d)
Constructs a new Quantity holding the number of seconds stored in the given duration.
Definition: Quantity.hpp:67
Quantity operator-(const Quantity &q) const
Subtracts a quantity from another; they must use the same units.
Definition: Quantity.cpp:21
friend class boost::serialization::access
Definition: Quantity.hpp:214
bool operator<=(const Quantity &q) const
Compares two quantities of the same units.
Definition: Quantity.cpp:70
void serialize(A &a, const unsigned int)
Definition: Quantity.hpp:216
Quantity & operator/=(const Quantity &q)
Divides a quantity by another; the units are also divided.
Definition: Quantity.cpp:50
Defines Unit objects for specific common base and derived SI units.
bool operator==(const Quantity &q) const
Compares two quantities for equality.
Definition: Quantity.hpp:165
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
General errors.
#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
constexpr Unit Second(DUDS_UNIT_VALUE(0, 0, 0, 0, 0, 0, 1, 0, 0))
Quantity operator/(const Quantity &q) const
Divides a quantity by another; the units are also divided.
Definition: Quantity.hpp:99
bool operator>=(const Quantity &q) const
Compares two quantities of the same units.
Definition: Quantity.cpp:77