27inline std::ostream& operator<<(std::ostream& os, ICell
const& v) {
28 return os << v.to_string();
31inline std::ostream& operator<<(std::ostream& os, cell_t
const& v) {
32 return os << (v ? v->to_string() : std::string{
"<null>"});
37#if defined(__cpp_lib_format) && __cpp_lib_format >= 201907L
43struct std::formatter<parcel::ICell, char> {
44 enum class Mode { Compact, Formatted, JsonCompact, JsonPretty, KindOnly };
45 Mode mode = Mode::Compact;
47 constexpr auto parse(
const std::format_parse_context& ctx) {
50 auto it = ctx.begin();
53 const auto end = ctx.end();
54 if (it != end && *it ==
'#') {
55 mode = Mode::Formatted;
57 }
else if (it != end && *it ==
'j') {
59 if (it != end && *it ==
'2') {
60 mode = Mode::JsonPretty;
63 mode = Mode::JsonCompact;
65 }
else if (it != end && *it ==
'k') {
66 mode = Mode::KindOnly;
69 if (it != end && *it !=
'}') {
70 throw std::format_error(
71 "parcel::ICell formatter: expected one of '{}', '{:#}', '{:j}', '{:j2}', '{:k}'");
76 auto format(
parcel::ICell const& v, std::format_context& ctx)
const {
80 case Mode::JsonCompact:
81 return std::format_to(ctx.out(),
"{}", v.
to_json().dump());
82 case Mode::JsonPretty:
83 return std::format_to(ctx.out(),
"{}", v.
to_json().dump(2));
85 return std::format_to(ctx.out(),
"{}", v.
kind());
88 return std::format_to(ctx.out(),
"{}", v.
to_string());
94 requires std::derived_from<T, parcel::ICell> && (!std::same_as<T, parcel::ICell>)
95struct std::formatter<T, char> : std::formatter<parcel::ICell, char> {
96 auto format(T
const& v, std::format_context& ctx)
const {
97 return std::formatter<parcel::ICell, char>::format(
static_cast<parcel::ICell const&
>(v),
103struct std::formatter<parcel::
cell_t, char> : std::formatter<parcel::ICell, char> {
104 auto format(
parcel::cell_t const& v, std::format_context& ctx)
const {
106 return std::format_to(ctx.out(),
"<null>");
108 return std::formatter<parcel::ICell, char>::format(*v, ctx);
114#if defined(__cpp_lib_print) && __cpp_lib_print >= 202207L
123template <
class... Args>
124void print(std::format_string<Args...> fmt, Args&&... args) {
125 std::print(fmt, std::forward<Args>(args)...);
129template <
class... Args>
130void println(std::format_string<Args...> fmt, Args&&... args) {
131 std::println(fmt, std::forward<Args>(args)...);
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
Polymorphic root of every parcel cell.
Definition cell.h:84
virtual std::string to_formatted_string() const
Render the cell as a multi-line, indented string.
Definition cell.h:122
virtual std::string to_string() const =0
Render the cell's value as a compact human-readable string.
virtual json_t to_json() const =0
Serialize this cell to its canonical JSON representation.
virtual std::string_view kind() const =0
Wire-stable kind identifier for this cell.