dimval 0.2.0
Modern C++23 header-only library of dimensional values (units, measures, ranges)
Loading...
Searching...
No Matches
core.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <cstddef>
7#include <cstdint>
8#include <string>
9#include <string_view>
10#include <type_traits>
11
12namespace dimval {
13
16template <typename T>
17concept NumericValue = std::is_arithmetic_v<T>;
18
28
30struct ParseError {
31 ParseErrorCode code;
32 std::string input;
33 std::size_t pos{0};
34 std::string message;
35};
36
42
44struct RangeError {
45 RangeErrorCode code;
46 std::string message;
47};
48
50enum class Bound : std::uint8_t {
51 Inclusive,
52 Exclusive,
53};
54
57 Bound lower{Bound::Inclusive};
58 Bound upper{Bound::Inclusive};
59
60 [[nodiscard]] static constexpr RangeInclusion closed() noexcept {
61 return {Bound::Inclusive, Bound::Inclusive};
62 }
63 [[nodiscard]] static constexpr RangeInclusion open() noexcept {
64 return {Bound::Exclusive, Bound::Exclusive};
65 }
66 [[nodiscard]] static constexpr RangeInclusion left_open() noexcept {
67 return {Bound::Exclusive, Bound::Inclusive};
68 }
69 [[nodiscard]] static constexpr RangeInclusion right_open() noexcept {
70 return {Bound::Inclusive, Bound::Exclusive};
71 }
72
73 [[nodiscard]] friend constexpr bool operator==(RangeInclusion,
74 RangeInclusion) noexcept = default;
75};
76
78enum class RangeCompareResult : std::int8_t {
79 Less = -1,
80 Equal = 0,
81 Greater = 1,
82 Overlapping = 2,
83};
84
85namespace detail {
86
88[[nodiscard]] constexpr std::string_view ltrim(std::string_view s) noexcept {
89 while (!s.empty() && (s.front() == ' ' || s.front() == '\t')) {
90 s.remove_prefix(1);
91 }
92 return s;
93}
94
95[[nodiscard]] constexpr std::string_view rtrim(std::string_view s) noexcept {
96 while (!s.empty() && (s.back() == ' ' || s.back() == '\t')) {
97 s.remove_suffix(1);
98 }
99 return s;
100}
101
102[[nodiscard]] constexpr std::string_view trim(const std::string_view s) noexcept {
103 return rtrim(ltrim(s));
104}
105
106} // namespace detail
107
108} // namespace dimval
Concept for value types stored inside a UnitValue / MeasureValue.
Definition core.hpp:17
constexpr std::string_view ltrim(std::string_view s) noexcept
Return a span between the first matching token and the trailing decimal/sign characters.
Definition core.hpp:88
RangeCompareResult
Result of comparing two values or ranges.
Definition core.hpp:78
ParseErrorCode
Error codes returned by parse_unit_value / parse_measure_value.
Definition core.hpp:20
@ UnitMismatch
Symbol parsed but doesn't match the requested unit tag.
@ UnknownUnit
Symbol/id not present in the registry.
@ InvalidNumber
Numeric portion failed to parse.
@ MeasureMismatch
Measure id doesn't match the requested measure tag.
@ Empty
Input was empty or whitespace-only.
@ TrailingGarbage
Unexpected characters after the value.
Bound
Inclusivity of a single range bound.
Definition core.hpp:50
RangeErrorCode
Error codes for range construction.
Definition core.hpp:38
@ MaxLessThanMin
Upper bound is strictly less than lower bound.
@ EmptyOpenRange
Equal bounds with at least one exclusive side.
Detailed error report from a parse_*_value call.
Definition core.hpp:30
std::string message
Optional human-readable detail.
Definition core.hpp:34
std::string input
Copy of the original input for diagnostics.
Definition core.hpp:32
std::size_t pos
Byte position where parsing failed.
Definition core.hpp:33
Detailed error report from a UnitRangeValue::make / MeasureRangeValue::make call.
Definition core.hpp:44
Inclusivity of both range bounds together.
Definition core.hpp:56