metadata 0.2.0
Modern C++23 header-only metadata container (JSON-like)
Loading...
Searching...
No Matches
parcel.hpp
Go to the documentation of this file.
1#pragma once
2
18
19#include <commons/color.hpp>
20#include <commons/icons.hpp>
21#include <md/json.hpp> // brings ADL to_json/from_json for md::Value/Object
22#include <md/object.hpp>
23#include <md/ostream.hpp> // operator<< for to_string()
24#include <md/value.hpp>
25
26#include <memory>
27#include <sstream>
28#include <string>
29#include <string_view>
30
31#include <parcel/parcel.h>
32
33namespace md {
34
39class ValueCell : public parcel::BaseCell<ValueCell, Value> {
40 using base_t = parcel::BaseCell<ValueCell, Value>;
41
42public:
46 using base_t::base_t;
49 using base_t::operator=;
50
52 static constexpr std::string_view kind_id = "md:v";
53
56 [[nodiscard]] std::string to_string() const override {
57 std::ostringstream os;
58 os << this->value;
59 return os.str();
60 }
61
67 [[maybe_unused]] static parcel::cell_t from_json(parcel::json_t const& j,
68 parcel::ParcelRegistry const&) {
69 return base_t::from_json_strict(j);
70 }
71
74 static parcel::cell_type_descriptor_t descriptor() {
75 static const auto d =
76 std::make_shared<parcel::SimpleCellTypeDescriptor<ValueCell>>(parcel::DisplayInfo{
77 .name = "md::Value",
78 .description = "Dynamic JSON-shaped value from the metadata library.",
79 .icon = comms::Icons::mdi::variable,
80 .color = comms::Colors::mui::deep_purple[500],
81 });
82 return d;
83 }
84};
85
90class ObjectCell : public parcel::BaseCell<ObjectCell, Object> {
91 using base_t = parcel::BaseCell<ObjectCell, Object>;
92
93public:
95 using base_t::base_t;
97 using base_t::operator=;
98
103 ObjectCell(std::initializer_list<Object::value_type> il) : base_t(Object(il)) {}
104
106 static constexpr std::string_view kind_id = "md:o";
107
110 [[nodiscard]] std::string to_string() const override {
111 std::ostringstream os;
112 os << this->value;
113 return os.str();
114 }
115
120 [[maybe_unused]] static parcel::cell_t from_json(parcel::json_t const& j,
121 parcel::ParcelRegistry const&) {
122 return base_t::from_json_strict(j);
123 }
124
126 static parcel::cell_type_descriptor_t descriptor() {
127 static const auto d =
128 std::make_shared<parcel::SimpleCellTypeDescriptor<ObjectCell>>(parcel::DisplayInfo{
129 .name = "md::Object",
130 .description = "Unordered string-keyed map of md::Value (md::Object/Metadata).",
131 .icon = comms::Icons::mdi::code_braces,
132 .color = comms::Colors::mui::amber[700],
133 });
134 return d;
135 }
136};
137
142class ArrayCell : public parcel::BaseCell<ArrayCell, Array> {
143 using base_t = parcel::BaseCell<ArrayCell, Array>;
144
145public:
147 using base_t::base_t;
149 using base_t::operator=;
150
156 template <class T0, class T1, class... Rest>
157 requires(std::constructible_from<Value, T0 &&> && std::constructible_from<Value, T1 &&> &&
158 (std::constructible_from<Value, Rest &&> && ...))
159 ArrayCell(T0&& a, T1&& b, Rest&&... rest)
160 : base_t(Array{Value(std::forward<T0>(a)),
161 Value(std::forward<T1>(b)),
162 Value(std::forward<Rest>(rest))...}) {}
163
165 static constexpr std::string_view kind_id = "md:a";
166
169 [[nodiscard]] std::string to_string() const override {
170 std::ostringstream os;
171 os << this->value;
172 return os.str();
173 }
174
179 [[maybe_unused]] static parcel::cell_t from_json(parcel::json_t const& j,
180 parcel::ParcelRegistry const&) {
181 return base_t::from_json_strict(j);
182 }
183
185 static parcel::cell_type_descriptor_t descriptor() {
186 static const auto d =
187 std::make_shared<parcel::SimpleCellTypeDescriptor<ArrayCell>>(parcel::DisplayInfo{
188 .name = "md::Array",
189 .description = "Heterogeneous vector of md::Value (md::Array).",
190 .icon = comms::Icons::mdi::code_brackets,
191 .color = comms::Colors::mui::green[600],
192 });
193 return d;
194 }
195};
196
203inline void register_cells(parcel::ParcelRegistry& registry) {
204 registry.register_kind(ValueCell::descriptor());
205 registry.register_kind(ObjectCell::descriptor());
206 registry.register_kind(ArrayCell::descriptor());
207}
208
209} // namespace md
210
211// FieldsBuilder/parcel::cell(...) inference hooks. Each macro expands to a
212// `parcel::default_cell_for<Storage>` specialization at namespace scope.
213PARCEL_DEFAULT_CELL(md::ValueCell);
214PARCEL_DEFAULT_CELL(md::ObjectCell);
215PARCEL_DEFAULT_CELL(md::ArrayCell);
216
217#define METADATA_HAS_PARCEL 1
Parcel cell wrapping an md::Array (std::vector<md::Value>).
Definition parcel.hpp:142
std::string to_string() const override
Compact textual rendering of the wrapped md::Array, identical to streaming via operator<<.
Definition parcel.hpp:169
static constexpr std::string_view kind_id
Wire-stable kind id reported under the "k" slot.
Definition parcel.hpp:165
ArrayCell(T0 &&a, T1 &&b, Rest &&... rest)
Brace-init passthrough so md::ArrayCell{"a", "b", "c"} mirrors md::Array{...}.
Definition parcel.hpp:159
static parcel::cell_type_descriptor_t descriptor()
Registry descriptor for ArrayCell.
Definition parcel.hpp:185
static parcel::cell_t from_json(parcel::json_t const &j, parcel::ParcelRegistry const &)
Parcel deserialization entry point.
Definition parcel.hpp:179
Parcel cell wrapping an md::Object (a.k.a.
Definition parcel.hpp:90
std::string to_string() const override
Compact textual rendering of the wrapped md::Object, identical to streaming via operator<<.
Definition parcel.hpp:110
static constexpr std::string_view kind_id
Wire-stable kind id reported under the "k" slot.
Definition parcel.hpp:106
static parcel::cell_t from_json(parcel::json_t const &j, parcel::ParcelRegistry const &)
Parcel deserialization entry point.
Definition parcel.hpp:120
ObjectCell(std::initializer_list< Object::value_type > il)
Brace-init passthrough so md::ObjectCell{{"k", 1}, {"k2", "v"}} mirrors md::Object{....
Definition parcel.hpp:103
static parcel::cell_type_descriptor_t descriptor()
Registry descriptor for ObjectCell.
Definition parcel.hpp:126
Ordered-by-insertion-time-ish string-keyed map of Values, with transparent string-view lookup and JSO...
Definition object.hpp:21
Parcel cell wrapping an md::Value.
Definition parcel.hpp:39
static parcel::cell_t from_json(parcel::json_t const &j, parcel::ParcelRegistry const &)
Parcel deserialization entry point.
Definition parcel.hpp:67
std::string to_string() const override
Compact textual rendering of the wrapped md::Value, identical to streaming via operator<< (escaped JS...
Definition parcel.hpp:56
static parcel::cell_type_descriptor_t descriptor()
Registry descriptor for ValueCell.
Definition parcel.hpp:74
static constexpr std::string_view kind_id
Wire-stable kind id reported under the "k" slot.
Definition parcel.hpp:52
Discriminated union holding one of the JSON-like alternatives (null, bool, signed/unsigned integer,...
Definition value.hpp:67
void register_cells(parcel::ParcelRegistry &registry)
Register all three md cells (md:v, md:o, md:a) on registry.
Definition parcel.hpp:203