quanta 0.1.0
Type-safe units for temperature, frequency, electrical and spatial quantities, modeled after std::chrono
Loading...
Searching...
No Matches
quanta::electro::quantity< Unit, Rep, Precision > Class Template Reference

A quantity of an electrical unit with a representation and precision. More...

#include <quanta/electro.hpp>

Public Types

using unit = Unit
 The unit tag type.
 
using rep = Rep
 The representation type.
 
using precision = typename Precision::type
 The precision as a std::ratio.
 

Public Member Functions

constexpr quantity ()=default
 Constructs a zero quantity.
 
 quantity (const quantity &)=default
 
template<typename Rep2 >
requires std::convertible_to<const Rep2&, rep> && (treat_as_inexact_v<rep> || !treat_as_inexact_v<Rep2>)
constexpr quantity (const Rep2 &r)
 Constructs from a tick count.
 
template<typename Rep2 , typename Precision2 >
requires std::convertible_to<const Rep2&, rep> && detail::_lossless_conversion<Rep2, Precision2, rep, precision> && detail::_quantity_castable<quantity<Unit, Rep2, Precision2>, quantity>
constexpr quantity (const quantity< Unit, Rep2, Precision2 > &q)
 Constructs from another quantity of the same unit with different representation or precision.
 
template<typename Rep2 , typename Precision2 >
requires (!detail::_lossless_conversion<Rep2, Precision2, rep, precision> || !std::convertible_to<const Rep2&, rep>) && detail::_quantity_castable<quantity<Unit, Rep2, Precision2>, quantity>
constexpr quantity (const quantity< Unit, Rep2, Precision2 > &q)
 Explicit constructor for lossy conversions.
 
 ~quantity ()=default
 
quantity & operator= (const quantity &)=default
 
constexpr rep count () const
 Returns the tick count.
 
constexpr quantity< Unit, typename std::common_type< rep >::type, precision > operator+ () const
 
constexpr quantity< Unit, typename std::common_type< rep >::type, precision > operator- () const
 
constexpr quantity & operator++ ()
 
constexpr quantity operator++ (int)
 
constexpr quantity & operator-- ()
 
constexpr quantity operator-- (int)
 
constexpr quantity & operator+= (const quantity &q)
 
constexpr quantity & operator-= (const quantity &q)
 
constexpr quantity & operator*= (const rep &r)
 
constexpr quantity & operator/= (const rep &r)
 
constexpr quantity & operator%= (const rep &r)
 
constexpr quantity & operator%= (const quantity &q)
 

Static Public Member Functions

static constexpr quantity zero () noexcept
 Returns a zero quantity.
 
static constexpr quantity min () noexcept
 Returns the minimum representable quantity.
 
static constexpr quantity max () noexcept
 Returns the maximum representable quantity.
 

Detailed Description

template<typename Unit, typename Rep, typename Precision>
class quanta::electro::quantity< Unit, Rep, Precision >

A quantity of an electrical unit with a representation and precision.

Similar to std::chrono::duration, quantity represents an amount of some electrical unit (volts, amperes, ohms, ...). The precision is expressed as a std::ratio of one base unit.

Examples

// Integer quantities (default)
millivolts vbat{3300};
kiloohms r1{10};
auto usb = 5_V;
// Arithmetic within a unit
auto sum = 1000_mV + 500_mV; // 1500 mV
auto scaled = 100_mA * 3; // 300 mA
auto ratio = 10_kOhm / 5_kOhm; // 2 (scalar)
// Cross-unit arithmetic
auto v = 100_mA * 2_kOhm; // 200 V
auto p = 12_V * 2_A; // 24 W
voltage< int64_t, std::milli > millivolts
Voltage with 1 mV precision.
Definition electro.hpp:921
resistance< int64_t, std::kilo > kiloohms
Resistance with 1 kΩ precision.
Definition electro.hpp:945
constexpr scaled_t scaled
Selects the scaled form in to_string().
Definition core.hpp:161
Template Parameters
UnitThe unit tag type (volt_unit, ampere_unit, ...).
RepArithmetic type for the tick count.
PrecisionA std::ratio representing the precision (default: 1).

Definition at line 245 of file electro.hpp.

Member Typedef Documentation

◆ precision

template<typename Unit , typename Rep , typename Precision >
using quanta::electro::quantity< Unit, Rep, Precision >::precision = typename Precision::type

The precision as a std::ratio.

Definition at line 256 of file electro.hpp.

◆ rep

template<typename Unit , typename Rep , typename Precision >
using quanta::electro::quantity< Unit, Rep, Precision >::rep = Rep

The representation type.

Definition at line 254 of file electro.hpp.

◆ unit

template<typename Unit , typename Rep , typename Precision >
using quanta::electro::quantity< Unit, Rep, Precision >::unit = Unit

The unit tag type.

Definition at line 252 of file electro.hpp.

Constructor & Destructor Documentation

◆ quantity() [1/5]

◆ quantity() [2/5]

template<typename Unit , typename Rep , typename Precision >
quanta::electro::quantity< Unit, Rep, Precision >::quantity ( const quantity< Unit, Rep, Precision > &  )
default

◆ quantity() [3/5]

template<typename Unit , typename Rep , typename Precision >
template<typename Rep2 >
requires std::convertible_to<const Rep2&, rep> && (treat_as_inexact_v<rep> || !treat_as_inexact_v<Rep2>)
constexpr quanta::electro::quantity< Unit, Rep, Precision >::quantity ( const Rep2 &  r)
inlineexplicitconstexpr

Constructs from a tick count.

Implicit for inexact types, explicit otherwise to prevent accidental precision loss.

Template Parameters
Rep2The source representation type.
Parameters
rThe tick count.

Definition at line 273 of file electro.hpp.

◆ quantity() [4/5]

template<typename Unit , typename Rep , typename Precision >
template<typename Rep2 , typename Precision2 >
requires std::convertible_to<const Rep2&, rep> && detail::_lossless_conversion<Rep2, Precision2, rep, precision> && detail::_quantity_castable<quantity<Unit, Rep2, Precision2>, quantity>
constexpr quanta::electro::quantity< Unit, Rep, Precision >::quantity ( const quantity< Unit, Rep2, Precision2 > &  q)
inlineconstexpr

Constructs from another quantity of the same unit with different representation or precision.

Implicit when the conversion is lossless (the target rep is floating-point, or the target precision evenly divides the source precision and the source rep is not floating-point) and the source rep converts implicitly to the target rep. Any other conversion that the cast can perform is explicit (see below).

Template Parameters
Rep2Source representation type.
Precision2Source precision.
Parameters
qThe source quantity.

Definition at line 294 of file electro.hpp.

◆ quantity() [5/5]

template<typename Unit , typename Rep , typename Precision >
template<typename Rep2 , typename Precision2 >
requires (!detail::_lossless_conversion<Rep2, Precision2, rep, precision> || !std::convertible_to<const Rep2&, rep>) && detail::_quantity_castable<quantity<Unit, Rep2, Precision2>, quantity>
constexpr quanta::electro::quantity< Unit, Rep, Precision >::quantity ( const quantity< Unit, Rep2, Precision2 > &  q)
inlineexplicitconstexpr

Explicit constructor for lossy conversions.

Every conversion that the cast can perform and that is not implicit (see above) is explicit: a lossy one (to a coarser precision, or from a floating-point rep to an integral one), or one to a rep that the source rep converts to only explicitly. The value is that of the domain's cast, truncated toward zero. The cast can perform the conversion when the conversion factor between the precisions fits in a std::ratio and the reps are integral or have a common type with intmax_t through which the count converts.

Template Parameters
Rep2Source representation type.
Precision2Source precision.
Parameters
qThe source quantity.

Definition at line 317 of file electro.hpp.

◆ ~quantity()

template<typename Unit , typename Rep , typename Precision >
quanta::electro::quantity< Unit, Rep, Precision >::~quantity ( )
default

Member Function Documentation

◆ count()

◆ max()

template<typename Unit , typename Rep , typename Precision >
static constexpr quantity quanta::electro::quantity< Unit, Rep, Precision >::max ( )
inlinestaticconstexprnoexcept

Returns the maximum representable quantity.

Definition at line 389 of file electro.hpp.

References quanta::electro::quantity< Unit, Rep, Precision >::quantity().

◆ min()

template<typename Unit , typename Rep , typename Precision >
static constexpr quantity quanta::electro::quantity< Unit, Rep, Precision >::min ( )
inlinestaticconstexprnoexcept

Returns the minimum representable quantity.

Definition at line 386 of file electro.hpp.

References quanta::electro::quantity< Unit, Rep, Precision >::quantity().

◆ operator%=() [1/2]

template<typename Unit , typename Rep , typename Precision >
constexpr quantity & quanta::electro::quantity< Unit, Rep, Precision >::operator%= ( const quantity< Unit, Rep, Precision > &  q)
inlineconstexpr

◆ operator%=() [2/2]

template<typename Unit , typename Rep , typename Precision >
constexpr quantity & quanta::electro::quantity< Unit, Rep, Precision >::operator%= ( const rep &  r)
inlineconstexpr

Definition at line 368 of file electro.hpp.

◆ operator*=()

template<typename Unit , typename Rep , typename Precision >
constexpr quantity & quanta::electro::quantity< Unit, Rep, Precision >::operator*= ( const rep &  r)
inlineconstexpr

Definition at line 358 of file electro.hpp.

◆ operator+()

template<typename Unit , typename Rep , typename Precision >
constexpr quantity< Unit, typename std::common_type< rep >::type, precision > quanta::electro::quantity< Unit, Rep, Precision >::operator+ ( ) const
inlineconstexpr

Definition at line 326 of file electro.hpp.

◆ operator++() [1/2]

template<typename Unit , typename Rep , typename Precision >
constexpr quantity & quanta::electro::quantity< Unit, Rep, Precision >::operator++ ( )
inlineconstexpr

Definition at line 334 of file electro.hpp.

◆ operator++() [2/2]

template<typename Unit , typename Rep , typename Precision >
constexpr quantity quanta::electro::quantity< Unit, Rep, Precision >::operator++ ( int  )
inlineconstexpr

◆ operator+=()

template<typename Unit , typename Rep , typename Precision >
constexpr quantity & quanta::electro::quantity< Unit, Rep, Precision >::operator+= ( const quantity< Unit, Rep, Precision > &  q)
inlineconstexpr

◆ operator-()

template<typename Unit , typename Rep , typename Precision >
constexpr quantity< Unit, typename std::common_type< rep >::type, precision > quanta::electro::quantity< Unit, Rep, Precision >::operator- ( ) const
inlineconstexpr

Definition at line 330 of file electro.hpp.

◆ operator--() [1/2]

template<typename Unit , typename Rep , typename Precision >
constexpr quantity & quanta::electro::quantity< Unit, Rep, Precision >::operator-- ( )
inlineconstexpr

Definition at line 341 of file electro.hpp.

◆ operator--() [2/2]

template<typename Unit , typename Rep , typename Precision >
constexpr quantity quanta::electro::quantity< Unit, Rep, Precision >::operator-- ( int  )
inlineconstexpr

◆ operator-=()

template<typename Unit , typename Rep , typename Precision >
constexpr quantity & quanta::electro::quantity< Unit, Rep, Precision >::operator-= ( const quantity< Unit, Rep, Precision > &  q)
inlineconstexpr

◆ operator/=()

template<typename Unit , typename Rep , typename Precision >
constexpr quantity & quanta::electro::quantity< Unit, Rep, Precision >::operator/= ( const rep &  r)
inlineconstexpr

Definition at line 363 of file electro.hpp.

◆ operator=()

template<typename Unit , typename Rep , typename Precision >
quantity & quanta::electro::quantity< Unit, Rep, Precision >::operator= ( const quantity< Unit, Rep, Precision > &  )
default

◆ zero()

template<typename Unit , typename Rep , typename Precision >
static constexpr quantity quanta::electro::quantity< Unit, Rep, Precision >::zero ( )
inlinestaticconstexprnoexcept

Returns a zero quantity.

Definition at line 383 of file electro.hpp.

References quanta::electro::quantity< Unit, Rep, Precision >::quantity().


The documentation for this class was generated from the following file: