electro 0.3.0
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
 
quantityoperator= (const quantity &)=default
 
constexpr rep count () const
 Returns the tick count.
 
constexpr quantity< Unit, typename std::common_type< rep >::type, precisionoperator+ () const
 
constexpr quantity< Unit, typename std::common_type< rep >::type, precisionoperator- () const
 
constexpr quantityoperator++ ()
 
constexpr quantity operator++ (int)
 
constexpr quantityoperator-- ()
 
constexpr quantity operator-- (int)
 
constexpr quantityoperator+= (const quantity &q)
 
constexpr quantityoperator-= (const quantity &q)
 
constexpr quantityoperator*= (const rep &r)
 
constexpr quantityoperator/= (const rep &r)
 
constexpr quantityoperator%= (const rep &r)
 
constexpr quantityoperator%= (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:286
constexpr bool treat_as_inexact_v
Definition electro.hpp:162
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 286 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 297 of file electro.hpp.

◆ rep

The representation type.

Definition at line 295 of file electro.hpp.

◆ unit

The unit tag type.

Definition at line 293 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 314 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 331 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 344 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 416 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 413 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 402 of file electro.hpp.

References electro::treat_as_inexact_v.

◆ operator%=() [2/2]

Definition at line 395 of file electro.hpp.

References electro::treat_as_inexact_v.

◆ operator*=()

Definition at line 385 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 353 of file electro.hpp.

◆ operator++() [1/2]

Definition at line 361 of file electro.hpp.

◆ operator++() [2/2]

◆ operator+=()

Definition at line 375 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 357 of file electro.hpp.

◆ operator--() [1/2]

Definition at line 368 of file electro.hpp.

◆ operator--() [2/2]

◆ operator-=()

Definition at line 380 of file electro.hpp.

References electro::treat_as_inexact_v.

◆ operator/=()

Definition at line 390 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: