dimval 0.2.0
Modern C++23 header-only library of dimensional values (units, measures, ranges)
Loading...
Searching...
No Matches
base.hpp
Go to the documentation of this file.
1#pragma once
2
8
10
11#include <commons/color.hpp>
12#include <commons/icon.hpp>
13
14#include <optional>
15#include <string_view>
16
17namespace dimval {
18
20struct UnitTag {};
21
23struct MeasureTag {};
24
29template <typename Self>
30struct UnitBase : UnitTag {
31 using self_t = Self;
32
33 static constexpr double offset = 0.0;
34 static constexpr std::string_view formatter = "default";
35 static constexpr int default_precision = -1;
36 static constexpr bool no_space_before_symbol = false;
37 static constexpr std::optional<comms::Icon> icon = std::nullopt;
38 static constexpr std::optional<comms::Color> color = std::nullopt;
39
40 [[nodiscard]] static constexpr UnitDescriptor descriptor() noexcept {
41 return UnitDescriptor{
42 .id = Self::id,
43 .symbol = Self::symbol,
44 .short_name = Self::short_name,
45 .long_name = Self::long_name,
46 .kind = Self::kind,
47 .factor = Self::factor,
48 .offset = Self::offset,
49 .formatter = Self::formatter,
50 .default_precision = Self::default_precision,
51 .no_space_before_symbol = Self::no_space_before_symbol,
52 .icon = Self::icon,
53 .color = Self::color,
54 };
55 }
56};
57
62template <typename Self, typename BaseUnit>
64 using self_t = Self;
65 using base_unit_t = BaseUnit;
66
68 static constexpr std::string_view base_unit_id = BaseUnit::id;
69
70 static constexpr std::string_view formatter = {};
71 static constexpr int default_precision = -1;
72 static constexpr std::optional<comms::Icon> icon = std::nullopt;
73 static constexpr std::optional<comms::Color> color = std::nullopt;
74
75 [[nodiscard]] static constexpr MeasureDescriptor descriptor() noexcept {
76 return MeasureDescriptor{
77 .id = Self::id,
78 .base_unit_id = BaseUnit::id,
79 .name = Self::name,
80 .formatter = Self::formatter,
81 .default_precision = Self::default_precision,
82 .icon = Self::icon,
83 .color = Self::color,
84 };
85 }
86};
87
88} // namespace dimval
Runtime metadata structs for units and measures.
CRTP base for measure tags.
Definition base.hpp:63
static constexpr std::string_view base_unit_id
Derived from the unit type — the redundant call-site string is gone.
Definition base.hpp:68
Runtime metadata for a measure (a semantic specialization of a unit).
Definition descriptor.hpp:54
std::string_view id
Stable identifier (e.g. "distance"). Unique per registry.
Definition descriptor.hpp:55
Marker base — concept detection picks up any type that derives from this.
Definition base.hpp:23
CRTP base for unit tags.
Definition base.hpp:30
Runtime metadata for a unit.
Definition descriptor.hpp:29
std::string_view id
Stable identifier (e.g. "m"). Unique per registry.
Definition descriptor.hpp:30
Marker base — concept detection picks up any type that derives from this.
Definition base.hpp:20