parcel 0.2.2
Wrappable, wire-transferable C++23 value system with JSON serialization
Loading...
Searching...
No Matches
descriptor.h
Go to the documentation of this file.
1#pragma once
2
14#include <parcel/cell.h>
15#include <parcel/common.h>
16
17#include <map>
18#include <memory>
19#include <string>
20#include <string_view>
21#include <typeindex>
22#include <typeinfo>
23#include <utility>
24#include <vector>
25
26namespace parcel::descriptor {
27
35enum class CellCategory { Primitive, TypedList, List, TypedMap, Map, Struct, Union, Custom };
36
37NLOHMANN_JSON_SERIALIZE_ENUM(CellCategory,
38 {
39 {CellCategory::Primitive, "primitive"},
40 {CellCategory::TypedList, "typed_list"},
41 {CellCategory::List, "list"},
42 {CellCategory::TypedMap, "typed_map"},
43 {CellCategory::Map, "map"},
44 {CellCategory::Struct, "struct"},
45 {CellCategory::Union, "union"},
46 {CellCategory::Custom, "custom"},
47 })
48
49} // namespace parcel::descriptor
50
51namespace parcel {
52class ParcelRegistry;
53
63 virtual ~ICellTypeDescriptor() = default;
64
66 [[nodiscard]] virtual std::string_view kind() const = 0;
67
69 [[nodiscard]] virtual DisplayInfo display_info() const = 0;
70
72 [[nodiscard]] virtual descriptor::CellCategory category() const = 0;
73
75 [[nodiscard]] virtual std::type_index storage_type() const = 0;
76
84 [[nodiscard]] virtual cell_t cell_from_json(const json_t& j,
85 ParcelRegistry const& reg) const = 0;
86
91 [[nodiscard]] virtual json_t to_json() const = 0;
92};
93
100 virtual ~IFieldDescriptor() = default;
101
103 [[nodiscard]] virtual std::string_view key() const = 0;
104
106 [[nodiscard]] virtual std::string_view kind() const = 0;
107
109 [[nodiscard]] virtual DisplayInfo display_info() const = 0;
110
112 [[nodiscard]] virtual bool is_required() const = 0;
113
118 [[nodiscard]] virtual json_t to_json() const = 0;
119};
120
122using field_descriptor_t = std::shared_ptr<IFieldDescriptor>;
123
125using field_descriptors_t = std::map<std::string, field_descriptor_t>;
126
134 virtual ~IHasFields() = default;
135
137 [[nodiscard]] virtual field_descriptors_t fields() const = 0;
138};
139
148struct ISubTypes {
149 virtual ~ISubTypes() = default;
150
152 [[nodiscard]] virtual std::vector<std::string_view> sub_kinds() const = 0;
153};
154
164template <CellLike T>
166public:
167 using cell_type = T;
168
170 static constexpr std::string_view kind_id = T::kind_id;
171
176 explicit BaseCellTypeDescriptor(DisplayInfo info) : display_info_(std::move(info)) {}
177
178 [[nodiscard]] std::string_view kind() const override {
179 return kind_id;
180 }
181
182 [[nodiscard]] DisplayInfo display_info() const override {
183 return display_info_;
184 }
185
186 [[nodiscard]] std::type_index storage_type() const override {
187 return std::type_index(typeid(typename T::storage_t));
188 }
189
190 [[nodiscard]] json_t to_json() const override {
191 return base_to_json();
192 }
193
194protected:
197
202 [[nodiscard]] json_t base_to_json() const {
203 return json_t{
204 {"kind", kind_id},
205 {"display_info", json_t(display_info_)},
206 {"category", this->category()},
207 };
208 }
209};
210
219template <CellLike T>
221public:
222 using cell_type = T;
223
229 : BaseCellTypeDescriptor<T>(std::move(info)) {}
230
231 [[nodiscard]] descriptor::CellCategory category() const override {
232 return descriptor::CellCategory::Primitive;
233 }
234
235 [[nodiscard]] cell_t cell_from_json(const json_t& j, const ParcelRegistry& reg) const override {
236 return T::from_json(j, reg);
237 }
238};
239
240} // namespace parcel
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
CRTP base that fills in the boilerplate of every cell-type descriptor.
Definition descriptor.h:165
json_t base_to_json() const
Common JSON skeleton (kind, display_info, category) reused by overrides.
Definition descriptor.h:202
BaseCellTypeDescriptor(DisplayInfo info)
Construct with the given display info.
Definition descriptor.h:176
static constexpr std::string_view kind_id
Wire-stable kind id, lifted from T::kind_id.
Definition descriptor.h:170
DisplayInfo display_info() const override
Display info for this cell type.
Definition descriptor.h:182
json_t to_json() const override
Serialize the descriptor itself (kind + display info + category + extras).
Definition descriptor.h:190
std::type_index storage_type() const override
std::type_index of the cell's storage type.
Definition descriptor.h:186
std::string_view kind() const override
Wire-stable kind identifier.
Definition descriptor.h:178
DisplayInfo display_info_
Stored display info.
Definition descriptor.h:196
Runtime catalog of cell-type descriptors, keyed by wire kind id.
Definition registry.h:115
Plain descriptor for cells in the Primitive category.
Definition descriptor.h:220
cell_t cell_from_json(const json_t &j, const ParcelRegistry &reg) const override
Construct a cell from JSON, dispatching to the concrete from_json.
Definition descriptor.h:235
descriptor::CellCategory category() const override
Coarse classification (primitive, list, struct, …).
Definition descriptor.h:231
SimpleCellTypeDescriptor(DisplayInfo info)
Construct with the given display info.
Definition descriptor.h:228
The shared vocabulary aliases re-exported from commons and the compile-time kind-id machinery.
comms::DisplayInfo DisplayInfo
Display info attached to a cell or descriptor — re-exported from comms::DisplayInfo.
Definition common.h:75
std::map< std::string, field_descriptor_t > field_descriptors_t
Ordered map of field key → descriptor.
Definition descriptor.h:125
CellCategory
Coarse runtime classification of a cell type.
Definition descriptor.h:35
std::shared_ptr< IFieldDescriptor > field_descriptor_t
Shared handle to a field descriptor.
Definition descriptor.h:122
nlohmann::json json_t
Project-wide alias for nlohmann::json.
Definition json.h:19
Runtime descriptor for a cell type — type-erased face of a CellLike.
Definition descriptor.h:62
virtual descriptor::CellCategory category() const =0
Coarse classification (primitive, list, struct, …).
virtual json_t to_json() const =0
Serialize the descriptor itself (kind + display info + category + extras).
virtual DisplayInfo display_info() const =0
Display info for this cell type.
virtual std::string_view kind() const =0
Wire-stable kind identifier.
virtual cell_t cell_from_json(const json_t &j, ParcelRegistry const &reg) const =0
Construct a cell from JSON, dispatching to the concrete from_json.
virtual std::type_index storage_type() const =0
std::type_index of the cell's storage type.
Runtime descriptor for a single struct field.
Definition descriptor.h:99
virtual json_t to_json() const =0
Serialize the field descriptor itself.
virtual std::string_view key() const =0
JSON key under which this field appears in "v".
virtual bool is_required() const =0
Whether the field must be present on deserialization.
virtual DisplayInfo display_info() const =0
Display info for this field.
virtual std::string_view kind() const =0
Cell kind id for the field's value type.
Mix-in declaring a descriptor exposes a static set of named fields.
Definition descriptor.h:133
virtual field_descriptors_t fields() const =0
Field descriptors keyed by JSON key.
Mix-in declaring a descriptor refers to other registered cell kinds.
Definition descriptor.h:148
virtual std::vector< std::string_view > sub_kinds() const =0
Sub-kind ids reachable from this descriptor.