electro 0.3.0
Type-safe electrical units library modeled after std::chrono
Loading...
Searching...
No Matches
electro.hpp File Reference
#include <chrono>
#include <compare>
#include <concepts>
#include <cstdint>
#include <limits>
#include <ratio>
#include <string>
#include <assert.h>

Go to the source code of this file.

Classes

struct  electro::is_quantity< T >
 Trait to detect quantity specializations. More...
 
struct  electro::is_quantity< quantity< Unit, Rep, Precision > >
 
struct  electro::quantity_values< Rep >
 Provides special values for quantity representations. More...
 
struct  electro::treat_as_inexact< T >
 Trait to indicate floating-point-like behavior. More...
 
struct  electro::volt_unit
 Voltage, measured in volts. More...
 
struct  electro::ampere_unit
 Electric current, measured in amperes. More...
 
struct  electro::ohm_unit
 Electrical resistance, measured in ohms. More...
 
struct  electro::watt_unit
 Power, measured in watts. More...
 
struct  electro::coulomb_unit
 Electric charge, measured in coulombs. More...
 
struct  electro::joule_unit
 Energy, measured in joules. More...
 
struct  electro::farad_unit
 Capacitance, measured in farads. More...
 
struct  electro::henry_unit
 Inductance, measured in henries. More...
 
class  electro::quantity< Unit, Rep, Precision >
 A quantity of an electrical unit with a representation and precision. More...
 
struct  electro::quantity_suffix< Unit, Precision >
 Trait giving the display suffix for a (unit, precision) pair. More...
 
struct  std::common_type< electro::quantity< Unit, Rep1, Precision1 >, electro::quantity< Unit, Rep2, Precision2 > >
 

Namespaces

namespace  electro
 Electrical unit types and utilities.
 
namespace  std
 
namespace  electro_literals
 User-defined literals for electrical quantity types.
 

Macros

#define CONFIG_ELECTRO_STD_FORMAT   0
 

Typedefs

template<typename Rep , typename Precision = std::ratio<1>>
using electro::voltage = quantity< volt_unit, Rep, Precision >
 Voltage quantity with configurable representation and precision.
 
template<typename Rep , typename Precision = std::ratio<1>>
using electro::current = quantity< ampere_unit, Rep, Precision >
 Current quantity with configurable representation and precision.
 
template<typename Rep , typename Precision = std::ratio<1>>
using electro::resistance = quantity< ohm_unit, Rep, Precision >
 Resistance quantity with configurable representation and precision.
 
template<typename Rep , typename Precision = std::ratio<1>>
using electro::power = quantity< watt_unit, Rep, Precision >
 Power quantity with configurable representation and precision.
 
template<typename Rep , typename Precision = std::ratio<1>>
using electro::charge = quantity< coulomb_unit, Rep, Precision >
 Charge quantity with configurable representation and precision.
 
template<typename Rep , typename Precision = std::ratio<1>>
using electro::energy = quantity< joule_unit, Rep, Precision >
 Energy quantity with configurable representation and precision.
 
template<typename Rep , typename Precision = std::ratio<1>>
using electro::capacitance = quantity< farad_unit, Rep, Precision >
 Capacitance quantity with configurable representation and precision.
 
template<typename Rep , typename Precision = std::ratio<1>>
using electro::inductance = quantity< henry_unit, Rep, Precision >
 Inductance quantity with configurable representation and precision.
 
using electro::microvolts = voltage< int64_t, std::micro >
 Voltage with 1 µV precision.
 
using electro::millivolts = voltage< int64_t, std::milli >
 Voltage with 1 mV precision.
 
using electro::volts = voltage< int64_t >
 Voltage with 1 V precision.
 
using electro::kilovolts = voltage< int64_t, std::kilo >
 Voltage with 1 kV precision.
 
using electro::microamperes = current< int64_t, std::micro >
 Current with 1 µA precision.
 
using electro::milliamperes = current< int64_t, std::milli >
 Current with 1 mA precision.
 
using electro::amperes = current< int64_t >
 Current with 1 A precision.
 
using electro::microamps = microamperes
 Shorthand for microamperes.
 
using electro::milliamps = milliamperes
 Shorthand for milliamperes.
 
using electro::amps = amperes
 Shorthand for amperes.
 
using electro::milliohms = resistance< int64_t, std::milli >
 Resistance with 1 mΩ precision.
 
using electro::ohms = resistance< int64_t >
 Resistance with 1 Ω precision.
 
using electro::kiloohms = resistance< int64_t, std::kilo >
 Resistance with 1 kΩ precision.
 
using electro::megaohms = resistance< int64_t, std::mega >
 Resistance with 1 MΩ precision.
 
using electro::gigaohms = resistance< int64_t, std::giga >
 Resistance with 1 GΩ precision.
 
using electro::microwatts = power< int64_t, std::micro >
 Power with 1 µW precision.
 
using electro::milliwatts = power< int64_t, std::milli >
 Power with 1 mW precision.
 
using electro::watts = power< int64_t >
 Power with 1 W precision.
 
using electro::kilowatts = power< int64_t, std::kilo >
 Power with 1 kW precision.
 
using electro::megawatts = power< int64_t, std::mega >
 Power with 1 MW precision.
 
using electro::gigawatts = power< int64_t, std::giga >
 Power with 1 GW precision.
 
using electro::microcoulombs = charge< int64_t, std::micro >
 Charge with 1 µC precision.
 
using electro::millicoulombs = charge< int64_t, std::milli >
 Charge with 1 mC precision.
 
using electro::coulombs = charge< int64_t >
 Charge with 1 C precision.
 
using electro::milliampere_hours = charge< int64_t, std::ratio< 18, 5 > >
 Charge with 1 mAh (3.6 C) precision.
 
using electro::ampere_hours = charge< int64_t, std::ratio< 3600 > >
 Charge with 1 Ah (3600 C) precision.
 
using electro::millijoules = energy< int64_t, std::milli >
 Energy with 1 mJ precision.
 
using electro::joules = energy< int64_t >
 Energy with 1 J precision.
 
using electro::kilojoules = energy< int64_t, std::kilo >
 Energy with 1 kJ precision.
 
using electro::megajoules = energy< int64_t, std::mega >
 Energy with 1 MJ precision.
 
using electro::watt_hours = energy< int64_t, std::ratio< 3600 > >
 Energy with 1 Wh (3600 J) precision.
 
using electro::kilowatt_hours = energy< int64_t, std::ratio< 3600000 > >
 Energy with 1 kWh (3,600,000 J) precision.
 
using electro::picofarads = capacitance< int64_t, std::pico >
 Capacitance with 1 pF precision.
 
using electro::nanofarads = capacitance< int64_t, std::nano >
 Capacitance with 1 nF precision.
 
using electro::microfarads = capacitance< int64_t, std::micro >
 Capacitance with 1 µF precision.
 
using electro::millifarads = capacitance< int64_t, std::milli >
 Capacitance with 1 mF precision.
 
using electro::farads = capacitance< int64_t >
 Capacitance with 1 F precision.
 
using electro::nanohenries = inductance< int64_t, std::nano >
 Inductance with 1 nH precision.
 
using electro::microhenries = inductance< int64_t, std::micro >
 Inductance with 1 µH precision.
 
using electro::millihenries = inductance< int64_t, std::milli >
 Inductance with 1 mH precision.
 
using electro::henries = inductance< int64_t >
 Inductance with 1 H precision.
 

Functions

template<typename ToQuantity , typename Unit , typename Rep , typename Precision >
constexpr ToQuantity electro::quantity_cast (const quantity< Unit, Rep, Precision > &q)
 Converts a quantity to a different precision or representation.
 
template<typename ToQuantity , typename Unit , typename Rep , typename Precision >
constexpr ToQuantity electro::floor (const quantity< Unit, Rep, Precision > &q)
 Converts a quantity to the target type, rounding toward negative infinity.
 
template<typename ToQuantity , typename Unit , typename Rep , typename Precision >
constexpr ToQuantity electro::ceil (const quantity< Unit, Rep, Precision > &q)
 Converts a quantity to the target type, rounding toward positive infinity.
 
template<typename ToQuantity , typename Unit , typename Rep , typename Precision >
constexpr ToQuantity electro::round (const quantity< Unit, Rep, Precision > &q)
 Converts a quantity to the target type, rounding to nearest (ties to even).
 
template<typename Unit , typename Rep , typename Precision >
constexpr quantity< Unit, Rep, Precisionelectro::abs (const quantity< Unit, Rep, Precision > &q)
 Returns the absolute value of a quantity.
 
template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
constexpr auto electro::operator+ (const quantity< Unit, Rep1, Precision1 > &lhs, const quantity< Unit, Rep2, Precision2 > &rhs) -> std::common_type_t< quantity< Unit, Rep1, Precision1 >, quantity< Unit, Rep2, Precision2 > >
 Returns the sum of two quantities of the same unit.
 
template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
constexpr auto electro::operator- (const quantity< Unit, Rep1, Precision1 > &lhs, const quantity< Unit, Rep2, Precision2 > &rhs) -> std::common_type_t< quantity< Unit, Rep1, Precision1 >, quantity< Unit, Rep2, Precision2 > >
 Returns the difference of two quantities of the same unit.
 
template<typename Unit , typename Rep1 , typename Precision , typename Rep2 >
requires not_quantity<Rep2> && std::convertible_to<const Rep2&, std::common_type_t<Rep1, Rep2>>
constexpr auto electro::operator* (const quantity< Unit, Rep1, Precision > &q, const Rep2 &r) -> quantity< Unit, std::common_type_t< Rep1, Rep2 >, Precision >
 Multiplies a quantity by a scalar.
 
template<typename Unit , typename Rep1 , typename Rep2 , typename Precision >
requires not_quantity<Rep1> && std::convertible_to<const Rep1&, std::common_type_t<Rep1, Rep2>>
constexpr auto electro::operator* (const Rep1 &r, const quantity< Unit, Rep2, Precision > &q) -> quantity< Unit, std::common_type_t< Rep1, Rep2 >, Precision >
 Multiplies a scalar by a quantity.
 
template<typename Unit , typename Rep1 , typename Precision , typename Rep2 >
requires not_quantity<Rep2> && std::convertible_to<const Rep2&, std::common_type_t<Rep1, Rep2>>
constexpr auto electro::operator/ (const quantity< Unit, Rep1, Precision > &q, const Rep2 &s) -> quantity< Unit, std::common_type_t< Rep1, Rep2 >, Precision >
 Divides a quantity by a scalar.
 
template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
constexpr auto electro::operator/ (const quantity< Unit, Rep1, Precision1 > &lhs, const quantity< Unit, Rep2, Precision2 > &rhs) -> std::common_type_t< Rep1, Rep2 >
 Divides two quantities of the same unit, returning a scalar.
 
template<typename Unit , typename Rep1 , typename Precision , typename Rep2 >
requires not_quantity<Rep2> && std::convertible_to<const Rep2&, std::common_type_t<Rep1, Rep2>> && (!treat_as_inexact_v<Rep1> && !treat_as_inexact_v<Rep2>)
constexpr auto electro::operator% (const quantity< Unit, Rep1, Precision > &q, const Rep2 &s) -> quantity< Unit, std::common_type_t< Rep1, Rep2 >, Precision >
 Returns the remainder of dividing a quantity by a scalar.
 
template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
requires (!treat_as_inexact_v<Rep1> && !treat_as_inexact_v<Rep2>)
constexpr auto electro::operator% (const quantity< Unit, Rep1, Precision1 > &lhs, const quantity< Unit, Rep2, Precision2 > &rhs) -> std::common_type_t< quantity< Unit, Rep1, Precision1 >, quantity< Unit, Rep2, Precision2 > >
 Returns the remainder of dividing two quantities of the same unit.
 
template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
constexpr bool electro::operator== (const quantity< Unit, Rep1, Precision1 > &lhs, const quantity< Unit, Rep2, Precision2 > &rhs)
 
template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
requires std::three_way_comparable<std::common_type_t<Rep1, Rep2>>
constexpr auto electro::operator<=> (const quantity< Unit, Rep1, Precision1 > &lhs, const quantity< Unit, Rep2, Precision2 > &rhs)
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator* (const quantity< ampere_unit, R1, P1 > &i, const quantity< ohm_unit, R2, P2 > &r)
 Ohm's law: current x resistance = voltage.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator* (const quantity< ohm_unit, R1, P1 > &r, const quantity< ampere_unit, R2, P2 > &i)
 Ohm's law: resistance x current = voltage.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator/ (const quantity< volt_unit, R1, P1 > &v, const quantity< ohm_unit, R2, P2 > &r)
 Ohm's law: voltage / resistance = current.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator/ (const quantity< volt_unit, R1, P1 > &v, const quantity< ampere_unit, R2, P2 > &i)
 Ohm's law: voltage / current = resistance.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator* (const quantity< volt_unit, R1, P1 > &v, const quantity< ampere_unit, R2, P2 > &i)
 Power law: voltage x current = power.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator* (const quantity< ampere_unit, R1, P1 > &i, const quantity< volt_unit, R2, P2 > &v)
 Power law: current x voltage = power.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator/ (const quantity< watt_unit, R1, P1 > &p, const quantity< volt_unit, R2, P2 > &v)
 Power law: power / voltage = current.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator/ (const quantity< watt_unit, R1, P1 > &p, const quantity< ampere_unit, R2, P2 > &i)
 Power law: power / current = voltage.
 
template<typename R1 , typename P1 , duration_like D>
constexpr auto electro::operator* (const quantity< ampere_unit, R1, P1 > &i, const D &t)
 Charge: current x time = charge.
 
template<duration_like D, typename R1 , typename P1 >
constexpr auto electro::operator* (const D &t, const quantity< ampere_unit, R1, P1 > &i)
 Charge: time x current = charge.
 
template<typename R1 , typename P1 , duration_like D>
constexpr auto electro::operator/ (const quantity< coulomb_unit, R1, P1 > &q, const D &t)
 Charge: charge / time = current.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator/ (const quantity< coulomb_unit, R1, P1 > &q, const quantity< ampere_unit, R2, P2 > &i)
 Charge: charge / current = time.
 
template<typename R1 , typename P1 , duration_like D>
constexpr auto electro::operator* (const quantity< watt_unit, R1, P1 > &p, const D &t)
 Energy: power x time = energy.
 
template<duration_like D, typename R1 , typename P1 >
constexpr auto electro::operator* (const D &t, const quantity< watt_unit, R1, P1 > &p)
 Energy: time x power = energy.
 
template<typename R1 , typename P1 , duration_like D>
constexpr auto electro::operator/ (const quantity< joule_unit, R1, P1 > &e, const D &t)
 Energy: energy / time = power.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator/ (const quantity< joule_unit, R1, P1 > &e, const quantity< watt_unit, R2, P2 > &p)
 Energy: energy / power = time.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator/ (const quantity< joule_unit, R1, P1 > &e, const quantity< volt_unit, R2, P2 > &v)
 Energy: energy / voltage = charge.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator/ (const quantity< joule_unit, R1, P1 > &e, const quantity< coulomb_unit, R2, P2 > &q)
 Energy: energy / charge = voltage.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator* (const quantity< farad_unit, R1, P1 > &c, const quantity< volt_unit, R2, P2 > &v)
 Capacitance: capacitance x voltage = charge.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator* (const quantity< volt_unit, R1, P1 > &v, const quantity< farad_unit, R2, P2 > &c)
 Capacitance: voltage x capacitance = charge.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator/ (const quantity< coulomb_unit, R1, P1 > &q, const quantity< volt_unit, R2, P2 > &v)
 Capacitance: charge / voltage = capacitance.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator/ (const quantity< coulomb_unit, R1, P1 > &q, const quantity< farad_unit, R2, P2 > &c)
 Capacitance: charge / capacitance = voltage.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator* (const quantity< ohm_unit, R1, P1 > &r, const quantity< farad_unit, R2, P2 > &c)
 RC time constant: resistance x capacitance = time.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator* (const quantity< farad_unit, R1, P1 > &c, const quantity< ohm_unit, R2, P2 > &r)
 RC time constant: capacitance x resistance = time.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto electro::operator/ (const quantity< henry_unit, R1, P1 > &l, const quantity< ohm_unit, R2, P2 > &r)
 L/R time constant: inductance / resistance = time.
 
template<typename Unit , typename Rep , typename Precision >
requires _has_suffix<Unit, typename Precision::type>
std::string electro::to_string (const quantity< Unit, Rep, Precision > &q)
 Converts a quantity to a string with its unit suffix.
 
template<char... Digits>
constexpr electro::microvolts electro_literals::operator""_uV ()
 Literal for microvolts (e.g., 500_uV).
 
template<char... Digits>
constexpr electro::millivolts electro_literals::operator""_mV ()
 Literal for millivolts (e.g., 3300_mV).
 
template<char... Digits>
constexpr electro::volts electro_literals::operator""_V ()
 Literal for volts (e.g., 5_V).
 
template<char... Digits>
constexpr electro::kilovolts electro_literals::operator""_kV ()
 Literal for kilovolts (e.g., 11_kV).
 
template<char... Digits>
constexpr electro::microamperes electro_literals::operator""_uA ()
 Literal for microamperes (e.g., 250_uA).
 
template<char... Digits>
constexpr electro::milliamperes electro_literals::operator""_mA ()
 Literal for milliamperes (e.g., 150_mA).
 
template<char... Digits>
constexpr electro::amperes electro_literals::operator""_A ()
 Literal for amperes (e.g., 2_A).
 
template<char... Digits>
constexpr electro::milliohms electro_literals::operator""_mOhm ()
 Literal for milliohms (e.g., 50_mOhm).
 
template<char... Digits>
constexpr electro::ohms electro_literals::operator""_Ohm ()
 Literal for ohms (e.g., 220_Ohm).
 
template<char... Digits>
constexpr electro::kiloohms electro_literals::operator""_kOhm ()
 Literal for kiloohms (e.g., 10_kOhm).
 
template<char... Digits>
constexpr electro::megaohms electro_literals::operator""_MOhm ()
 Literal for megaohms (e.g., 1_MOhm).
 
template<char... Digits>
constexpr electro::milliohms electro_literals::operator""_mΩ ()
 Literal for milliohms (e.g., 50_mΩ).
 
template<char... Digits>
constexpr electro::ohms electro_literals::operator""_Ω ()
 Literal for ohms (e.g., 220_Ω).
 
template<char... Digits>
constexpr electro::kiloohms electro_literals::operator""_kΩ ()
 Literal for kiloohms (e.g., 10_kΩ).
 
template<char... Digits>
constexpr electro::megaohms electro_literals::operator""_MΩ ()
 Literal for megaohms (e.g., 1_MΩ).
 
template<char... Digits>
constexpr electro::microwatts electro_literals::operator""_uW ()
 Literal for microwatts (e.g., 500_uW).
 
template<char... Digits>
constexpr electro::milliwatts electro_literals::operator""_mW ()
 Literal for milliwatts (e.g., 250_mW).
 
template<char... Digits>
constexpr electro::watts electro_literals::operator""_W ()
 Literal for watts (e.g., 60_W).
 
template<char... Digits>
constexpr electro::kilowatts electro_literals::operator""_kW ()
 Literal for kilowatts (e.g., 2_kW).
 
template<char... Digits>
constexpr electro::megawatts electro_literals::operator""_MW ()
 Literal for megawatts (e.g., 5_MW).
 
template<char... Digits>
constexpr electro::coulombs electro_literals::operator""_C ()
 Literal for coulombs (e.g., 10_C).
 
template<char... Digits>
constexpr electro::milliampere_hours electro_literals::operator""_mAh ()
 Literal for milliampere-hours (e.g., 2000_mAh).
 
template<char... Digits>
constexpr electro::ampere_hours electro_literals::operator""_Ah ()
 Literal for ampere-hours (e.g., 2_Ah).
 
template<char... Digits>
constexpr electro::millijoules electro_literals::operator""_mJ ()
 Literal for millijoules (e.g., 500_mJ).
 
template<char... Digits>
constexpr electro::joules electro_literals::operator""_J ()
 Literal for joules (e.g., 100_J).
 
template<char... Digits>
constexpr electro::kilojoules electro_literals::operator""_kJ ()
 Literal for kilojoules (e.g., 4_kJ).
 
template<char... Digits>
constexpr electro::watt_hours electro_literals::operator""_Wh ()
 Literal for watt-hours (e.g., 100_Wh).
 
template<char... Digits>
constexpr electro::kilowatt_hours electro_literals::operator""_kWh ()
 Literal for kilowatt-hours (e.g., 10_kWh).
 
template<char... Digits>
constexpr electro::picofarads electro_literals::operator""_pF ()
 Literal for picofarads (e.g., 22_pF).
 
template<char... Digits>
constexpr electro::nanofarads electro_literals::operator""_nF ()
 Literal for nanofarads (e.g., 100_nF).
 
template<char... Digits>
constexpr electro::microfarads electro_literals::operator""_uF ()
 Literal for microfarads (e.g., 10_uF).
 
template<char... Digits>
constexpr electro::farads electro_literals::operator""_F ()
 Literal for farads (e.g., 1_F).
 
template<char... Digits>
constexpr electro::nanohenries electro_literals::operator""_nH ()
 Literal for nanohenries (e.g., 100_nH).
 
template<char... Digits>
constexpr electro::microhenries electro_literals::operator""_uH ()
 Literal for microhenries (e.g., 47_uH).
 
template<char... Digits>
constexpr electro::millihenries electro_literals::operator""_mH ()
 Literal for millihenries (e.g., 10_mH).
 
template<char... Digits>
constexpr electro::henries electro_literals::operator""_H ()
 Literal for henries (e.g., 1_H).
 

Variables

template<typename T >
constexpr bool electro::is_quantity_v = is_quantity<T>::value
 
template<typename T >
constexpr bool electro::treat_as_inexact_v = treat_as_inexact<T>::value
 

Macro Definition Documentation

◆ CONFIG_ELECTRO_STD_FORMAT

#define CONFIG_ELECTRO_STD_FORMAT   0

Definition at line 19 of file electro.hpp.