dimval 0.2.0
Modern C++23 header-only library of dimensional values (units, measures, ranges)
Loading...
Searching...
No Matches
parse.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <dimval/core.hpp>
7#include <dimval/measure.hpp>
9#include <dimval/registry.hpp>
10#include <dimval/traits.hpp>
11#include <dimval/unit.hpp>
12
13#include <expected>
14#include <string>
15#include <string_view>
16
17namespace dimval {
18
22 const UnitDescriptor* desc{};
23 double value{};
24};
25
28template <UnitLike U, NumericValue T = double>
29[[nodiscard]] std::expected<UnitValue<U, T>, ParseError> parse_unit_value(std::string_view input) {
30 return UnitValue<U, T>::parse(input);
31}
32
35template <MeasureLike M, NumericValue T = double>
36[[nodiscard]] std::expected<MeasureValue<M, T>, ParseError>
37parse_measure_value(std::string_view input) {
38 return MeasureValue<M, T>::parse(input);
39}
40
44[[nodiscard]] inline std::expected<DynamicUnitValue, ParseError>
45parse_dynamic_unit_value(std::string_view input) {
46 auto split = detail::split_number_and_tail(input);
47 if (!split) {
48 return std::unexpected{split.error()};
49 }
50 auto num = detail::parse_number<double>(split->number, input);
51 if (!num) {
52 return std::unexpected{num.error()};
53 }
54 if (split->tail.empty()) {
55 // Interpret as dimensionless.
56 const auto* d = UnitRegistry::global().find("dimensionless");
57 if (d == nullptr) {
58 return std::unexpected{
59 ParseError{ParseErrorCode::UnknownUnit,
60 std::string{input},
61 split->number_end,
62 "no symbol given and 'dimensionless' is not registered"}};
63 }
64 return DynamicUnitValue{d, *num};
65 }
66 const auto* d = UnitRegistry::global().find(split->tail);
67 if (d == nullptr) {
68 return std::unexpected{
69 ParseError{ParseErrorCode::UnknownUnit,
70 std::string{input},
71 split->number_end,
72 std::string{"unknown unit symbol: "} + std::string{split->tail}}};
73 }
74 return DynamicUnitValue{d, *num};
75}
76
77} // namespace dimval
const UnitDescriptor * find(const std::string_view id_or_symbol) const
Look up by id, then by symbol. Returns nullptr if not found.
Definition registry.hpp:62
static UnitRegistry & global()
Process-wide singleton.
Definition registry.hpp:36
Common concepts, error types, and small utilities used across dimval.
MeasureValue<M,T>: a UnitValue tagged with an additional semantic measure.
std::expected< DynamicUnitValue, ParseError > parse_dynamic_unit_value(std::string_view input)
Parse a string of the form "42.5 m" against the runtime registry.
Definition parse.hpp:45
std::expected< MeasureValue< M, T >, ParseError > parse_measure_value(std::string_view input)
Parse a string of the form "42.5 m" into a MeasureValue<M,T>.
Definition parse.hpp:37
std::expected< UnitValue< U, T >, ParseError > parse_unit_value(std::string_view input)
Parse a string of the form "42.5 m" into a UnitValue<U,T>.
Definition parse.hpp:29
Low-level parsing primitives shared by parse.hpp and the inline UnitValue::parse / MeasureValue::pars...
Thread-safe runtime registries of unit and measure descriptors.
Type-erased dynamic parse result.
Definition parse.hpp:21
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
Detailed error report from a parse_*_value call.
Definition core.hpp:30
Runtime metadata for a unit.
Definition descriptor.hpp:29
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.