Unit Conversion and Dimensional Analysis Library 3.6.1
A compile-time, header-only C++23 dimensional-analysis library
Loading...
Searching...
No Matches
units::any_unit Class Reference

Public Member Functions

 any_unit (unit_identity id, double base)
 constructs an erased quantity from a decoded identity and SI-base magnitude
 any_unit ()
 constructs an empty erased quantity (dimensionless, zero) — the target for stream extraction
template<class Dimension>
bool is () const noexcept
 whether this erased quantity is of the requested dimension
double value_in_base () const noexcept
 the magnitude in SI canonical base units, for logging or routing
const unit_identityidentity () const noexcept
 the decoded dimension signature
std::span< const std::byte > bytes () const noexcept
 the serialized byte stream, as a type-safe view
 operator std::span< const std::byte > () const noexcept
 an any_unit is viewable as its serialized bytes
const char * data () const noexcept
 a pointer to the serialized bytes as const char*, for byte-oriented interfaces
std::size_t size () const noexcept
 the number of serialized bytes
std::string to_string () const
 a human-readable text rendering of the erased quantity, for logging and diagnostics
std::string to_string_raw () const
 the dimension-agnostic text rendering, keyed by name-hash — always available, never resolves a name
bool operator== (const any_unit &other) const noexcept
 whether two erased quantities are the same dimension and magnitude
bool operator!= (const any_unit &other) const noexcept
 whether two erased quantities differ in dimension or magnitude
std::partial_ordering operator<=> (const any_unit &other) const noexcept
 orders two erased quantities of the same dimension by magnitude
template<class Unit>
std::expected< Unit, deserialize_errorto () const
 collapses into a concrete unit, checked (the safe default)
template<class Unit>
Unit try_to () const
 collapses into a concrete unit, throwing on a dimension mismatch
template<class Unit>
bool assign_to (Unit &out) const
 collapses into an existing unit variable, leaving it untouched on a dimension mismatch
template<class... Dimensions, class Visitor>
void visit (Visitor &&visitor) const
 invokes a visitor with the canonical quantity for the decoded dimension

Constructor & Destructor Documentation

◆ any_unit() [1/2]

units::any_unit::any_unit ( unit_identity id,
double base )
inline

constructs an erased quantity from a decoded identity and SI-base magnitude

The serialized byte form is materialized once here (from the same encoder serialize uses), so it is owned by the any_unit and shares its lifetime: bytes(), data(), and size() view a buffer that stays valid for as long as the any_unit does.

Parameters
[in]idthe dimension signature
[in]basethe magnitude in SI canonical base

◆ any_unit() [2/2]

units::any_unit::any_unit ( )
inline

constructs an empty erased quantity (dimensionless, zero) — the target for stream extraction

Provided so any_unit value; stream >> value; is well-formed; operator>> overwrites it with the decoded quantity, or sets the stream's failbit and leaves it empty on malformed input.

Member Function Documentation

◆ assign_to()

template<class Unit>
bool units::any_unit::assign_to ( Unit & out) const
inline

collapses into an existing unit variable, leaving it untouched on a dimension mismatch

The mismatch-tolerant collapse: assigns into out and returns true iff the decoded dimension is out's dimension, otherwise returns false and leaves out unchanged. It is the ergonomic form of if (auto v = to<Unit>()) out = *v; — the target unit is deduced from out, so the value need not be named twice, and the boolean says whether the assignment happened. A value that would not fit out's underlying type (to's lossy_target) is also reported as not assigned, so out is written only with a value it represents exactly. Unlike try_to/unit_cast, a mismatch is not an error but an expected outcome — the shape for pulling one erased quantity into whichever of several typed fields it fits, without a throw or a named target at each site.

Template Parameters
Unitthe target unit type; deduced from out
Parameters
[out]outreceives the collapsed value on a dimension (and representability) match
Returns
true iff out was assigned

◆ bytes()

std::span< const std::byte > units::any_unit::bytes ( ) const
inlinenodiscardnoexcept

the serialized byte stream, as a type-safe view

A view into the buffer owned by this any_unit; valid for the object's lifetime. Feed it straight back to deserialize. Copy it (e.g. into a std::vector) if it must outlive the any_unit.

Returns
a span over the owned bytes

◆ data()

const char * units::any_unit::data ( ) const
inlinenodiscardnoexcept

a pointer to the serialized bytes as const char*, for byte-oriented interfaces

Paired with size(), this drops directly into the interfaces that take a const char*/const void* and a length — std::ostream::write, std::fwrite, a socket send — with no cast at the call site. The pointer views the buffer owned by this any_unit and is valid for its lifetime.

Returns
a pointer to the first byte

◆ identity()

const unit_identity & units::any_unit::identity ( ) const
inlinenodiscardnoexcept

the decoded dimension signature

Returns
the unit_identity

◆ is()

template<class Dimension>
bool units::any_unit::is ( ) const
inlinenodiscardnoexcept

whether this erased quantity is of the requested dimension

Template Parameters
Dimensiona dimension::* type
Returns
true iff the decoded dimension matches

◆ operator std::span< const std::byte >()

units::any_unit::operator std::span< const std::byte > ( ) const
inlinenodiscardnoexcept

an any_unit is viewable as its serialized bytes

Lets an any_unit pass directly to anything expecting a byte span — notably deserialize, so a serialize result feeds straight back in. The view is tied to this object's lifetime, as bytes().

Returns
a span over the owned bytes

◆ operator!=()

bool units::any_unit::operator!= ( const any_unit & other) const
inlinenodiscardnoexcept

whether two erased quantities differ in dimension or magnitude

Parameters
[in]otherthe erased quantity to compare against
Returns
true iff not equal

◆ operator<=>()

std::partial_ordering units::any_unit::operator<=> ( const any_unit & other) const
inlinenodiscardnoexcept

orders two erased quantities of the same dimension by magnitude

Ordering is only meaningful within a dimension: two lengths compare by their SI-base magnitude, but a length and a time have no order. Same-dimension operands compare by base value; operands of different dimensions are unordered, so <, <=, >, >= are all false between them. This is a std::partial_ordering precisely because the relation is partial across dimensions.

Parameters
[in]otherthe erased quantity to compare against
Returns
the base-value ordering when same-dimension, else std::partial_ordering::unordered

◆ operator==()

bool units::any_unit::operator== ( const any_unit & other) const
inlinenodiscardnoexcept

whether two erased quantities are the same dimension and magnitude

Equal iff the dimension signatures match and the SI-base magnitudes compare equal. The magnitude comparison uses the same relative-epsilon tolerance as the concrete unit comparison, so an any_unit compares no more strictly than the units it erases; two quantities of the same base value are equal regardless of the source unit (a serialized 1000 m equals a serialized 1 km).

Parameters
[in]otherthe erased quantity to compare against
Returns
true iff same dimension and (tolerantly) equal magnitude
Note
As with unit, the tolerance may not suit every application when the base value is a double; compare value_in_base() directly for a different criterion.

◆ size()

std::size_t units::any_unit::size ( ) const
inlinenodiscardnoexcept

the number of serialized bytes

Returns
the byte count

◆ to()

template<class Unit>
std::expected< Unit, deserialize_error > units::any_unit::to ( ) const
inlinenodiscard

collapses into a concrete unit, checked (the safe default)

Template Parameters
Unitthe target unit type
Returns
the value as Unit on a dimension match, else deserialize_error::dimension_mismatch

◆ to_string()

std::string units::any_unit::to_string ( ) const
inlinenodiscard

a human-readable text rendering of the erased quantity, for logging and diagnostics

For a dimension the library knows, renders the SI-base magnitude in that dimension's canonical unit with its unit name — the same text operator<<(ostream, unit) produces (e.g. 1000 m, 9.81 m s^-2) — resolved without the caller naming a target, over the same candidate set visit() uses. For a dimension outside that set (a user-defined make_dimension), the name cannot be recovered from the wire's name-hash (the runtime→type wall), so it degrades to the raw hash form of to_string_raw(). This is the TEXT form; operator<</operator>> on a stream move the raw BINARY bytes.

Returns
the text rendering — a named-unit form when the dimension is known, else the raw hash form

◆ to_string_raw()

std::string units::any_unit::to_string_raw ( ) const
inlinenodiscard

the dimension-agnostic text rendering, keyed by name-hash — always available, never resolves a name

The SI-base magnitude followed by each base term as #<hash>^<exponent> (a fractional exponent as #<hash>^<num>/<den>, dimensionless as [dimensionless]). Unlike to_string(), it never attempts to name the dimension, so it renders identically for a built-in and a user-defined dimension and is the honest diagnostic for a quantity whose type the library cannot know. to_string() falls back to this whenever no known dimension matched.

Returns
the raw hash-keyed text rendering

◆ try_to()

template<class Unit>
Unit units::any_unit::try_to ( ) const
inlinenodiscard

collapses into a concrete unit, throwing on a dimension mismatch

Template Parameters
Unitthe target unit type
Returns
the value as Unit

◆ value_in_base()

double units::any_unit::value_in_base ( ) const
inlinenodiscardnoexcept

the magnitude in SI canonical base units, for logging or routing

Returns
the value

◆ visit()

template<class... Dimensions, class Visitor>
void units::any_unit::visit ( Visitor && visitor) const
inline

invokes a visitor with the canonical quantity for the decoded dimension

The visitor is called with the canonical SI unit of whichever candidate dimension the stream holds, so no target type is named at the call site and all arithmetic inside the visitor is compile-time checked. With no explicit candidates, every dimension the library defines is a candidate (so velocity/force/energy/... resolve out of the box); pass explicit candidate dimensions (visit<my_dimension>(f)) to resolve a user-defined dimension or to disambiguate dimensions that share a signature (e.g. torque vs energy — the first listed wins). The visitor must be a generic callable (e.g. a [](auto q) lambda). Throws if no candidate matched.

Template Parameters
Dimensionscandidate dimension types (defaults to the library's known dimensions)
Visitora callable invocable with each candidate's canonical unit
Parameters
[in]visitorthe callable

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