parcel 0.2.4
Wrappable, wire-transferable C++23 value system with JSON serialization
Loading...
Searching...
No Matches
metadata.h
Go to the documentation of this file.
1#pragma once
2
16#include <parcel/cell.h>
17#include <parcel/defaults.h>
18#include <parcel/descriptor.h>
19#include <parcel/json.h>
20
21#include <commons/color.hpp>
22#include <commons/icons.hpp>
23#include <commons/json/metadata.hpp> // brings ADL to_json/from_json for comms::md types
24#include <commons/metadata.hpp> // types + operator<< for to_string()
25
26#include <sstream>
27#include <string>
28#include <string_view>
29#include <utility>
30
31namespace parcel {
32
39class ValueCell : public BaseCell<ValueCell, comms::md::Value> {
41
42public:
43 using base_t::base_t;
44 using base_t::operator=;
45
46 static constexpr std::string_view kind_id = "md:v";
47
48 [[nodiscard]] std::string to_string() const override {
49 std::ostringstream os;
50 os << this->value;
51 return os.str();
52 }
53
54 static cell_t from_json(json_t const& j, ParcelRegistry const&) {
56 }
57
58 static cell_type_descriptor_t descriptor() {
59 static const auto d = std::make_shared<SimpleCellTypeDescriptor<ValueCell>>(DisplayInfo{
60 .name = "md::Value",
61 .description = "Dynamic JSON-shaped value from the commons metadata tree.",
62 .icon = comms::Icons::mdi::variable,
63 .color = comms::Colors::mui::deep_purple[500],
64 });
65 return d;
66 }
67};
68
75class ObjectCell : public BaseCell<ObjectCell, comms::md::Object> {
77
78public:
79 using base_t::base_t;
80 using base_t::operator=;
81
86 ObjectCell(const std::initializer_list<comms::md::Object::value_type> il)
87 : base_t(comms::md::Object(il)) {}
88
89 static constexpr std::string_view kind_id = "md:o";
90
91 [[nodiscard]] std::string to_string() const override {
92 std::ostringstream os;
93 os << this->value;
94 return os.str();
95 }
96
97 static cell_t from_json(json_t const& j, ParcelRegistry const&) {
99 }
100
101 static cell_type_descriptor_t descriptor() {
102 static const auto d = std::make_shared<SimpleCellTypeDescriptor<ObjectCell>>(DisplayInfo{
103 .name = "md::Object",
104 .description = "Unordered string-keyed map of md::Value (md::Object/Metadata).",
105 .icon = comms::Icons::mdi::code_braces,
106 .color = comms::Colors::mui::amber[700],
107 });
108 return d;
109 }
110};
111
118class ArrayCell : public BaseCell<ArrayCell, comms::md::Array> {
120
121public:
122 using base_t::base_t;
123 using base_t::operator=;
124
132 template <class T0, class T1, class... Rest>
133 requires(std::constructible_from<comms::md::Value, T0 &&> &&
134 std::constructible_from<comms::md::Value, T1 &&> &&
135 (std::constructible_from<comms::md::Value, Rest &&> && ...))
136 ArrayCell(T0&& a, T1&& b, Rest&&... rest)
137 : base_t(comms::md::Array{comms::md::Value(std::forward<T0>(a)),
138 comms::md::Value(std::forward<T1>(b)),
139 comms::md::Value(std::forward<Rest>(rest))...}) {}
140
141 static constexpr std::string_view kind_id = "md:a";
142
143 [[nodiscard]] std::string to_string() const override {
144 std::ostringstream os;
145 os << this->value;
146 return os.str();
147 }
148
149 static cell_t from_json(json_t const& j, ParcelRegistry const&) {
150 return base_t::from_json_strict(j);
151 }
152
153 static cell_type_descriptor_t descriptor() {
154 static const auto d = std::make_shared<SimpleCellTypeDescriptor<ArrayCell>>(DisplayInfo{
155 .name = "md::Array",
156 .description = "Heterogeneous vector of md::Value (md::Array).",
157 .icon = comms::Icons::mdi::code_brackets,
158 .color = comms::Colors::mui::green[600],
159 });
160 return d;
161 }
162};
163
164} // namespace parcel
165
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 a comms::md::Array (std::vector<comms::md::Value>).
Definition metadata.h:118
std::string to_string() const override
Render the cell's value as a compact human-readable string.
Definition metadata.h:143
ArrayCell(T0 &&a, T1 &&b, Rest &&... rest)
Brace-init passthrough so ArrayCell{"a", "b", "c"} mirrors comms::md::Array{...}.
Definition metadata.h:136
Cell wrapping a comms::md::Object (a.k.a.
Definition metadata.h:75
std::string to_string() const override
Render the cell's value as a compact human-readable string.
Definition metadata.h:91
ObjectCell(const std::initializer_list< comms::md::Object::value_type > il)
Brace-init passthrough so ObjectCell{{"k", 1}, {"k2", "v"}} mirrors comms::md::Object{....
Definition metadata.h:86
Runtime catalog of cell-type descriptors, keyed by wire kind id.
Definition registry.h:115
Cell wrapping a comms::md::Value.
Definition metadata.h:39
std::string to_string() const override
Render the cell's value as a compact human-readable string.
Definition metadata.h:48
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
comms::md::Value value
Held value of the cell.
Definition cell.h:348