dimval 0.2.0
Modern C++23 header-only library of dimensional values (units, measures, ranges)
Loading...
Searching...
No Matches
parcel.hpp
Go to the documentation of this file.
1#pragma once
2
26
27#if !defined(DIMVAL_WITH_PARCEL)
28#if __has_include(<parcel/parcel.h>)
29#define DIMVAL_WITH_PARCEL 1
30#else
31#define DIMVAL_WITH_PARCEL 0
32#endif
33#endif
34
35#if DIMVAL_WITH_PARCEL
36
37#include <dimval/format.hpp>
39#include <dimval/measure.hpp>
40#include <dimval/range.hpp>
41#include <dimval/traits.hpp>
42#include <dimval/unit.hpp>
43
44#include <memory>
45#include <string>
46#include <string_view>
47
48#include <parcel/parcel.h>
49
50namespace dimval {
51
52template <UnitLike U, NumericValue T = double>
53class UnitValueCell : public ::parcel::BaseCell<UnitValueCell<U, T>, UnitValue<U, T>> {
54 using base_t = ::parcel::BaseCell<UnitValueCell<U, T>, UnitValue<U, T>>;
55
56public:
57 using base_t::base_t;
58 using base_t::operator=;
59
60 static constexpr std::string_view kind_id = "uv";
61
62 [[nodiscard]] std::string to_string() const override {
63 return this->value.to_string();
64 }
65
66 [[nodiscard]] std::string to_formatted_string() const override {
67 return this->value.to_formatted_string();
68 }
69
70 static ::parcel::cell_t from_json(::parcel::json_t const& j, ::parcel::ParcelRegistry const&) {
71 auto v = base_t::template cell_from_json<UnitValue<U, T>>(j, kind_id);
72 auto cell = std::make_shared<UnitValueCell>(v);
73 base_t::absorb_display_info(j, cell);
74 return cell;
75 }
76
77 static ::parcel::cell_type_descriptor_t descriptor() {
78 static const auto d = std::make_shared<::parcel::SimpleCellTypeDescriptor<UnitValueCell>>(
79 ::parcel::DisplayInfo{.name = "dimval::UnitValue"});
80 return d;
81 }
82};
83
84template <MeasureLike M, NumericValue T = double>
85class MeasureValueCell : public ::parcel::BaseCell<MeasureValueCell<M, T>, MeasureValue<M, T>> {
86 using base_t = ::parcel::BaseCell<MeasureValueCell<M, T>, MeasureValue<M, T>>;
87
88public:
89 using base_t::base_t;
90 using base_t::operator=;
91
92 static constexpr std::string_view kind_id = "mv";
93
94 [[nodiscard]] std::string to_string() const override {
95 return this->value.to_string();
96 }
97
98 [[nodiscard]] std::string to_formatted_string() const override {
99 return this->value.to_formatted_string();
100 }
101
102 static ::parcel::cell_t from_json(::parcel::json_t const& j, ::parcel::ParcelRegistry const&) {
103 auto v = base_t::template cell_from_json<MeasureValue<M, T>>(j, kind_id);
104 auto cell = std::make_shared<MeasureValueCell>(v);
105 base_t::absorb_display_info(j, cell);
106 return cell;
107 }
108
109 static ::parcel::cell_type_descriptor_t descriptor() {
110 static const auto d =
111 std::make_shared<::parcel::SimpleCellTypeDescriptor<MeasureValueCell>>(
112 ::parcel::DisplayInfo{.name = "dimval::MeasureValue"});
113 return d;
114 }
115};
116
117template <UnitLike U, NumericValue T = double>
118class UnitRangeValueCell
119 : public ::parcel::BaseCell<UnitRangeValueCell<U, T>, UnitRangeValue<U, T>> {
120 using base_t = ::parcel::BaseCell<UnitRangeValueCell<U, T>, UnitRangeValue<U, T>>;
121
122public:
123 using base_t::base_t;
124 using base_t::operator=;
125
126 static constexpr std::string_view kind_id = "urv";
127
128 [[nodiscard]] std::string to_string() const override {
129 return this->value.to_string();
130 }
131
132 [[nodiscard]] std::string to_formatted_string() const override {
133 return this->value.to_formatted_string();
134 }
135
136 static ::parcel::cell_t from_json(::parcel::json_t const& j, ::parcel::ParcelRegistry const&) {
137 auto v = base_t::template cell_from_json<UnitRangeValue<U, T>>(j, kind_id);
138 auto cell = std::make_shared<UnitRangeValueCell>(v);
139 base_t::absorb_display_info(j, cell);
140 return cell;
141 }
142
143 static ::parcel::cell_type_descriptor_t descriptor() {
144 static const auto d =
145 std::make_shared<::parcel::SimpleCellTypeDescriptor<UnitRangeValueCell>>(
146 ::parcel::DisplayInfo{.name = "dimval::UnitRangeValue"});
147 return d;
148 }
149};
150
151template <MeasureLike M, NumericValue T = double>
152class MeasureRangeValueCell
153 : public ::parcel::BaseCell<MeasureRangeValueCell<M, T>, MeasureRangeValue<M, T>> {
154 using base_t = ::parcel::BaseCell<MeasureRangeValueCell<M, T>, MeasureRangeValue<M, T>>;
155
156public:
157 using base_t::base_t;
158 using base_t::operator=;
159
160 static constexpr std::string_view kind_id = "mrv";
161
162 [[nodiscard]] std::string to_string() const override {
163 return this->value.to_string();
164 }
165
166 [[nodiscard]] std::string to_formatted_string() const override {
167 return this->value.to_formatted_string();
168 }
169
170 static ::parcel::cell_t from_json(::parcel::json_t const& j, ::parcel::ParcelRegistry const&) {
171 auto v = base_t::template cell_from_json<MeasureRangeValue<M, T>>(j, kind_id);
172 auto cell = std::make_shared<MeasureRangeValueCell>(v);
173 base_t::absorb_display_info(j, cell);
174 return cell;
175 }
176
177 static ::parcel::cell_type_descriptor_t descriptor() {
178 static const auto d =
179 std::make_shared<::parcel::SimpleCellTypeDescriptor<MeasureRangeValueCell>>(
180 ::parcel::DisplayInfo{.name = "dimval::MeasureRangeValue"});
181 return d;
182 }
183};
184
185} // namespace dimval
186
187#endif // DIMVAL_WITH_PARCEL
std::formatter specializations for UnitValue, MeasureValue, ranges, and descriptors.
Optional nlohmann::json integration.
MeasureValue<M,T>: a UnitValue tagged with an additional semantic measure.
UnitRangeValue and MeasureRangeValue — closed/open intervals of dimensional values.
Concepts and helper accessors that drive the static side of dimval.
UnitValue<U,T>: a strongly-typed value carrying a compile-time unit tag.