LArSoft
v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
|
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 |
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 554 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 562 of file quantities.h.
using util::quantities::concepts::Quantity< Unit, T >::quantity_t = Quantity<Unit, T> |
The type of this class.
Definition at line 559 of file quantities.h.
using util::quantities::concepts::Quantity< Unit, T >::unit_t = Unit |
The unit and scale of this quantity.
Definition at line 558 of file quantities.h.
using util::quantities::concepts::Quantity< Unit, T >::value_t = T |
Type of the stored value.
Definition at line 557 of file quantities.h.
|
explicitdefault |
Constructor: value is left uninitialized.
|
inlineexplicit |
Constructor: takes a value in the intended representation.
Definition at line 570 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 582 of file quantities.h.
|
inline |
Returns a quantity with the absolute value of this one.
Definition at line 679 of file quantities.h.
References util::abs(), util::quantities::concepts::operator!=(), util::quantities::concepts::operator==(), util::quantities::concepts::operator>(), util::quantities::concepts::operator>=(), fhicl::other, and value.
|
inlinestatic |
Returns an object with as type the base unit (baseunit_t
).
Definition at line 731 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 816 of file quantities.h.
References value.
Referenced by util::quantities::electronics_literals::operator""_tick(), and util::quantities::electronics_literals::operator""_tickd().
|
inline |
Convert this quantity into the specified one.
Definition at line 790 of file quantities.h.
|
inlinestatic |
Returns whether U
has (or is) a value type compatible with value_t
.
Definition at line 780 of file quantities.h.
|
inlinestatic |
Returns whether U
is a value type compatible with value_t
.
Definition at line 772 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 1286 of file quantities.h.
References fhicl::other.
|
inlineexplicit |
Explicit conversion to the base quantity.
Definition at line 593 of file quantities.h.
References util::quantities::concepts::operator+(), lar::dump::operator+=(), util::quantities::concepts::operator-(), util::quantities::concepts::operator/(), fhicl::other, and value.
constexpr bool util::quantities::concepts::Quantity< U, T >::operator!= | ( | Quantity< OU, OT > const | other | ) | const |
Definition at line 1375 of file quantities.h.
References value, and util::quantities::concepts::Quantity< Unit, T >::value().
|
inline |
Scale this quantity by a factor.
Definition at line 658 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 673 of file quantities.h.
References value.
constexpr auto util::quantities::concepts::Quantity< Unit, T >::operator+ | ( | Quantity< OU, OT > const | other | ) | const -> quantity_t |
Definition at line 1245 of file quantities.h.
References fhicl::other, and value.
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 1321 of file quantities.h.
References fhicl::other.
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 676 of file quantities.h.
References value.
constexpr auto util::quantities::concepts::Quantity< Unit, T >::operator- | ( | Quantity< OU, OT > const | other | ) | const -> quantity_t |
Definition at line 1256 of file quantities.h.
References fhicl::other, and value.
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 1340 of file quantities.h.
References fhicl::other.
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 1305 of file quantities.h.
References value.
|
inline |
Scale the quantity dividing it by a quotient.
Definition at line 666 of file quantities.h.
constexpr bool util::quantities::concepts::Quantity< U, T >::operator< | ( | Quantity< OU, OT > const | other | ) | const |
Definition at line 1423 of file quantities.h.
References value, and util::quantities::concepts::Quantity< Unit, T >::value().
constexpr bool util::quantities::concepts::Quantity< U, T >::operator<= | ( | Quantity< OU, OT > const | other | ) | const |
Definition at line 1391 of file quantities.h.
References value, and util::quantities::concepts::Quantity< Unit, T >::value().
constexpr bool util::quantities::concepts::Quantity< U, T >::operator== | ( | Quantity< OU, OT > const | other | ) | const |
Definition at line 1359 of file quantities.h.
References value, and util::quantities::concepts::Quantity< Unit, T >::value().
constexpr bool util::quantities::concepts::Quantity< U, T >::operator> | ( | Quantity< OU, OT > const | other | ) | const |
Definition at line 1439 of file quantities.h.
References value, and util::quantities::concepts::Quantity< Unit, T >::value().
constexpr bool util::quantities::concepts::Quantity< U, T >::operator>= | ( | Quantity< OU, OT > const | other | ) | const |
Definition at line 1407 of file quantities.h.
References value, and util::quantities::concepts::Quantity< Unit, T >::value().
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 1267 of file quantities.h.
References fhicl::other.
|
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 745 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 756 of file quantities.h.
|
inlinestatic |
Returns an object with as type the scaled unit (unit_t
).
Definition at line 728 of file quantities.h.
Referenced by util::quantities::concepts::operator<<(), and util::quantities::concepts::to_string().
|
inlinestatic |
Returns the full name of the unit, in a string-like object.
Definition at line 734 of file quantities.h.
|
inlinestatic |
Returns the symbol of the unit, in a string-like object.
Definition at line 737 of file quantities.h.
|
inline |
Returns the value of the quantity.
Definition at line 590 of file quantities.h.
Referenced by detinfo::DetectorClocksWithUnits::G4ToElecTime(), util::quantities::concepts::Quantity< Unit, T >::operator!=(), util::quantities::concepts::operator*(), util::quantities::concepts::operator/(), util::quantities::concepts::Quantity< Unit, T >::operator<(), util::quantities::concepts::operator<<(), util::quantities::concepts::Quantity< Unit, T >::operator<=(), util::quantities::concepts::Quantity< Unit, T >::operator==(), util::quantities::concepts::Quantity< Unit, T >::operator>(), util::quantities::concepts::Quantity< Unit, T >::operator>=(), util::quantities::concepts::to_string(), and detinfo::DetectorClocksWithUnits::TPCTick2TDC().
|
private |
Stored value.
Definition at line 822 of file quantities.h.
|
static |
Whether U
has (or is) a value type compatible with value_t
.
Definition at line 767 of file quantities.h.
|
static |
Whether U
is a value type compatible with value_t
.
Definition at line 763 of file quantities.h.