electro 0.4.1
Type-safe electrical units library modeled after std::chrono
Loading...
Searching...
No Matches
electro::quantity< Unit, Rep, Precision > Class Template Reference

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

#include <electro/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> && (treat_as_inexact_v<rep> || (_harmonic_precision<Precision2, precision> && !treat_as_inexact_v<Rep2>))
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 (!std::is_same_v<quantity, quantity<Unit, Rep2, Precision2>>) && (!treat_as_inexact_v<rep>) && (!_harmonic_precision<Precision2, precision>)
constexpr quantity (const quantity< Unit, Rep2, Precision2 > &q)
 Explicit constructor for lossy precision 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 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)
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
A quantity of an electrical unit with a representation and precision.
Definition electro.hpp:324
constexpr scaled_t scaled
Selects the scaled (engineering) form in to_string().
Definition electro.hpp:1471
constexpr bool treat_as_inexact_v
Definition electro.hpp:200
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 324 of file electro.hpp.

Member Typedef Documentation

◆ precision

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

The precision as a std::ratio.

Definition at line 335 of file electro.hpp.

◆ rep

The representation type.

Definition at line 333 of file electro.hpp.

◆ unit

The unit tag type.

Definition at line 331 of file electro.hpp.

Constructor & Destructor Documentation

◆ quantity() [1/5]

◆ quantity() [2/5]

◆ quantity() [3/5]

template<typename Rep2 >
requires std::convertible_to<const Rep2&, rep> && (treat_as_inexact_v<rep> || !treat_as_inexact_v<Rep2>)
constexpr 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 352 of file electro.hpp.

◆ quantity() [4/5]

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

Implicit when the conversion is lossless (target precision evenly divides source precision).

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

Definition at line 369 of file electro.hpp.

◆ quantity() [5/5]

constexpr electro::quantity< Unit, Rep, Precision >::quantity ( const quantity< Unit, Rep2, Precision2 > &  q)
inlineexplicitconstexpr

Explicit constructor for lossy precision conversions.

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

Definition at line 382 of file electro.hpp.

◆ ~quantity()

Member Function Documentation

◆ count()

◆ max()

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

Returns the maximum representable quantity.

Definition at line 454 of file electro.hpp.

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

Referenced by electro::level< Reference, Rep, Precision >::max().

◆ min()

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

Returns the minimum representable quantity.

Definition at line 451 of file electro.hpp.

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

Referenced by electro::level< Reference, Rep, Precision >::min().

◆ operator%=() [1/2]

Definition at line 440 of file electro.hpp.

References electro::treat_as_inexact_v.

◆ operator%=() [2/2]

Definition at line 433 of file electro.hpp.

References electro::treat_as_inexact_v.

◆ operator*=()

Definition at line 423 of file electro.hpp.

References electro::treat_as_inexact_v.

◆ operator+()

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

Definition at line 391 of file electro.hpp.

◆ operator++() [1/2]

Definition at line 399 of file electro.hpp.

◆ operator++() [2/2]

◆ operator+=()

Definition at line 413 of file electro.hpp.

References electro::treat_as_inexact_v.

◆ operator-()

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

Definition at line 395 of file electro.hpp.

◆ operator--() [1/2]

Definition at line 406 of file electro.hpp.

◆ operator--() [2/2]

◆ operator-=()

Definition at line 418 of file electro.hpp.

References electro::treat_as_inexact_v.

◆ operator/=()

Definition at line 428 of file electro.hpp.

References electro::treat_as_inexact_v.

◆ operator=()

◆ zero()

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

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