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

CRTP base providing default to_json / clone / kind plumbing on top of a storage type. More...

#include <cell.h>

Inheritance diagram for parcel::BaseCell< Derived, Storage >:
Collaboration diagram for parcel::BaseCell< Derived, Storage >:

Public Member Functions

template<typename U >
requires std::constructible_from<Storage, U&&>
 explicit (false) BaseCell(U &&v)
 Construct by forwarding into the storage (v becomes Storage::value).
 
template<typename U >
requires std::assignable_from<Storage&, U&&>
Derived & operator= (U &&v)
 Assign to the storage from v.
 
Storage & get ()
 Mutable access to the held storage.
 
Storage const & get () const
 Read-only access to the held storage.
 
std::string_view kind () const override
 Wire-stable kind identifier for this cell.
 
std::optional< DisplayInfo > const & overridden_display_info () const override
 Read-only access to this cell's optional display info.
 
cell_t clone () const override
 Deep-copy this cell.
 
std::partial_ordering compare (ICell const &other) const override
 Default three-way comparison: kind first, then storage value.
 
json_t to_json () const override
 Default JSON serialization for cells with JSON-convertible storage.
 
void inject_display_info (json_t &j) const
 Copy this cell's display info (if any) into the JSON object under "d".
 
- Public Member Functions inherited from parcel::ICell
virtual std::string to_string () const =0
 Render the cell's value as a compact human-readable string.
 
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

template<typename... Args>
requires std::constructible_from<Derived, Args&&...>
static std::shared_ptr< Derived > of (Args &&... args)
 Construct a shared_ptr<Derived> forwarding the arguments.
 
template<typename... Args>
requires std::constructible_from<Derived, Args&&...>
static std::unique_ptr< Derived > unique (Args &&... args)
 Construct a unique_ptr<Derived> forwarding the arguments.
 
template<typename Out >
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.
 
static cell_t from_json_strict (json_t const &j)
 Strict shorthand for the recurring cell_from_json + make_shared.
 

Public Attributes

Storage value {}
 Held value of the cell.
 

Protected Member Functions

void set_display_info (std::optional< DisplayInfo > m) override
 Replace this cell's display info in place.
 

Static Protected Member Functions

template<typename T >
static T cell_from_json (const json_t &j, const std::string_view expected_kind)
 Validate a wrapped {"k","v"} JSON object and extract its "v" as T.
 

Protected Attributes

std::optional< DisplayInfodisplay_info_
 Optional display info; omitted from JSON when empty.
 

Additional Inherited Members

- 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").
 

Detailed Description

template<typename Derived, typename Storage>
struct parcel::BaseCell< Derived, Storage >

CRTP base providing default to_json / clone / kind plumbing on top of a storage type.

Concrete cells inherit BaseCell<Derived, Storage> and supply kind_id, from_json, descriptor, and to_string. The storage type is held by value as BaseCell::value and exposed through get() plus inherited container-style helpers in derived cells (PrimitiveCell, TypedListCell, etc.).

Template Parameters
DerivedThe concrete cell type (CRTP self).
StorageThe held value type.

Member Function Documentation

◆ absorb_display_info()

template<typename Derived , typename Storage >
template<typename Out >
static void parcel::BaseCell< Derived, Storage >::absorb_display_info ( json_t const &  j,
Out &  out 
)
inlinestatic

Read "d" (if present) from a JSON object and assign it onto a cell.

Template Parameters
OutAny pointer-like type whose pointee derives from ICell.
Parameters
jInput JSON object.
outPointer-like to a cell whose display info should be populated.

◆ cell_from_json()

template<typename Derived , typename Storage >
template<typename T >
static T parcel::BaseCell< Derived, Storage >::cell_from_json ( const json_t j,
const std::string_view  expected_kind 
)
inlinestaticprotected

Validate a wrapped {"k","v"} JSON object and extract its "v" as T.

Used by primitive-style from_json overloads.

Template Parameters
TType to deserialize the "v" slot into.
Parameters
jInput JSON object.
expected_kindKind id this cell expects under "k".
Returns
Deserialized value.
Exceptions
std::runtime_errorif the object shape is invalid or the kind does not match expected_kind.

◆ clone()

template<typename Derived , typename Storage >
cell_t parcel::BaseCell< Derived, Storage >::clone ( ) const
inlineoverridevirtual

Deep-copy this cell.

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

Implements parcel::ICell.

Reimplemented in parcel::ListCell, parcel::MapCell, parcel::StructCell< Derived, Payload, StructId >, parcel::UnionCell< Ts >, and parcel::HashMapCell.

◆ compare()

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

Default three-way comparison: kind first, then storage value.

If Storage supports <=>, that comparison is forwarded directly (which yields partial_ordering for floating-point storage). If only == is available, equal values map to equivalent and unequal values to unordered. Storage types lacking either, or std::variant storage (which UnionCell overrides), return unordered.

Parameters
otherCell to compare with.
Returns
Three-way comparison result; ignores display info.

Implements parcel::ICell.

Reimplemented in parcel::TypedListCell< T >, parcel::ListCell, parcel::TypedMapCell< T >, parcel::MapCell, parcel::StructCell< Derived, Payload, StructId >, parcel::UnionCell< Ts >, parcel::TypedHashMapCell< T >, and parcel::HashMapCell.

◆ explicit()

template<typename Derived , typename Storage >
template<typename U >
requires std::constructible_from<Storage, U&&>
parcel::BaseCell< Derived, Storage >::explicit ( false  ) &&
inline

Construct by forwarding into the storage (v becomes Storage::value).

Conditionally non-explicit so concrete cells (e.g. I32Cell c = 42) can be brace-/copy-initialized from a raw storage value.

Template Parameters
UAny type the storage is constructible from.

◆ from_json_strict()

template<typename Derived , typename Storage >
static cell_t parcel::BaseCell< Derived, Storage >::from_json_strict ( json_t const &  j)
inlinestatic

Strict shorthand for the recurring cell_from_json + make_shared.

  • absorb_display_info pattern in concrete from_json overloads.

Validates that j is an object whose "k" matches Derived::kind_id, deserializes "v" as Storage, builds a shared_ptr<Derived> from it, and absorbs any display info block.

Requires Storage to be JSON-deserializable (every primitive cell already is) — concrete cells whose payload needs custom decoding (e.g. TypedListCell, StructCell) keep their own from_json body.

Parameters
jInput JSON object.
Returns
Newly built cell handle.
Exceptions
std::runtime_erroron shape or kind mismatch.

◆ inject_display_info()

template<typename Derived , typename Storage >
void parcel::BaseCell< Derived, Storage >::inject_display_info ( json_t j) const
inline

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

Parameters
jJSON object to mutate.

◆ kind()

template<typename Derived , typename Storage >
std::string_view parcel::BaseCell< Derived, Storage >::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.

◆ of()

template<typename Derived , typename Storage >
template<typename... Args>
requires std::constructible_from<Derived, Args&&...>
static std::shared_ptr< Derived > parcel::BaseCell< Derived, Storage >::of ( Args &&...  args)
inlinestatic

Construct a shared_ptr<Derived> forwarding the arguments.

Template Parameters
ArgsAny pack accepted by a Derived constructor.
Parameters
argsArguments forwarded to the constructor.
Returns
std::shared_ptr<Derived> owning the new instance.

◆ operator=()

template<typename Derived , typename Storage >
template<typename U >
requires std::assignable_from<Storage&, U&&>
Derived & parcel::BaseCell< Derived, Storage >::operator= ( U &&  v)
inline

Assign to the storage from v.

Template Parameters
UAny type assignable to the storage.
Parameters
vReplacement value.
Returns
Reference to the derived cell for chaining.

◆ overridden_display_info()

template<typename Derived , typename Storage >
std::optional< DisplayInfo > const & parcel::BaseCell< Derived, Storage >::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 , typename Storage >
void parcel::BaseCell< Derived, Storage >::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 , typename Storage >
json_t parcel::BaseCell< Derived, Storage >::to_json ( ) const
inlineoverridevirtual

Default JSON serialization for cells with JSON-convertible storage.

If Storage has a JSON adapter, emits {"k": kind_id, "v": value, optional "d": display info}. Otherwise the call throws — the derived cell must override to_json itself (ListCell, MapCell, etc.).

Returns
JSON object representation.
Exceptions
std::runtime_errorif Storage is not JSON-convertible.

Implements parcel::ICell.

Reimplemented in parcel::SystemTimePointCell, parcel::UnixMillisCell, parcel::DurationMsCell, parcel::YmdCell, parcel::PathCell, parcel::TypedListCell< T >, parcel::ListCell, parcel::TypedMapCell< T >, parcel::MapCell, parcel::StructCell< Derived, Payload, StructId >, parcel::UnionCell< Ts >, parcel::TypedHashMapCell< T >, and parcel::HashMapCell.

◆ unique()

template<typename Derived , typename Storage >
template<typename... Args>
requires std::constructible_from<Derived, Args&&...>
static std::unique_ptr< Derived > parcel::BaseCell< Derived, Storage >::unique ( Args &&...  args)
inlinestatic

Construct a unique_ptr<Derived> forwarding the arguments.

Template Parameters
ArgsAny pack accepted by a Derived constructor.
Parameters
argsArguments forwarded to the constructor.
Returns
std::unique_ptr<Derived> owning the new instance.

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