parcel 0.2.2
Wrappable, wire-transferable C++23 value system with JSON serialization
Loading...
Searching...
No Matches
parcel::SelfStructCell< Derived > Class Template Reference

CRTP base for struct cells that are their own payload. More...

#include <struct.h>

Inheritance diagram for parcel::SelfStructCell< Derived >:
Collaboration diagram for parcel::SelfStructCell< Derived >:

Public Types

using storage_t = Derived
 Storage-type alias used by descriptors that probe T::storage_t.
 

Public Member Functions

std::string_view kind () const override
 Wire-stable kind identifier for this cell.
 
std::string to_string () const override
 Render the cell's value as a compact human-readable string.
 
json_t to_json () const override
 Serialize this cell to its canonical JSON representation.
 
cell_t clone () const override
 Deep-copy this cell.
 
std::partial_ordering compare (ICell const &other) const override
 Three-way comparison over field values (and cell_extras_ when allow_extra_fields is true); ignores display info.
 
std::optional< DisplayInfo > const & overridden_display_info () const override
 Read-only access to this cell's optional display info.
 
- Public Member Functions inherited from parcel::ICell
virtual std::string to_formatted_string () const
 Render the cell as a multi-line, indented string.
 
virtual std::size_t hash_value () const noexcept
 Equality-consistent hash that mirrors compare's display-info-insensitivity.
 
cell_t with_display_info (DisplayInfo m) const
 Return a deep copy with the entire display info block replaced.
 
cell_t with_name (std::string v) const
 Return a deep copy with name set to v.
 
cell_t with_description (std::string v) const
 Return a deep copy with description set to v.
 
cell_t with_icon (comms::Icon icon) const
 Return a deep copy with icon set to the typed icon.
 
cell_t with_icon (std::string const &v) const
 Return a deep copy with icon parsed from an Iconify set:name string (e.g.
 
cell_t with_color (comms::Color color) const
 Return a deep copy with color set to the typed color.
 
cell_t with_color (std::string const &v) const
 Return a deep copy with color parsed from a color string (hex like "#ffcc00", a CSS-functional form, or a CSS color name).
 

Static Public Member Functions

static cell_t from_json (json_t const &j, ParcelRegistry const &reg)
 Deserialize the self-payload struct cell from JSON.
 
static cell_type_descriptor_t descriptor ()
 Cached descriptor for this self-payload struct cell.
 

Static Public Attributes

static constexpr bool allow_extra_fields = false
 Whether unknown JSON keys are tolerated.
 
- Static Public Attributes inherited from parcel::ICell
static constexpr std::string_view KEY_KIND = "k"
 JSON key for the kind id ("k").
 
static constexpr std::string_view KEY_VALUE = "v"
 JSON key for the value payload ("v").
 
static constexpr std::string_view KEY_DESCRIPTION = "d"
 JSON key for the optional display info block ("d").
 

Protected Member Functions

void inject_display_info (json_t &j) const
 Copy this cell's display info (if any) into the JSON object under "d".
 
void set_display_info (std::optional< DisplayInfo > m) override
 Replace this cell's display info in place.
 

Protected Attributes

std::optional< DisplayInfodisplay_info_
 Optional display info; omitted from JSON when empty.
 
std::map< std::string, cell_tcell_extras_
 Unknown keys retained when Derived::allow_extra_fields is true.
 

Detailed Description

template<typename Derived>
class parcel::SelfStructCell< Derived >

CRTP base for struct cells that are their own payload.

Where StructCell<Derived, Payload, StructId> composes a separate payload struct held in BaseCell::value, SelfStructCell<Derived> inherits ICell directly and treats Derived itself as the payload. Field descriptors are IPayloadFieldDescriptor<Derived> and address members on the cell instance itself — there is no nested value.

Library authors typically introduce a CRTP intermediate (e.g. BaseEvent<Self, EventId>) on top of SelfStructCell to carry common fields and to choose their own wire-namespace prefix:

template <typename Self, parcel::FixedString EventId>
class BaseEvent : public parcel::SelfStructCell<Self> {
public:
static constexpr std::string_view kind_id =
parcel::id_join_lit_v<"s:event:", EventId>;
protected:
std::int64_t timestamp_{};
public:
[[nodiscard]] std::int64_t timestamp() const { return timestamp_; }
static auto field_descriptors() {
auto builder = parcel::FieldsBuilder<Self>{};
return Self::event_field_descriptors(builder)
.template field<&BaseEvent::timestamp_>("timestamp")
.build();
}
};
Fluent builder used inside Derived::field_descriptors() to declare struct fields.
Definition struct.h:342
payload_field_descriptors_t build()
Move out the accumulated field descriptors.
Definition struct.h:543
CRTP base for struct cells that are their own payload.
Definition struct.h:984
constexpr std::string_view id_join_lit_v
Convenience alias yielding the joined std::string_view.
Definition common.h:140

Unlike StructCell, SelfStructCell has no StructId template parameter and does not synthesize a kind_id; the deriving class (or a CRTP intermediate) declares static constexpr std::string_view kind_id directly, which gives library authors freedom to carve out their own sub-namespace (e.g. "s:event:something").

Derived must additionally provide:

  • static constexpr std::string_view kind_id — wire kind id.
  • static auto field_descriptors() — built via FieldsBuilder<Derived>.
  • (optional) static DisplayInfo display_info() — cell-level display info; defaults to an empty DisplayInfo{} when not declared.
  • (optional) static constexpr bool allow_extra_fields — defaults to false.

Derived must also be default-constructible with access from parcel-internal codefrom_json calls std::make_shared<Derived>(). Keeping the default constructor public while making data members protected/private is the standard pattern.

Template Parameters
DerivedThe concrete struct cell, which is also its own payload.

Member Typedef Documentation

◆ storage_t

template<typename Derived >
using parcel::SelfStructCell< Derived >::storage_t = Derived

Storage-type alias used by descriptors that probe T::storage_t.

SelfStructCell does not own a separate payload — the cell is its own payload — so storage_t resolves to Derived itself. Lets the existing BaseCellTypeDescriptor<Derived>::storage_type() keep working unchanged.

Member Function Documentation

◆ clone()

template<typename Derived >
cell_t parcel::SelfStructCell< Derived >::clone ( ) const
inlineoverridevirtual

Deep-copy this cell.

Returns
A new cell_t whose state is independent of the original.

Implements parcel::ICell.

◆ compare()

template<typename Derived >
std::partial_ordering parcel::SelfStructCell< Derived >::compare ( ICell const &  other) const
inlineoverridevirtual

Three-way comparison over field values (and cell_extras_ when allow_extra_fields is true); ignores display info.

Walks declared fields in the order returned by field_descriptors() and short-circuits on the first non-equal result. Optional fields follow std::optional's <=>. When allow_extra_fields is true, after declared fields agree the cell_extras_ maps are compared lexicographically by key, with ICell <=> resolving each value pair.

Implements parcel::ICell.

◆ descriptor()

template<typename Derived >
static cell_type_descriptor_t parcel::SelfStructCell< Derived >::descriptor ( )
inlinestatic

Cached descriptor for this self-payload struct cell.

Returns
Shared StructCellTypeDescriptor<Derived, Derived> instance.

◆ from_json()

template<typename Derived >
static cell_t parcel::SelfStructCell< Derived >::from_json ( json_t const &  j,
ParcelRegistry const &  reg 
)
inlinestatic

Deserialize the self-payload struct cell from JSON.

Mirrors StructCell::from_json, but field descriptors target Derived itself rather than a separate payload type.

Parameters
jInput JSON object — must match {"k": kind_id, "v": {…}}.
regRegistry used to dispatch nested cells.
Returns
Newly built cell handle.
Exceptions
std::runtime_erroron shape or kind mismatch, missing required field, or unknown key when extras are not allowed.

◆ inject_display_info()

template<typename Derived >
void parcel::SelfStructCell< Derived >::inject_display_info ( json_t j) const
inlineprotected

Copy this cell's display info (if any) into the JSON object under "d".

Mirrors BaseCell::inject_display_info — re-declared here because SelfStructCell inherits ICell directly, not BaseCell.

Parameters
jJSON object to mutate.

◆ kind()

template<typename Derived >
std::string_view parcel::SelfStructCell< Derived >::kind ( ) const
inlineoverridevirtual

Wire-stable kind identifier for this cell.

Returns
The same value as the static kind_id on the concrete type.

Implements parcel::ICell.

◆ overridden_display_info()

template<typename Derived >
std::optional< DisplayInfo > const & parcel::SelfStructCell< Derived >::overridden_display_info ( ) const
inlineoverridevirtual

Read-only access to this cell's optional display info.

Returns
Reference to the held optional; empty when no display info is set.

Implements parcel::ICell.

◆ set_display_info()

template<typename Derived >
void parcel::SelfStructCell< Derived >::set_display_info ( std::optional< DisplayInfo m)
inlineoverrideprotectedvirtual

Replace this cell's display info in place.

Used by with_* builders on freshly cloned receivers and by JSON deserializers; no other public path mutates display info.

Parameters
mNew display info value (may be empty to clear).

Implements parcel::ICell.

◆ to_json()

template<typename Derived >
json_t parcel::SelfStructCell< Derived >::to_json ( ) const
inlineoverridevirtual

Serialize this cell to its canonical JSON representation.

Returns
JSON object of shape {"k", "v", optional "d"}.

Implements parcel::ICell.

◆ to_string()

template<typename Derived >
std::string parcel::SelfStructCell< Derived >::to_string ( ) const
inlineoverridevirtual

Render the cell's value as a compact human-readable string.

Returns
String form — implementation-defined per cell type.

Implements parcel::ICell.

Member Data Documentation

◆ allow_extra_fields

template<typename Derived >
constexpr bool parcel::SelfStructCell< Derived >::allow_extra_fields = false
staticconstexpr

Whether unknown JSON keys are tolerated.

Concrete Derived types may shadow this with true to opt into lenient deserialization; the default is strict (false).


The documentation for this class was generated from the following file: