DUDS
Distributed Update of Data from Something
duds::data::Quantity Struct Reference

A container for a value and a unit to better describe the value. More...

#include <Quantity.hpp>

Collaboration diagram for duds::data::Quantity:

Public Member Functions

 Quantity ()=default
 The default constructor; expect value and unit to be uninitialized. More...
 
 Quantity (const Quantity &)=default
 A default copy constrcutor. More...
 
constexpr Quantity (const double &v, const Unit &u)
 Constructs a new Quantity with the given values. More...
 
template<class Duration >
 Quantity (const Duration &d)
 Constructs a new Quantity holding the number of seconds stored in the given duration. More...
 
bool operator!= (const Quantity &q) const
 Compares two quantities for inequality. More...
 
Quantity operator* (const Quantity &q) const
 Multiplies two quantities; the units are also multiplied. More...
 
Quantity operator* (double s) const
 Multiplies a Quantity by a scalar. More...
 
Quantityoperator*= (const Quantity &q)
 Multiplies two quantities; the units are also multiplied. More...
 
Quantityoperator*= (double s)
 Multiplies a Quantity by a scalar. More...
 
Quantity operator+ (const Quantity &q) const
 Adds two quantities; they must use the same units. More...
 
Quantityoperator+= (const Quantity &q)
 Adds two quantities; they must use the same units. More...
 
Quantity operator- (const Quantity &q) const
 Subtracts a quantity from another; they must use the same units. More...
 
Quantityoperator-= (const Quantity &q)
 Subtracts a quantity from another; they must use the same units. More...
 
Quantity operator/ (const Quantity &q) const
 Divides a quantity by another; the units are also divided. More...
 
Quantity operator/ (double s) const
 Divides a Quantity by a scalar. More...
 
Quantityoperator/= (const Quantity &q)
 Divides a quantity by another; the units are also divided. More...
 
Quantityoperator/= (double s)
 Divides a Quantity by a scalar. More...
 
bool operator< (const Quantity &q) const
 Compares two quantities of the same units. More...
 
bool operator<= (const Quantity &q) const
 Compares two quantities of the same units. More...
 
bool operator== (const Quantity &q) const
 Compares two quantities for equality. More...
 
bool operator> (const Quantity &q) const
 Compares two quantities of the same units. More...
 
bool operator>= (const Quantity &q) const
 Compares two quantities of the same units. More...
 
template<class Duration >
void toDuration (Duration &d) const
 Sets a duration to the seconds stored in this quantity. More...
 
template<class Duration >
Duration toDuration () const
 Returns a duration with the seconds stored in this quantity. More...
 

Public Attributes

Unit unit
 The units describing the value. More...
 
double value
 Some value; probably something measured. More...
 

Private Member Functions

template<class A >
void serialize (A &a, const unsigned int)
 

Friends

class boost::serialization::access
 

Detailed Description

A container for a value and a unit to better describe the value.

This struct has a full set of arithmetic operators with Quantity objects as both operands. These operators enforce proper use of units by throwing exceptions when the operation is invalid. For multiplication and division, the new unit is also computed for the result.

The unit member is a Unit, which is simpiler to work with than an ExtendedUnit. Because of the limitations of Unit, the value member must be floating point. Bewteen this and the decision to use doubles unless integers are a better representation, I decided not to make this class a template and force the use of a double.

Todo:
Consider allowing user-defined literals with common units.
Author
Jeff Jackowski
Examples:
am2320test.cpp, apds9301test.cpp, ina219test.cpp, mcp9808test.cpp, and tsl2591test.cpp.

Definition at line 37 of file Quantity.hpp.

Constructor & Destructor Documentation

◆ Quantity() [1/4]

duds::data::Quantity::Quantity ( )
default

The default constructor; expect value and unit to be uninitialized.

Referenced by operator*(), operator+(), operator-(), operator/(), and duds::data::operator/().

◆ Quantity() [2/4]

duds::data::Quantity::Quantity ( const Quantity )
default

A default copy constrcutor.

◆ Quantity() [3/4]

constexpr duds::data::Quantity::Quantity ( const double &  v,
const Unit u 
)
inline

Constructs a new Quantity with the given values.

Parameters
vThe initial value.
uThe initlal unit.

Definition at line 59 of file Quantity.hpp.

◆ Quantity() [4/4]

template<class Duration >
duds::data::Quantity::Quantity ( const Duration &  d)
inline

Constructs a new Quantity holding the number of seconds stored in the given duration.

Template Parameters
DurationA time duration class related to std::chrono::duration.
Parameters
dThe duration.

Definition at line 67 of file Quantity.hpp.

Member Function Documentation

◆ operator!=()

bool duds::data::Quantity::operator!= ( const Quantity q) const
inline

Compares two quantities for inequality.

Definition at line 171 of file Quantity.hpp.

◆ operator*() [1/2]

Quantity duds::data::Quantity::operator* ( const Quantity q) const
inline

Multiplies two quantities; the units are also multiplied.

Exceptions
BadUnitExponentThe resulting units cannot be represented.

Definition at line 86 of file Quantity.hpp.

◆ operator*() [2/2]

Quantity duds::data::Quantity::operator* ( double  s) const
inline

Multiplies a Quantity by a scalar.

Definition at line 92 of file Quantity.hpp.

◆ operator*=() [1/2]

Quantity & duds::data::Quantity::operator*= ( const Quantity q)

Multiplies two quantities; the units are also multiplied.

Exceptions
BadUnitExponentThe resulting units cannot be represented.

Definition at line 44 of file Quantity.cpp.

Referenced by operator/().

◆ operator*=() [2/2]

Quantity& duds::data::Quantity::operator*= ( double  s)
inline

Multiplies a Quantity by a scalar.

Definition at line 126 of file Quantity.hpp.

◆ operator+()

Quantity duds::data::Quantity::operator+ ( const Quantity q) const

Adds two quantities; they must use the same units.

Exceptions
UnitMismatchThe quantities are for different units.

Definition at line 14 of file Quantity.cpp.

Referenced by Quantity().

◆ operator+=()

Quantity & duds::data::Quantity::operator+= ( const Quantity q)

Adds two quantities; they must use the same units.

Exceptions
UnitMismatchThe quantities are for different units.

Definition at line 28 of file Quantity.cpp.

Referenced by operator/().

◆ operator-()

Quantity duds::data::Quantity::operator- ( const Quantity q) const

Subtracts a quantity from another; they must use the same units.

Exceptions
UnitMismatchThe quantities are for different units.

Definition at line 21 of file Quantity.cpp.

Referenced by Quantity().

◆ operator-=()

Quantity & duds::data::Quantity::operator-= ( const Quantity q)

Subtracts a quantity from another; they must use the same units.

Exceptions
UnitMismatchThe quantities are for different units.

Definition at line 36 of file Quantity.cpp.

Referenced by operator/().

◆ operator/() [1/2]

Quantity duds::data::Quantity::operator/ ( const Quantity q) const
inline

Divides a quantity by another; the units are also divided.

Exceptions
BadUnitExponentThe resulting units cannot be represented.

Definition at line 99 of file Quantity.hpp.

◆ operator/() [2/2]

Quantity duds::data::Quantity::operator/ ( double  s) const
inline

Divides a Quantity by a scalar.

Definition at line 105 of file Quantity.hpp.

◆ operator/=() [1/2]

Quantity & duds::data::Quantity::operator/= ( const Quantity q)

Divides a quantity by another; the units are also divided.

Exceptions
BadUnitExponentThe resulting units cannot be represented.

Definition at line 50 of file Quantity.cpp.

Referenced by operator*=().

◆ operator/=() [2/2]

Quantity& duds::data::Quantity::operator/= ( double  s)
inline

Divides a Quantity by a scalar.

Definition at line 138 of file Quantity.hpp.

◆ operator<()

bool duds::data::Quantity::operator< ( const Quantity q) const

Compares two quantities of the same units.

Exceptions
UnitMismatchThe quantities are for different units.

Definition at line 56 of file Quantity.cpp.

Referenced by operator/=().

◆ operator<=()

bool duds::data::Quantity::operator<= ( const Quantity q) const

Compares two quantities of the same units.

Exceptions
UnitMismatchThe quantities are for different units.

Definition at line 70 of file Quantity.cpp.

Referenced by operator/=().

◆ operator==()

bool duds::data::Quantity::operator== ( const Quantity q) const
inline

Compares two quantities for equality.

Definition at line 165 of file Quantity.hpp.

◆ operator>()

bool duds::data::Quantity::operator> ( const Quantity q) const

Compares two quantities of the same units.

Exceptions
UnitMismatchThe quantities are for different units.

Definition at line 63 of file Quantity.cpp.

Referenced by operator/=().

◆ operator>=()

bool duds::data::Quantity::operator>= ( const Quantity q) const

Compares two quantities of the same units.

Exceptions
UnitMismatchThe quantities are for different units.

Definition at line 77 of file Quantity.cpp.

Referenced by operator/=().

◆ serialize()

template<class A >
void duds::data::Quantity::serialize ( A &  a,
const unsigned  int 
)
inlineprivate

Definition at line 216 of file Quantity.hpp.

◆ toDuration() [1/2]

template<class Duration >
void duds::data::Quantity::toDuration ( Duration &  d) const
inline

Sets a duration to the seconds stored in this quantity.

Precondition
The units of this quantity are seconds.
Template Parameters
DurationThe duration type. It must be usable in the same manner as std::chrono::duration.
Parameters
dThe duration object to set. The value will be converted to the units used by the duration type.
Exceptions
UnitBadConversionThe quantity value is not in seconds.

Definition at line 184 of file Quantity.hpp.

Referenced by duds::data::GenericValueDurationVisitor< IST >::operator()().

◆ toDuration() [2/2]

template<class Duration >
Duration duds::data::Quantity::toDuration ( ) const
inline

Returns a duration with the seconds stored in this quantity.

Precondition
The units of this quantity are seconds.
Template Parameters
DurationThe duration type. It must be usable in the same manner as std::chrono::duration.
Returns
The duration object. The value will be converted to the units used by the duration type.
Exceptions
UnitBadConversionThe quantity value is not in seconds.

Definition at line 203 of file Quantity.hpp.

Friends And Related Function Documentation

◆ boost::serialization::access

friend class boost::serialization::access
friend

Definition at line 214 of file Quantity.hpp.

Member Data Documentation

◆ unit

◆ value


The documentation for this struct was generated from the following files: