morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
morph::units::Quantity< U, DeclaredDecimals > Struct Template Reference

A unit-tagged, optionally-empty exact value with a declared precision in its type and (optionally) a recorded derivation. More...

#include <quantity.hpp>

Public Member Functions

constexpr Quantity () noexcept=default
 Constructs the empty state (no recorded derivation).
 Quantity (math::Rational engaged)
 Engages with engaged (which keeps its own runtime precision).
 Quantity (std::optional< math::Rational > adopted)
 Adopts an optional payload as-is.
template<std::uint32_t OtherDecimals>
requires (OtherDecimals != DeclaredDecimals)
 Quantity (const Quantity< U, OtherDecimals > &other)
 Converts from the same unit at another declared precision; the value, its runtime tag, and its derivation carry over unchanged.
constexpr bool hasValue () const noexcept
 Whether a value has been entered/measured.
constexpr bool withinDeclaredBounds () const noexcept
 Pre-decode validation seam: whether the current payload falls within this field's unit-declared bounds (UnitTraits<E>::bounds), when the unit declares any. An empty payload, or a unit with no declared bounds() (HasUnitBounds not satisfied), is always within bounds — this is an opt-in check, not a new default restriction, so a unit system that declares no bounds behaves exactly as it did before this feature existed.
constexpr const std::optional< math::Rational > & value () const noexcept
 The payload, for pattern-matching / -> access.
constexpr math::Rational value_or (const math::Rational &fallback) const noexcept
 The payload if engaged, otherwise a caller-supplied fallback.
constexpr const math::Rational & operator* () const noexcept
 Unchecked access to the engaged value (UB when empty, exactly like std::optional::operator*).
Quantity withDecimalPlaces (math::DecimalPlaces newPrecision) const
 Retags the value's runtime precision (the exact value itself is unchanged — precision only affects rounding and formatting).
Quantity roundedToDecimalPlaces (math::DecimalPlaces newPrecision, math::RoundingMode mode=math::RoundingMode::HalfAwayFromZero) const
 Rounds the value to a runtime precision, changing the exact value as well as the tag — unlike withDecimalPlaces, which retags only and leaves display and storage free to disagree.
Quantity atDeclaredPrecision () const
 Rounds the value to this field's declared precision — the one the generated schema advertises as x-decimalPlaces.
Quantity named (std::string label) const
 Seals this value with a name: equation() shows the name instead of expanding its derivation. Builds a fresh node (never mutates a shared one); a no-op returning empty on empty or with tracing off.
std::vector< std::string > equation (std::size_t maxSteps=kDefaultEquationSteps) const
 The worked derivation as print-ready lines (see docs/spec/util/quantity_type.md).
template<auto To>
requires Convertible<U, To>
 operator Quantity< To > () const
 Implicit within-dimension conversion to another unit; delegates to convert, propagates empty, and records a provenance step.

Static Public Member Functions

static constexpr UnitMeta unitMeta () noexcept
 The unit's static metadata (id, display text, default decimals).
static constexpr math::DecimalPlaces declaredPrecision () noexcept
 The declared precision as a DecimalPlaces strong type.
static constexpr std::span< const UnitAlternative< decltype(U)> > unitAlternatives () noexcept
 The convertible display/entry units for this field's unit, derived from UnitTraits::relations.
static Quantity fromDouble (double raw) noexcept
 Converts a floating-point reading at the declared precision.
static Quantity fromOptional (std::optional< math::Rational > adopted)
 Wraps an optional payload, preserving the declared precision.

Public Attributes

std::optional< math::Rational > payload
 The payload; std::nullopt means "not entered / not measured".

Static Public Attributes

static constexpr auto unit = U
 The unit this quantity is denominated in (compile-time).
static constexpr std::uint32_t declaredDecimals = DeclaredDecimals
 Declared decimal places of this field (compile-time).

Detailed Description

template<auto U, std::uint32_t DeclaredDecimals>
requires UnitEnum<decltype(U)>
struct morph::units::Quantity< U, DeclaredDecimals >

A unit-tagged, optionally-empty exact value with a declared precision in its type and (optionally) a recorded derivation.

Template Parameters
UAn enumerator of an application unit enum satisfying UnitEnum.
DeclaredDecimalsDeclared decimal places of the field; defaults to the unit's UnitMeta::defaultDecimals.

Definition at line 671 of file quantity.hpp.

Constructor & Destructor Documentation

◆ Quantity() [1/3]

template<auto U, std::uint32_t DeclaredDecimals>
morph::units::Quantity< U, DeclaredDecimals >::Quantity ( math::Rational engaged)
inline

Engages with engaged (which keeps its own runtime precision).

Parameters
engagedThe exact value to hold.

Definition at line 688 of file quantity.hpp.

◆ Quantity() [2/3]

template<auto U, std::uint32_t DeclaredDecimals>
morph::units::Quantity< U, DeclaredDecimals >::Quantity ( std::optional< math::Rational > adopted)
inline

Adopts an optional payload as-is.

Parameters
adoptedEngaged or empty payload.

Definition at line 692 of file quantity.hpp.

◆ Quantity() [3/3]

template<auto U, std::uint32_t DeclaredDecimals>
template<std::uint32_t OtherDecimals>
requires (OtherDecimals != DeclaredDecimals)
morph::units::Quantity< U, DeclaredDecimals >::Quantity ( const Quantity< U, OtherDecimals > & other)
inline

Converts from the same unit at another declared precision; the value, its runtime tag, and its derivation carry over unchanged.

Parameters
otherSame-unit quantity with a different declared precision.

Definition at line 699 of file quantity.hpp.

Member Function Documentation

◆ unitMeta()

template<auto U, std::uint32_t DeclaredDecimals>
constexpr UnitMeta morph::units::Quantity< U, DeclaredDecimals >::unitMeta ( )
inlinestaticnodiscardconstexprnoexcept

The unit's static metadata (id, display text, default decimals).

Returns
The UnitMeta from the application's UnitTraits specialisation.

Definition at line 713 of file quantity.hpp.

◆ declaredPrecision()

template<auto U, std::uint32_t DeclaredDecimals>
constexpr math::DecimalPlaces morph::units::Quantity< U, DeclaredDecimals >::declaredPrecision ( )
inlinestaticnodiscardconstexprnoexcept

The declared precision as a DecimalPlaces strong type.

Returns
DecimalPlaces{declaredDecimals}.

Definition at line 717 of file quantity.hpp.

◆ unitAlternatives()

template<auto U, std::uint32_t DeclaredDecimals>
constexpr std::span< const UnitAlternative< decltype(U)> > morph::units::Quantity< U, DeclaredDecimals >::unitAlternatives ( )
inlinestaticnodiscardconstexprnoexcept

The convertible display/entry units for this field's unit, derived from UnitTraits::relations.

Returns
Exact-ratio alternatives (empty when the unit system declares none).

Definition at line 724 of file quantity.hpp.

◆ fromDouble()

template<auto U, std::uint32_t DeclaredDecimals>
Quantity morph::units::Quantity< U, DeclaredDecimals >::fromDouble ( double raw)
inlinestaticnodiscardnoexcept

Converts a floating-point reading at the declared precision.

Parameters
rawThe value to convert.
Returns
The engaged quantity, or empty when raw is not finite or does not fit int64 (empty-propagation philosophy: no error channel).

Definition at line 732 of file quantity.hpp.

◆ fromOptional()

template<auto U, std::uint32_t DeclaredDecimals>
Quantity morph::units::Quantity< U, DeclaredDecimals >::fromOptional ( std::optional< math::Rational > adopted)
inlinestaticnodiscard

Wraps an optional payload, preserving the declared precision.

Parameters
adoptedEngaged or empty payload.
Returns
The quantity (empty in → empty out).

Definition at line 742 of file quantity.hpp.

◆ hasValue()

template<auto U, std::uint32_t DeclaredDecimals>
bool morph::units::Quantity< U, DeclaredDecimals >::hasValue ( ) const
inlinenodiscardconstexprnoexcept

Whether a value has been entered/measured.

Returns
true if the payload is engaged.

Definition at line 748 of file quantity.hpp.

◆ withinDeclaredBounds()

template<auto U, std::uint32_t DeclaredDecimals>
bool morph::units::Quantity< U, DeclaredDecimals >::withinDeclaredBounds ( ) const
inlinenodiscardconstexprnoexcept

Pre-decode validation seam: whether the current payload falls within this field's unit-declared bounds (UnitTraits<E>::bounds), when the unit declares any. An empty payload, or a unit with no declared bounds() (HasUnitBounds not satisfied), is always within bounds — this is an opt-in check, not a new default restriction, so a unit system that declares no bounds behaves exactly as it did before this feature existed.

Comparison is on the exact Rational value only (never a lossy double), consistent with every other exact comparison in this header.

Returns
true when empty, when the unit declares no bounds, or when the engaged value satisfies min <= value <= max.

Definition at line 763 of file quantity.hpp.

◆ value()

template<auto U, std::uint32_t DeclaredDecimals>
const std::optional< math::Rational > & morph::units::Quantity< U, DeclaredDecimals >::value ( ) const
inlinenodiscardconstexprnoexcept

The payload, for pattern-matching / -> access.

Returns
Const reference to the optional payload — into this Quantity, so valid only for as long as it is.

Definition at line 779 of file quantity.hpp.

◆ value_or()

template<auto U, std::uint32_t DeclaredDecimals>
math::Rational morph::units::Quantity< U, DeclaredDecimals >::value_or ( const math::Rational & fallback) const
inlinenodiscardconstexprnoexcept

The payload if engaged, otherwise a caller-supplied fallback.

Parameters
fallbackValue to return when empty.
Returns
The engaged value or fallback.

Definition at line 786 of file quantity.hpp.

◆ operator*()

template<auto U, std::uint32_t DeclaredDecimals>
const math::Rational & morph::units::Quantity< U, DeclaredDecimals >::operator* ( ) const
inlinenodiscardconstexprnoexcept

Unchecked access to the engaged value (UB when empty, exactly like std::optional::operator*).

Returns
The engaged exact value — a reference into this Quantity, valid only for as long as it is.

Definition at line 795 of file quantity.hpp.

◆ withDecimalPlaces()

template<auto U, std::uint32_t DeclaredDecimals>
Quantity morph::units::Quantity< U, DeclaredDecimals >::withDecimalPlaces ( math::DecimalPlaces newPrecision) const
inlinenodiscard

Retags the value's runtime precision (the exact value itself is unchanged — precision only affects rounding and formatting).

This is the display-hint half of the pair: after it, the value shown at newPrecision and the value stored may differ. Use roundedToDecimalPlaces when the stored value must be the one displayed.

Parameters
newPrecisionRuntime precision; silently clamped to the valid range.
Returns
The retagged quantity, or empty if this is empty.

Definition at line 805 of file quantity.hpp.

◆ roundedToDecimalPlaces()

template<auto U, std::uint32_t DeclaredDecimals>
Quantity morph::units::Quantity< U, DeclaredDecimals >::roundedToDecimalPlaces ( math::DecimalPlaces newPrecision,
math::RoundingMode mode = math::RoundingMode::HalfAwayFromZero ) const
inlinenodiscard

Rounds the value to a runtime precision, changing the exact value as well as the tag — unlike withDecimalPlaces, which retags only and leaves display and storage free to disagree.

Delegates to math::roundToDecimalPlaces, and so shares its saturation behaviour: a value whose scaled form leaves int64 range clamps and logs instead of overflowing. A value already representable at newPrecision is unchanged apart from its tag.

Parameters
newPrecisionRuntime precision; silently clamped to the valid range.
modeTie-breaking rule; defaults to half away from zero, matching the decimal display path.
Returns
The rounded quantity, or empty if this is empty.

Definition at line 835 of file quantity.hpp.

◆ atDeclaredPrecision()

template<auto U, std::uint32_t DeclaredDecimals>
Quantity morph::units::Quantity< U, DeclaredDecimals >::atDeclaredPrecision ( ) const
inlinenodiscard

Rounds the value to this field's declared precision — the one the generated schema advertises as x-decimalPlaces.

The enforcement half of the x-decimalPlaces contract: the stored value becomes the value that renders at the declared precision, so a handler never persists digits the form does not show. Rounds half away from zero, the rule the decimal formatter uses.

Returns
The rounded quantity, or empty if this is empty.

Definition at line 860 of file quantity.hpp.

◆ named()

template<auto U, std::uint32_t DeclaredDecimals>
Quantity morph::units::Quantity< U, DeclaredDecimals >::named ( std::string label) const
inlinenodiscard

Seals this value with a name: equation() shows the name instead of expanding its derivation. Builds a fresh node (never mutates a shared one); a no-op returning empty on empty or with tracing off.

Parameters
labelThe symbol name.
Returns
A same-unit quantity carrying the name.

Definition at line 867 of file quantity.hpp.

◆ equation()

template<auto U, std::uint32_t DeclaredDecimals>
requires UnitEnum<decltype(U)>
std::vector< std::string > morph::units::Quantity< U, DeclaredDecimals >::equation ( std::size_t maxSteps = kDefaultEquationSteps) const
inlinenodiscard

The worked derivation as print-ready lines (see docs/spec/util/quantity_type.md).

Parameters
maxStepsHow many derivation steps to write out, across the formula and the legend together. Past it a sub-derivation is elided: it renders as eK and its value appears in the where legend. kEquationStepsUnlimited writes the whole derivation out (unbounded, quadratic on a right-leaning chain); 0 returns the formatted value alone. Ignored with tracing off, which has no derivation to render either way.
Returns
[0] formula, [1] substitution, [2] result, [3..] where legend; a single formatted-value element for a degenerate root (empty, named root, bare leaf, tracing off, or maxSteps == 0).

Definition at line 894 of file quantity.hpp.

◆ operator Quantity< To >()

template<auto U, std::uint32_t DeclaredDecimals>
template<auto To>
requires Convertible<U, To>
morph::units::Quantity< U, DeclaredDecimals >::operator Quantity< To > ( ) const
inlinenodiscard

Implicit within-dimension conversion to another unit; delegates to convert, propagates empty, and records a provenance step.

Template Parameters
ToTarget unit enumerator (same enum, different value, convertible).
Returns
The converted quantity in unit To.

Definition at line 910 of file quantity.hpp.

Member Data Documentation

◆ payload

template<auto U, std::uint32_t DeclaredDecimals>
std::optional<math::Rational> morph::units::Quantity< U, DeclaredDecimals >::payload

The payload; std::nullopt means "not entered / not measured".

Definition at line 676 of file quantity.hpp.

◆ unit

template<auto U, std::uint32_t DeclaredDecimals>
auto morph::units::Quantity< U, DeclaredDecimals >::unit = U
staticconstexpr

The unit this quantity is denominated in (compile-time).

Definition at line 706 of file quantity.hpp.

◆ declaredDecimals

template<auto U, std::uint32_t DeclaredDecimals>
std::uint32_t morph::units::Quantity< U, DeclaredDecimals >::declaredDecimals = DeclaredDecimals
staticconstexpr

Declared decimal places of this field (compile-time).

Definition at line 709 of file quantity.hpp.


The documentation for this struct was generated from the following files: