11#include <commons/color.hpp>
12#include <commons/icon.hpp>
30template <MeasureLike M, NumericValue T =
double>
33 using unit_t = M::base_unit_t;
46 [[nodiscard]]
static std::shared_ptr<MeasureValue>
of(T value) {
47 return std::make_shared<MeasureValue>(value);
51 [[nodiscard]]
static std::unique_ptr<MeasureValue>
unique(T value) {
52 return std::make_unique<MeasureValue>(value);
56 [[nodiscard]]
static std::expected<MeasureValue, ParseError>
parse(std::string_view input) {
59 return std::unexpected{u.error()};
65 [[nodiscard]]
static constexpr std::string_view id() noexcept {
68 [[nodiscard]]
static constexpr std::string_view unit_id() noexcept {
71 [[nodiscard]]
static constexpr std::string_view name() noexcept {
74 [[nodiscard]]
static constexpr std::string_view symbol() noexcept {
75 return unit_t::symbol;
77 [[nodiscard]]
static constexpr std::string_view kind() noexcept {
80 [[nodiscard]]
static constexpr std::optional<comms::Icon> icon() noexcept {
81 return M::icon ? M::icon : unit_t::icon;
83 [[nodiscard]]
static constexpr std::optional<comms::Color> color() noexcept {
84 return M::color ? M::color : unit_t::color;
97 return M::descriptor();
100 return unit_t::descriptor();
102 [[nodiscard]]
double numeric_as_double() const noexcept
override {
103 return static_cast<double>(v);
106 [[nodiscard]] std::string to_string()
const override;
107 [[nodiscard]] std::string to_formatted_string()
const override;
109 [[nodiscard]] std::unique_ptr<IMeasureValue> clone()
const override {
110 return std::make_unique<MeasureValue>(*
this);
113 [[nodiscard]]
friend constexpr bool operator==(
const MeasureValue& a,
114 const MeasureValue& b)
noexcept {
117 [[nodiscard]]
friend constexpr auto operator<=>(
const MeasureValue& a,
118 const MeasureValue& b)
noexcept {
122 constexpr MeasureValue& operator+=(
const MeasureValue& rhs)
noexcept {
126 constexpr MeasureValue& operator-=(
const MeasureValue& rhs)
noexcept {
130 constexpr MeasureValue& operator*=(T s)
noexcept {
134 constexpr MeasureValue& operator/=(T s)
noexcept {
140template <MeasureLike M, NumericValue T>
141[[nodiscard]]
constexpr MeasureValue<M, T> operator+(
const MeasureValue<M, T>& a,
142 const MeasureValue<M, T>& b)
noexcept {
143 return MeasureValue<M, T>{
static_cast<T
>(a.v + b.v)};
146template <MeasureLike M, NumericValue T>
147[[nodiscard]]
constexpr MeasureValue<M, T> operator-(
const MeasureValue<M, T>& a,
148 const MeasureValue<M, T>& b)
noexcept {
149 return MeasureValue<M, T>{
static_cast<T
>(a.v - b.v)};
152template <MeasureLike M, NumericValue T>
153[[nodiscard]]
constexpr MeasureValue<M, T> operator-(
const MeasureValue<M, T>& a)
noexcept {
154 return MeasureValue<M, T>{
static_cast<T
>(-a.v)};
157template <MeasureLike M, NumericValue T>
158[[nodiscard]]
constexpr MeasureValue<M, T> operator+(
const MeasureValue<M, T>& a)
noexcept {
162template <MeasureLike M, NumericValue T>
163[[nodiscard]]
constexpr MeasureValue<M, T> operator*(
const MeasureValue<M, T>& a, T s)
noexcept {
164 return MeasureValue<M, T>{
static_cast<T
>(a.v * s)};
167template <MeasureLike M, NumericValue T>
168[[nodiscard]]
constexpr MeasureValue<M, T> operator*(T s,
const MeasureValue<M, T>& a)
noexcept {
169 return MeasureValue<M, T>{
static_cast<T
>(a.v * s)};
172template <MeasureLike M, NumericValue T>
173[[nodiscard]]
constexpr MeasureValue<M, T> operator/(
const MeasureValue<M, T>& a, T s)
noexcept {
174 return MeasureValue<M, T>{
static_cast<T
>(a.v / s)};
177template <MeasureLike M, NumericValue T>
178[[nodiscard]]
constexpr T operator/(
const MeasureValue<M, T>& a,
179 const MeasureValue<M, T>& b)
noexcept {
180 return static_cast<T
>(a.v / b.v);
185template <MeasureLike M, NumericValue T>
186[[nodiscard]]
constexpr MeasureValue<M, T>
192template <MeasureLike M, NumericValue T =
double>
Polymorphic interface implemented by every MeasureValue<M,T>.
Definition interface.hpp:59
Common concepts, error types, and small utilities used across dimval.
Abstract interfaces for runtime polymorphic handling of dimensional values.
constexpr MeasureValue< M, T > from_unit_value(const UnitValue< typename M::base_unit_t, T > &u) noexcept
Wrap a UnitValue<base_unit_of<M>> as a MeasureValue<M>.
Definition measure.hpp:187
constexpr MeasureValue< M, T > measure_value(T v) noexcept
Sugar: produce a MeasureValue from a scalar literal.
Definition measure.hpp:193
Runtime metadata for a measure (a semantic specialization of a unit).
Definition descriptor.hpp:54
A value carrying both a measure tag and a unit tag.
Definition measure.hpp:31
static std::unique_ptr< MeasureValue > unique(T value)
Heap-allocated unique-owned factory.
Definition measure.hpp:51
static std::expected< MeasureValue, ParseError > parse(std::string_view input)
Parse "100 m" → MeasureValue<M,T>. Forwards to UnitValue<base_unit_t,T>::parse.
Definition measure.hpp:56
constexpr MeasureValue(T value) noexcept
Implicit conversion from the numeric type — enables DistanceValue d = 1500.0; style construction.
Definition measure.hpp:42
static std::shared_ptr< MeasureValue > of(T value)
Heap-allocated shared-owned factory: auto d = DistanceValue::of(1500.0); For a stack value,...
Definition measure.hpp:46
UnitDescriptor unit_descriptor() const noexcept override
Underlying unit's descriptor — read unit_descriptor().id, .symbol, etc.
Definition measure.hpp:99
MeasureDescriptor descriptor() const noexcept override
Measure descriptor — read fields like descriptor().name from here.
Definition measure.hpp:96
constexpr UnitValue< unit_t, T > as_unit_value() const noexcept
Drop the measure tag and expose the underlying UnitValue.
Definition measure.hpp:88
Runtime metadata for a unit.
Definition descriptor.hpp:29
A value paired at the type level with a unit tag.
Definition unit.hpp:36
static std::expected< UnitValue, ParseError > parse(const std::string_view input)
Parse "42.5 m" → UnitValue<U,T>.
Definition unit.hpp:62
Concepts and helper accessors that drive the static side of dimval.
UnitValue<U,T>: a strongly-typed value carrying a compile-time unit tag.