parcel 0.2.4
Wrappable, wire-transferable C++23 value system with JSON serialization
Loading...
Searching...
No Matches
lifecycle.h
Go to the documentation of this file.
1#pragma once
2
45#include <parcel/cell.h>
46#include <parcel/common.h>
47#include <parcel/defaults.h>
48#include <parcel/descriptor.h>
49#include <parcel/json.h>
50
51#include <commons/json/lifecycle.hpp>
52#include <commons/lifecycle.hpp>
53
54#include <compare>
55#include <string>
56#include <string_view>
57#include <vector>
58
59namespace parcel {
60
68class LifecycleStatusCell : public BaseCell<LifecycleStatusCell, comms::LifecycleStatus> {
70
71public:
72 using base_t::base_t;
73 using base_t::operator=;
74
75 static constexpr std::string_view kind_id = "lifecycle_status";
76
77 [[nodiscard]] std::string to_string() const override {
78 return this->value.name();
79 }
80
81 static cell_t from_json(json_t const& j, ParcelRegistry const&) {
83 }
84
85 static cell_type_descriptor_t descriptor() {
86 static const auto d = std::make_shared<SimpleCellTypeDescriptor<LifecycleStatusCell>>(
87 DisplayInfo{.name = "LifecycleStatus",
88 .description = "Lifecycle status (string name on the wire)"});
89 return d;
90 }
91};
92
101template <CellLike P = LifecycleStatusCell>
103 : public BaseCell<StatusTransitionCell<P>, comms::StatusTransition<typename P::storage_t>> {
105
106 [[nodiscard]] static std::string render(const typename P::storage_t& s) {
107 return P{s}.to_string();
108 }
109
110public:
111 using element_type = P;
112
113 using base_t::base_t;
114 using base_t::operator=;
115
116 static constexpr std::string_view kind_id = prefixed_id_v<"status_transition:", P::kind_id>;
117
118 [[nodiscard]] std::string to_string() const override {
119 return this->value.previous
120 ? render(*this->value.previous) + " \xe2\x86\x92 " + render(this->value.status)
121 : render(this->value.status);
122 }
123
124 static cell_t from_json(json_t const& j, ParcelRegistry const&) {
125 return base_t::from_json_strict(j);
126 }
127
128 static cell_type_descriptor_t descriptor() {
129 static const auto d = std::make_shared<SimpleCellTypeDescriptor<StatusTransitionCell>>(
130 DisplayInfo{.name = "StatusTransition",
131 .description = std::string("Lifecycle transition event (status of ") +
132 std::string(P::kind_id) + ")"});
133 return d;
134 }
135};
136
144template <CellLike P = LifecycleStatusCell>
146 : public BaseCell<StatusReportCell<P>, comms::StatusReport<typename P::storage_t>> {
148
149public:
150 using element_type = P;
151
152 using base_t::base_t;
153 using base_t::operator=;
154
155 static constexpr std::string_view kind_id = prefixed_id_v<"status_report:", P::kind_id>;
156
157 [[nodiscard]] std::string to_string() const override {
158 return P{this->value.status}.to_string();
159 }
160
161 static cell_t from_json(json_t const& j, ParcelRegistry const&) {
162 return base_t::from_json_strict(j);
163 }
164
165 static cell_type_descriptor_t descriptor() {
166 static const auto d = std::make_shared<SimpleCellTypeDescriptor<StatusReportCell>>(
167 DisplayInfo{.name = "StatusReport",
168 .description = std::string("Per-status period summary (status of ") +
169 std::string(P::kind_id) + ")"});
170 return d;
171 }
172};
173
187template <CellLike P = LifecycleStatusCell>
189 : public BaseCell<StatusTransitionTimelineCell<P>,
190 std::vector<comms::StatusTransition<typename P::storage_t>>> {
192 std::vector<comms::StatusTransition<typename P::storage_t>>>;
193
194public:
195 using element_type = P;
197 using status_t = typename P::storage_t;
199 using transition_t = comms::StatusTransition<status_t>;
201 using timeline_t = comms::StatusTransitionTimeline<status_t>;
202
203 using base_t::base_t;
204 using base_t::operator=;
205
207 explicit StatusTransitionTimelineCell(const timeline_t& tl) : base_t(tl.transitions()) {}
208
209 static constexpr std::string_view kind_id =
210 prefixed_id_v<"status_transition_timeline:", P::kind_id>;
211
218 void load_into(timeline_t& tl) const {
219 tl.load_transitions(this->value);
220 }
221
222 [[nodiscard]] std::string to_string() const override {
223 return "[" + std::to_string(this->value.size()) + " status transitions]";
224 }
225
226 // std::vector storage opts out of BaseCell's default compare (see list.h), so
227 // compare element-wise here — StatusTransition is equality-comparable only.
228 [[nodiscard]] std::partial_ordering compare(ICell const& other) const override {
229 if (const auto k = this->kind() <=> other.kind(); k != 0) {
230 return k;
231 }
232 const auto* o = dynamic_cast<StatusTransitionTimelineCell const*>(&other);
233 if (o == nullptr) {
234 return std::partial_ordering::unordered;
235 }
236 return this->value == o->value ? std::partial_ordering::equivalent
237 : std::partial_ordering::unordered;
238 }
239
240 static cell_t from_json(json_t const& j, ParcelRegistry const&) {
241 return base_t::from_json_strict(j);
242 }
243
244 static cell_type_descriptor_t descriptor() {
245 static const auto d =
246 std::make_shared<SimpleCellTypeDescriptor<StatusTransitionTimelineCell>>(
247 DisplayInfo{.name = "StatusTransitionTimeline",
248 .description = std::string("Lifecycle transition history (status of ") +
249 std::string(P::kind_id) + ")"});
250 return d;
251 }
252};
253
254} // namespace parcel
255
Core ICell interface, cell_t handle, BaseCell CRTP base, and CellLike concept.
std::shared_ptr< ICell > cell_t
Shared handle to any ICell-derived value — the canonical cell pointer.
Definition cell.h:68
Cell wrapping comms::LifecycleStatus.
Definition lifecycle.h:68
std::string to_string() const override
Render the cell's value as a compact human-readable string.
Definition lifecycle.h:77
Runtime catalog of cell-type descriptors, keyed by wire kind id.
Definition registry.h:115
Cell wrapping comms::StatusReport<typename P::storage_t>, a per-status period summary.
Definition lifecycle.h:146
std::string to_string() const override
Render the cell's value as a compact human-readable string.
Definition lifecycle.h:157
Cell wrapping comms::StatusTransition<typename P::storage_t>, a single transition event.
Definition lifecycle.h:103
std::string to_string() const override
Render the cell's value as a compact human-readable string.
Definition lifecycle.h:118
Cell carrying the serializable history of a comms::StatusTransitionTimeline<typename P::storage_t>.
Definition lifecycle.h:190
void load_into(timeline_t &tl) const
Restore the stored history into a live timeline via load_transitions (no replay — subscribers are not...
Definition lifecycle.h:218
typename P::storage_t status_t
The status type carried by each transition.
Definition lifecycle.h:197
comms::StatusTransition< status_t > transition_t
The element type of the stored history.
Definition lifecycle.h:199
std::partial_ordering compare(ICell const &other) const override
Default three-way comparison: kind first, then storage value.
Definition lifecycle.h:228
StatusTransitionTimelineCell(const timeline_t &tl)
Snapshot a live timeline's transition history into the cell.
Definition lifecycle.h:207
comms::StatusTransitionTimeline< status_t > timeline_t
The live timeline type this cell projects.
Definition lifecycle.h:201
std::string to_string() const override
Render the cell's value as a compact human-readable string.
Definition lifecycle.h:222
The shared vocabulary aliases re-exported from commons and the compile-time kind-id machinery.
constexpr std::string_view prefixed_id_v
Convenience alias yielding the joined std::string_view.
Definition common.h:175
The default_cell_for<T> trait that drives FieldsBuilder field-type inference.
#define PARCEL_DEFAULT_CELL(CellT)
Register a cell type as the default wrapper for its payload type.
Definition defaults.h:269
Runtime cell-type descriptors and the schema-graph mix-ins (IHasFields, ISubTypes).
nlohmann::json typedef shared across cell types.
nlohmann::json json_t
Project-wide alias for nlohmann::json.
Definition json.h:19
CRTP base providing default to_json / clone / kind plumbing on top of a storage type.
Definition cell.h:343
static cell_t from_json_strict(json_t const &j)
Strict shorthand for the recurring cell_from_json + make_shared.
Definition cell.h:571
std::string_view kind() const override
Wire-stable kind identifier for this cell.
Definition cell.h:411
comms::LifecycleStatus value
Held value of the cell.
Definition cell.h:348
Polymorphic root of every parcel cell.
Definition cell.h:84
virtual std::string to_string() const =0
Render the cell's value as a compact human-readable string.
virtual std::string_view kind() const =0
Wire-stable kind identifier for this cell.