parcel 0.2.4
Wrappable, wire-transferable C++23 value system with JSON serialization
Loading...
Searching...
No Matches
flag.h
Go to the documentation of this file.
1#pragma once
2
11#include <parcel/cell.h>
12#include <parcel/defaults.h>
13#include <parcel/descriptor.h>
14#include <parcel/json.h>
15
16#include <commons/flag.hpp>
17#include <commons/json/flag.hpp>
18
19#include <string>
20#include <string_view>
21#include <utility>
22
23namespace parcel {
24
31class FlagCell : public BaseCell<FlagCell, comms::FlagRef> {
33
34public:
35 using base_t::base_t;
36 using base_t::operator=;
37
38 static constexpr std::string_view kind_id = "flag";
39
40 [[nodiscard]] std::string to_string() const override {
41 return std::string{this->value.name};
42 }
43
44 static cell_t from_json(json_t const& j, ParcelRegistry const&) {
45 auto v = base_t::cell_from_json<comms::FlagRef>(j, kind_id);
46 auto cell = std::make_shared<FlagCell>(v);
48 return cell;
49 }
50
51 static cell_type_descriptor_t descriptor() {
52 static const auto d = std::make_shared<SimpleCellTypeDescriptor<FlagCell>>(DisplayInfo{
53 .name = "Flag", .description = "Reference to a registered flag (name on the wire)"});
54 return d;
55 }
56};
57
64class FlagSetCell : public BaseCell<FlagSetCell, comms::FlagSet> {
66
67public:
68 using base_t::base_t;
69 using base_t::operator=;
70
71 static constexpr std::string_view kind_id = "flag_set";
72
73 [[nodiscard]] std::string to_string() const override {
74 std::string out = "[";
75 bool first = true;
76 for (auto const& f : this->value) {
77 if (!first) {
78 out += ", ";
79 }
80 out += std::string{f.name};
81 first = false;
82 }
83 out += "]";
84 return out;
85 }
86
87 static cell_t from_json(json_t const& j, ParcelRegistry const&) {
88 auto v = base_t::cell_from_json<comms::FlagSet>(j, kind_id);
89 auto cell = std::make_shared<FlagSetCell>(std::move(v));
91 return cell;
92 }
93
94 static cell_type_descriptor_t descriptor() {
95 static const auto d = std::make_shared<SimpleCellTypeDescriptor<FlagSetCell>>(
96 DisplayInfo{.name = "FlagSet",
97 .description = "Insertion-ordered set of flags (names on the wire)"});
98 return d;
99 }
100};
101
102} // namespace parcel
103
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::FlagRef.
Definition flag.h:31
std::string to_string() const override
Render the cell's value as a compact human-readable string.
Definition flag.h:40
Cell wrapping comms::FlagSet.
Definition flag.h:64
std::string to_string() const override
Render the cell's value as a compact human-readable string.
Definition flag.h:73
Runtime catalog of cell-type descriptors, keyed by wire kind id.
Definition registry.h:115
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
CRTP base providing default to_json / clone / kind plumbing on top of a storage type.
Definition cell.h:343
static void absorb_display_info(json_t const &j, Out &out)
Read "d" (if present) from a JSON object and assign it onto a cell.
Definition cell.h:499
comms::FlagRef value
Held value of the cell.
Definition cell.h:348