A value measured in the specified unit. More...
#include <quantities.h>
Public Types | |
using | value_t = T |
Type of the stored value. More... | |
using | unit_t = Unit |
The unit and scale of this quantity. More... | |
using | quantity_t = Quantity< Unit, T > |
The type of this class. More... | |
using | baseunit_t = typename unit_t::baseunit_t |
Description of the unscaled unit. More... | |
Public Member Functions | |
Quantity ()=default | |
Constructor: value is left uninitialized. More... | |
constexpr | Quantity (value_t v) |
Constructor: takes a value in the intended representation. More... | |
template<typename Q , typename std::enable_if_t< details::is_quantity_v< Q >> * = nullptr> | |
constexpr | Quantity (Q q) |
Constructor: converts from another quantity. More... | |
constexpr value_t | value () const |
Returns the value of the quantity. More... | |
constexpr | operator value_t () const |
Explicit conversion to the base quantity. More... | |
template<typename OQ > | |
constexpr OQ | convertInto () const |
Convert this quantity into the specified one. More... | |
template<typename OU , typename OT > | |
constexpr auto | operator+ (Quantity< OU, OT > const other) const -> quantity_t |
template<typename OU , typename OT > | |
constexpr auto | operator- (Quantity< OU, OT > const other) const -> quantity_t |
template<typename OU , typename OT > | |
constexpr auto | plus (Quantity< OU, OT > const other) const -> quantity_t |
template<typename OU , typename OT > | |
constexpr auto | minus (Quantity< OU, OT > const other) const -> quantity_t |
template<typename OU , typename OT > | |
constexpr auto | operator/ (Quantity< OU, OT > const q) const -> value_t |
template<typename OU , typename OT > | |
auto | operator+= (Quantity< OU, OT > const other) -> quantity_t & |
template<typename OU , typename OT > | |
auto | operator-= (Quantity< OU, OT > const other) -> quantity_t & |
Asymmetric operand arithmetic operations | |
These arithmetic operations take care of preserving the quantity through them. Not all possible (or reasonable) operations are supported yet. Some operations that may be symmetric (like multiplication by a scalar) are implemented as free functions rather than methods.
| |
template<typename OU , typename OT > | |
constexpr quantity_t | operator+ (Quantity< OU, OT > const other) const |
Returns a quantity sum of this and other (must have same unit). More... | |
template<typename OU , typename OT > | |
constexpr quantity_t | operator- (Quantity< OU, OT > const other) const |
template<typename OU , typename OT > | |
constexpr quantity_t | plus (Quantity< OU, OT > const other) const |
Returns a quantity sum of this and other . More... | |
template<typename OU , typename OT > | |
constexpr quantity_t | minus (Quantity< OU, OT > const other) const |
Returns a quantity difference of this and other . More... | |
template<typename OU , typename OT > | |
constexpr value_t | operator/ (Quantity< OU, OT > q) const |
Division by a quantity, returns a pure number. More... | |
template<typename OU , typename OT > | |
quantity_t & | operator+= (Quantity< OU, OT > const other) |
Add the other quantity (possibly concerted) to this one. More... | |
template<typename OU , typename OT > | |
quantity_t & | operator-= (Quantity< OU, OT > const other) |
Subtract the other quantity (possibly concerted) to this one. More... | |
template<typename OT > | |
std::enable_if_t< std::is_arithmetic_v< OT >, quantity_t & > | operator*= (OT factor) |
Scale this quantity by a factor. More... | |
template<typename OT > | |
std::enable_if_t< std::is_arithmetic_v< OT >, quantity_t & > | operator/= (OT quot) |
Scale the quantity dividing it by a quotient. More... | |
constexpr quantity_t | operator+ () const |
Returns a quantity with same value. More... | |
constexpr quantity_t | operator- () const |
Returns a quantity with same value but the sign flipped. More... | |
constexpr quantity_t | abs () const |
Returns a quantity with the absolute value of this one. More... | |
Comparisons. | |
Comparisons with plain numbers are managed by implicit conversion. More care is needed for quantities. Comparisons between two quantity instances
Value storage types are compared according to C++ rules. | |
template<typename OU , typename OT > | |
constexpr bool | operator== (Quantity< OU, OT > const other) const |
template<typename OU , typename OT > | |
constexpr bool | operator!= (Quantity< OU, OT > const other) const |
template<typename OU , typename OT > | |
constexpr bool | operator>= (Quantity< OU, OT > const other) const |
template<typename OU , typename OT > | |
constexpr bool | operator> (Quantity< OU, OT > const other) const |
template<typename OU , typename OT > | |
constexpr bool | operator<= (Quantity< OU, OT > const other) const |
template<typename OU , typename OT > | |
constexpr bool | operator< (Quantity< OU, OT > const other) const |
Static Public Member Functions | |
template<typename U > | |
static constexpr quantity_t | castFrom (U value) |
Returns a new quantity initialized with the specified value. More... | |
Private Attributes | |
value_t | fValue {} |
Stored value. More... | |
Access to the scaled unit. | |
template<typename U > | |
static constexpr bool | is_compatible_value_v = details::is_value_compatible_with_v<U, value_t> |
Whether U is a value type compatible with value_t . More... | |
template<typename U > | |
static constexpr bool | has_compatible_value_v = details::has_value_compatible_with_v<U, value_t> |
Whether U has (or is) a value type compatible with value_t . More... | |
static constexpr unit_t | unit () |
Returns an object with as type the scaled unit (unit_t ). More... | |
static constexpr baseunit_t | baseUnit () |
Returns an object with as type the base unit (baseunit_t ). More... | |
static auto | unitName () |
Returns the full name of the unit, in a string-like object. More... | |
static auto | unitSymbol () |
Returns the symbol of the unit, in a string-like object. More... | |
template<typename OU > | |
static constexpr bool | sameBaseUnitAs () |
Returns whether this quantity has the same base unit as OU . More... | |
template<typename OU > | |
static constexpr bool | sameUnitAs () |
Returns whether this quantity has same unit and scale as OU . More... | |
template<typename U > | |
static constexpr bool | isCompatibleValue () |
Returns whether U is a value type compatible with value_t . More... | |
template<typename U > | |
static constexpr bool | hasCompatibleValue () |
A value measured in the specified unit.
Unit | the scaled unit type representing the unit of this quantity |
T | type of base value |
A Quantity
instance is a glorified number of type T
(or, value_t
). The Quantity
class adds to it the explicit documentation of the unit the value is measured in, plus some attempt to preserve that information:
Quantity
type will carry the information of its unit with the typequantities must be assigned other quantities: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp} using util::quantities::milliampere;
milliampere i; i = 2.5; // ERROR! what is 2.5? i = milliampere(2.5);
milliampere i2 { 2.5 }; // SPECIAL, allowed only in construction i2 = i1; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
can be converted, implicitly or explicitly, to its plain value: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp} using util::quantities::milliampere;
milliampere i { 6.0 }; double v = i; // implicit conversion v = double(i); // explicit conversion v = i.value(); // even more explicit conversion ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
weakly attempts to preserve the unit information ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp} using namespace util::quantities; using namespace util::quantities::electronics_literals;
milliampere mi { 4.0 }; microampere ui { 500.0 };
mi += ui; // 4.5 mA mi *= ui; // ERROR! what does this even mean?? mi += 5.0; // ERROR! mi += milliampere(3.0); // 7.5 mA mi += 2.0_ma; // 9.5 mA mi + ui; // ERROR! (arbitrary whether to represent in mA or uA) mi + 5.0; // ERROR! (as above) mi / 5.0; // milliampere{1.9} mi - 5_mA; // milliampere{4.5} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(milliampere
and microampere
are hypotetical instantiations of Quantity
template class from util::quantities
, and they also have a literal conversion to correctly interpret things like 2.0_ma
or 7_A
)
Quantity
objects by value; it is assumed that the type T
is simple enough that copying it is faster than passing around its reference, and that any overhead is optimized out by the compiler Definition at line 566 of file quantities.h.
using util::quantities::concepts::Quantity< Unit, T >::baseunit_t = typename unit_t::baseunit_t |
Description of the unscaled unit.
Definition at line 575 of file quantities.h.
using util::quantities::concepts::Quantity< Unit, T >::quantity_t = Quantity<Unit, T> |
The type of this class.
Definition at line 572 of file quantities.h.
using util::quantities::concepts::Quantity< Unit, T >::unit_t = Unit |
The unit and scale of this quantity.
Definition at line 571 of file quantities.h.
using util::quantities::concepts::Quantity< Unit, T >::value_t = T |
Type of the stored value.
Definition at line 570 of file quantities.h.
|
explicitdefault |
Constructor: value is left uninitialized.
|
inlineexplicit |
Constructor: takes a value in the intended representation.
Definition at line 584 of file quantities.h.
|
inline |
Constructor: converts from another quantity.
Q | type of the other quantity |
q | quantity to be converted from |
Quantities are required to be in the same unit (unit scale may differ). The value in q
is converted from its native scale into the one of this quantity.
Definition at line 599 of file quantities.h.
|
inline |
Returns a quantity with the absolute value of this one.
Definition at line 693 of file quantities.h.
|
inlinestatic |
Returns an object with as type the base unit (baseunit_t
).
Definition at line 748 of file quantities.h.
|
inlinestatic |
Returns a new quantity initialized with the specified value.
U | type to initialize the quantity with |
value | the value to initialize the quantity with |
Quantity
object initialized with value
The value
is cast into value_t
via static_cast()
.
Example: be Tick
a quantity based on an integral value, like util::quantities::tick
, and detClocks
an instance of detinfo::DetectorClocks
:
triggerTick
will be of type Tick
and will denote the number of the tick (0-based) within which detClocks.TriggerTime()
fell.
Definition at line 825 of file quantities.h.
|
inline |
Convert this quantity into the specified one.
Definition at line 801 of file quantities.h.
|
inlinestatic |
Returns whether U
has (or is) a value type compatible with value_t
.
Definition at line 792 of file quantities.h.
|
inlinestatic |
Returns whether U
is a value type compatible with value_t
.
Definition at line 786 of file quantities.h.
constexpr quantity_t util::quantities::concepts::Quantity< Unit, T >::minus | ( | Quantity< OU, OT > const | other | ) | const |
Returns a quantity difference of this and other
.
constexpr auto util::quantities::concepts::Quantity< Unit, T >::minus | ( | Quantity< OU, OT > const | other | ) | const -> quantity_t |
Definition at line 1340 of file quantities.h.
|
inlineexplicit |
Explicit conversion to the base quantity.
Definition at line 612 of file quantities.h.
constexpr bool util::quantities::concepts::Quantity< U, T >::operator!= | ( | Quantity< OU, OT > const | other | ) | const |
Definition at line 1448 of file quantities.h.
|
inline |
Scale this quantity by a factor.
Definition at line 679 of file quantities.h.
constexpr quantity_t util::quantities::concepts::Quantity< Unit, T >::operator+ | ( | Quantity< OU, OT > const | other | ) | const |
Returns a quantity sum of this and other
(must have same unit).
|
inline |
Returns a quantity with same value.
Definition at line 687 of file quantities.h.
constexpr auto util::quantities::concepts::Quantity< Unit, T >::operator+ | ( | Quantity< OU, OT > const | other | ) | const -> quantity_t |
Definition at line 1290 of file quantities.h.
quantity_t& util::quantities::concepts::Quantity< Unit, T >::operator+= | ( | Quantity< OU, OT > const | other | ) |
Add the other
quantity (possibly concerted) to this one.
auto util::quantities::concepts::Quantity< Unit, T >::operator+= | ( | Quantity< OU, OT > const | other | ) | -> quantity_t& |
Definition at line 1383 of file quantities.h.
constexpr quantity_t util::quantities::concepts::Quantity< Unit, T >::operator- | ( | Quantity< OU, OT > const | other | ) | const |
Returns a quantity difference of this and other
(must have same unit).
|
inline |
Returns a quantity with same value but the sign flipped.
Definition at line 690 of file quantities.h.
constexpr auto util::quantities::concepts::Quantity< Unit, T >::operator- | ( | Quantity< OU, OT > const | other | ) | const -> quantity_t |
Definition at line 1304 of file quantities.h.
quantity_t& util::quantities::concepts::Quantity< Unit, T >::operator-= | ( | Quantity< OU, OT > const | other | ) |
Subtract the other
quantity (possibly concerted) to this one.
auto util::quantities::concepts::Quantity< Unit, T >::operator-= | ( | Quantity< OU, OT > const | other | ) | -> quantity_t& |
Definition at line 1405 of file quantities.h.
constexpr value_t util::quantities::concepts::Quantity< Unit, T >::operator/ | ( | Quantity< OU, OT > | q | ) | const |
Division by a quantity, returns a pure number.
constexpr auto util::quantities::concepts::Quantity< Unit, T >::operator/ | ( | Quantity< OU, OT > const | q | ) | const -> value_t |
Definition at line 1362 of file quantities.h.
|
inline |
Scale the quantity dividing it by a quotient.
Definition at line 684 of file quantities.h.
constexpr bool util::quantities::concepts::Quantity< U, T >::operator< | ( | Quantity< OU, OT > const | other | ) | const |
Definition at line 1508 of file quantities.h.
constexpr bool util::quantities::concepts::Quantity< U, T >::operator<= | ( | Quantity< OU, OT > const | other | ) | const |
Definition at line 1468 of file quantities.h.
constexpr bool util::quantities::concepts::Quantity< U, T >::operator== | ( | Quantity< OU, OT > const | other | ) | const |
Definition at line 1428 of file quantities.h.
constexpr bool util::quantities::concepts::Quantity< U, T >::operator> | ( | Quantity< OU, OT > const | other | ) | const |
Definition at line 1528 of file quantities.h.
constexpr bool util::quantities::concepts::Quantity< U, T >::operator>= | ( | Quantity< OU, OT > const | other | ) | const |
Definition at line 1488 of file quantities.h.
constexpr quantity_t util::quantities::concepts::Quantity< Unit, T >::plus | ( | Quantity< OU, OT > const | other | ) | const |
Returns a quantity sum of this and other
.
constexpr auto util::quantities::concepts::Quantity< Unit, T >::plus | ( | Quantity< OU, OT > const | other | ) | const -> quantity_t |
Definition at line 1318 of file quantities.h.
|
inlinestatic |
Returns whether this quantity has the same base unit as OU
.
OU | any type with baseunit_t type (including ScaledUnit , Quantity , Interval ...) |
Definition at line 762 of file quantities.h.
|
inlinestatic |
Returns whether this quantity has same unit and scale as OU
.
OU | any type with unit_t type (including ScaledUnit , Quantity , Interval ...) |
Definition at line 771 of file quantities.h.
|
inlinestatic |
Returns an object with as type the scaled unit (unit_t
).
Definition at line 745 of file quantities.h.
|
inlinestatic |
Returns the full name of the unit, in a string-like object.
Definition at line 751 of file quantities.h.
|
inlinestatic |
Returns the symbol of the unit, in a string-like object.
Definition at line 754 of file quantities.h.
|
inline |
Returns the value of the quantity.
Definition at line 609 of file quantities.h.
|
private |
Stored value.
Definition at line 830 of file quantities.h.
|
static |
Whether U
has (or is) a value type compatible with value_t
.
Definition at line 782 of file quantities.h.
|
static |
Whether U
is a value type compatible with value_t
.
Definition at line 777 of file quantities.h.