parcel 0.2.4
Wrappable, wire-transferable C++23 value system with JSON serialization
Loading...
Searching...
No Matches
origin.h
Go to the documentation of this file.
1#pragma once
2
8#include <parcel/cell.h>
9#include <parcel/defaults.h>
10#include <parcel/descriptor.h>
11#include <parcel/json.h>
12
13#include <commons/json/origin.hpp>
14#include <commons/origin.hpp>
15
16#include <compare>
17#include <memory>
18#include <optional>
19#include <string>
20#include <string_view>
21#include <utility>
22
23namespace parcel {
24
35class OriginCell : public ICell {
36public:
37 using storage_t = comms::OriginPtr;
38
39 static constexpr std::string_view kind_id = "origin";
40
42 comms::OriginPtr value;
43
44 OriginCell() = default;
45 explicit OriginCell(comms::OriginPtr v) : value(std::move(v)) {}
46
48 static cell_t of(comms::OriginPtr v) {
49 return std::make_shared<OriginCell>(std::move(v));
50 }
51
52 [[nodiscard]] std::string_view kind() const override {
53 return kind_id;
54 }
55
56 [[nodiscard]] std::optional<DisplayInfo> const& overridden_display_info() const override {
57 return display_info_;
58 }
59
60 [[nodiscard]] std::string to_string() const override {
61 return value ? std::string{value->kind()} : std::string{"null"};
62 }
63
64 [[nodiscard]] json_t to_json() const override {
65 json_t j{{KEY_KIND, kind_id}, {KEY_VALUE, value}};
66 if (display_info_) {
67 j[KEY_DESCRIPTION] = *display_info_;
68 }
69 return j;
70 }
71
72 [[nodiscard]] cell_t clone() const override {
73 auto c = std::make_shared<OriginCell>(value ? value->clone() : comms::OriginPtr{});
74 c->display_info_ = display_info_;
75 return c;
76 }
77
78 [[nodiscard]] std::partial_ordering compare(ICell const& other) const override {
79 if (const auto k_cmp = this->kind() <=> other.kind(); k_cmp != 0) {
80 return k_cmp;
81 }
82 const auto* o = dynamic_cast<OriginCell const*>(&other);
83 if (o == nullptr) {
84 return std::partial_ordering::unordered;
85 }
86 return json_t(value) == json_t(o->value) ? std::partial_ordering::equivalent
87 : std::partial_ordering::unordered;
88 }
89
90 static cell_t from_json(json_t const& j, ParcelRegistry const&) {
91 // Mirror BaseCell::cell_from_json's {"k","v"} validation; OriginPtr is
92 // move-only so we cannot route through the BaseCell helper.
93 if (!j.is_object()) {
94 throw InvalidJsonException("Expected JSON object for ICell", std::string(kind_id));
95 }
96 const auto it_kind = j.find(KEY_KIND);
97 if (it_kind == j.end() || !it_kind->is_string()) {
98 throw InvalidJsonException("Missing or invalid 'k' field in ICell JSON",
99 std::string(kind_id));
100 }
101 if (const auto k = it_kind->get<std::string_view>(); k != kind_id) {
102 throw KindMismatchException("Unexpected kind in ICell JSON: expected '" +
103 std::string(kind_id) + "', got '" + std::string(k) +
104 "'",
105 std::string(kind_id));
106 }
107 const auto it_value = j.find(KEY_VALUE);
108 if (it_value == j.end()) {
109 throw InvalidJsonException("Missing 'v' field in ICell JSON", std::string(kind_id));
110 }
111 // Resolves "kind" against comms::GlobalOriginRegistry; unknown kind throws.
112 auto cell = std::make_shared<OriginCell>(it_value->get<comms::OriginPtr>());
113 if (const auto it_d = j.find(KEY_DESCRIPTION); it_d != j.end()) {
114 cell->set_display_info(it_d->get<DisplayInfo>());
115 }
116 return cell;
117 }
118
119 static cell_type_descriptor_t descriptor() {
120 static const auto d = std::make_shared<SimpleCellTypeDescriptor<OriginCell>>(DisplayInfo{
121 .name = "Origin",
122 .description = "Polymorphic provenance envelope ({\"kind\", …fields} on the wire)"});
123 return d;
124 }
125
126protected:
127 void set_display_info(std::optional<DisplayInfo> m) override {
128 display_info_ = std::move(m);
129 }
130
131private:
132 std::optional<DisplayInfo> display_info_;
133};
134
135} // namespace parcel
136
Core ICell interface, cell_t handle, BaseCell CRTP base, and CellLike concept.
std::shared_ptr< ICellTypeDescriptor > cell_type_descriptor_t
Shared handle to a runtime cell-type descriptor.
Definition cell.h:63
std::shared_ptr< ICell > cell_t
Shared handle to any ICell-derived value — the canonical cell pointer.
Definition cell.h:68
JSON shape was wrong (missing/non-string k, missing v, etc.).
Definition error.h:158
Cell wrapping comms::OriginPtr (a std::unique_ptr<comms::IOrigin>).
Definition origin.h:35
static cell_t of(comms::OriginPtr v)
Construct a shared_ptr<OriginCell> owning v.
Definition origin.h:48
std::optional< DisplayInfo > const & overridden_display_info() const override
Read-only access to this cell's optional display info.
Definition origin.h:56
json_t to_json() const override
Serialize this cell to its canonical JSON representation.
Definition origin.h:64
comms::OriginPtr value
Held origin (may be null).
Definition origin.h:42
std::string to_string() const override
Render the cell's value as a compact human-readable string.
Definition origin.h:60
void set_display_info(std::optional< DisplayInfo > m) override
Replace this cell's display info in place.
Definition origin.h:127
std::string_view kind() const override
Wire-stable kind identifier for this cell.
Definition origin.h:52
std::partial_ordering compare(ICell const &other) const override
Three-way compare against another cell.
Definition origin.h:78
cell_t clone() const override
Deep-copy this cell.
Definition origin.h:72
Runtime catalog of cell-type descriptors, keyed by wire kind id.
Definition registry.h:115
comms::DisplayInfo DisplayInfo
Display info attached to a cell or descriptor — re-exported from comms::DisplayInfo.
Definition common.h:75
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
auto cell(T &&v)
Wrap a raw value into its default cell, returning a shared_ptr to the cell.
Definition defaults.h:192
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
Polymorphic root of every parcel cell.
Definition cell.h:84
static constexpr std::string_view KEY_DESCRIPTION
JSON key for the optional display info block ("d").
Definition cell.h:90
static constexpr std::string_view KEY_VALUE
JSON key for the value payload ("v").
Definition cell.h:88
virtual std::string_view kind() const =0
Wire-stable kind identifier for this cell.
static constexpr std::string_view KEY_KIND
JSON key for the kind id ("k").
Definition cell.h:86