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

A quantity of a spatial unit with a representation and precision. More...

#include <quanta/spatial.hpp>

Inheritance diagram for quanta::spatial::quantity< Unit, Rep, Precision >:

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
 Returns a copy of this quantity.
 
constexpr quantity< Unit, typename std::common_type< rep >::type, precision > operator- () const
 Returns the negation of this quantity.
 
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::spatial::quantity< Unit, Rep, Precision >

A quantity of a spatial unit with a representation and precision.

Similar to std::chrono::duration, quantity represents an amount of some spatial unit (meters, square meters or cubic meters). The precision is expressed as a std::ratio of one base unit, so millimeters have precision std::milli, inches have precision std::ratio<127, 5000> (exactly 0.0254 m) and milliliters have precision std::micro (of one cubic meter).

Examples

// Integer quantities (default)
millimeters width{100};
feet height{9};
auto run = 5_km;
// Arithmetic within a unit
auto sum = 100_mm + 5_cm; // 150 mm
auto scaled = 12_in * 3; // 36 in
auto ratio = 1_mi / 1_ft; // 5280 (scalar)
// Arithmetic across units
auto patch = 3_ft * 4_ft; // 12 ft²
auto box = patch * 2_ft; // 24 ft³
length< int64_t, std::milli > millimeters
Length with 1 mm precision.
Definition spatial.hpp:727
length< int64_t, std::ratio< 381, 1250 > > feet
Length with 1 ft (12 in = 0.3048 m) precision.
Definition spatial.hpp:740
constexpr scaled_t scaled
Selects the scaled form in to_string().
Definition core.hpp:161

Lengths (and only lengths) have a nested period alias, equal to precision, matching std::chrono::duration::period. It gives a length the same shape as a duration, so it can be passed to generic code that expects a duration-like or distance-like type, such as quanta::frequency::frequency::wavelength<quanta::spatial::millimeters>(). Areas and volumes deliberately lack it, so such code rejects them.

Template Parameters
UnitThe unit tag type (meter_unit, square_meter_unit or cubic_meter_unit).
RepArithmetic type for the tick count.
PrecisionA std::ratio representing the precision (default: 1).

Definition at line 231 of file spatial.hpp.

Member Typedef Documentation

◆ precision

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

The precision as a std::ratio.

Definition at line 242 of file spatial.hpp.

◆ rep

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

The representation type.

Definition at line 240 of file spatial.hpp.

◆ unit

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

The unit tag type.

Definition at line 238 of file spatial.hpp.

Constructor & Destructor Documentation

◆ quantity() [1/5]

◆ quantity() [2/5]

template<typename Unit , typename Rep , typename Precision >
quanta::spatial::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::spatial::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 259 of file spatial.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::spatial::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 280 of file spatial.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::spatial::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 303 of file spatial.hpp.

◆ ~quantity()

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

Member Function Documentation

◆ count()

◆ max()

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

Returns the maximum representable quantity.

Definition at line 377 of file spatial.hpp.

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

◆ min()

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

Returns the minimum representable quantity.

Definition at line 374 of file spatial.hpp.

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

◆ operator%=() [1/2]

template<typename Unit , typename Rep , typename Precision >
constexpr quantity & quanta::spatial::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::spatial::quantity< Unit, Rep, Precision >::operator%= ( const rep &  r)
inlineconstexpr

Definition at line 356 of file spatial.hpp.

◆ operator*=()

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

Definition at line 346 of file spatial.hpp.

◆ operator+()

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

Returns a copy of this quantity.

Definition at line 313 of file spatial.hpp.

◆ operator++() [1/2]

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

Definition at line 322 of file spatial.hpp.

◆ operator++() [2/2]

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

◆ operator+=()

template<typename Unit , typename Rep , typename Precision >
constexpr quantity & quanta::spatial::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::spatial::quantity< Unit, Rep, Precision >::operator- ( ) const
inlineconstexpr

Returns the negation of this quantity.

Definition at line 318 of file spatial.hpp.

◆ operator--() [1/2]

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

Definition at line 329 of file spatial.hpp.

◆ operator--() [2/2]

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

◆ operator-=()

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

◆ operator/=()

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

Definition at line 351 of file spatial.hpp.

◆ operator=()

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

◆ zero()

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

Returns a zero quantity.

Definition at line 371 of file spatial.hpp.

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


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