From 45a0675436c7512398a0750b158d0a1907e6cf27 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Mon, 9 Oct 2023 14:54:39 -0400 Subject: [PATCH] printjson: add printer for nested TaggedPtr's --- src/printjson/PrintJson.cpp | 45 ++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/printjson/PrintJson.cpp b/src/printjson/PrintJson.cpp index 1ac048b6..7df2d5b3 100644 --- a/src/printjson/PrintJson.cpp +++ b/src/printjson/PrintJson.cpp @@ -17,6 +17,7 @@ namespace xo { using xo::reflect::TypeDescr; using xo::reflect::TaggedPtr; using xo::reflect::TaggedRcptr; + using xo::print::quoted; using xo::time::iso8601; using xo::ref::rp; using xo::xtag; @@ -183,6 +184,46 @@ namespace xo { this->print_tp(obj->self_tp(), p_os); } /*print_obj*/ + /* Consider: + * TaggedPtr tp = ...; + * std::ostream * p_os = ...; + * + * PrintJson * pjson = PrintJsonSingleton::instance(); + * + * // print json representation, depending on runtime type of tp's target + * pjson->print_tp(tp, p_os); + * + * // can also use .print(), relying on JsonPrinter_TaggedPtr + * // .print() will next original TaggedPtr in another; + * // this shim unwinds that + * // + * pjson->print(tp, p_os); + */ + class JsonPrinter_TaggedPtr : public JsonPrinter { + public: + JsonPrinter_TaggedPtr(PrintJson const * pjson) : JsonPrinter(pjson) {} + + virtual void print_json(TaggedPtr tp, + std::ostream * p_os) const override { + TaggedPtr * x = this->check_recover_native(tp, p_os); + + if (x) { + this->pjson()->print_tp(*x, p_os); + } + } /*print_json*/ + }; /*JsonPrinter_TaggedPtr*/ + + namespace { + void + provide_tagged_ptr_printer(PrintJson * p_json) + { + std::unique_ptr printer(new JsonPrinter_TaggedPtr(p_json)); + + p_json->provide_printer(Reflect::require(), + std::move(printer)); + } /*provide_tagged_ptr_printer*/ + } /*namespace*/ + class JsonPrinter_bool : public JsonPrinter { public: JsonPrinter_bool(PrintJson const * pjson) : JsonPrinter(pjson) {} @@ -299,7 +340,7 @@ namespace xo { if (x) { /* TODO: escapes special characters */ - *p_os << *x; + *p_os << quoted(*x); } else { report_internal_type_consistency_error(Reflect::require(), tp.td(), @@ -360,6 +401,8 @@ namespace xo { void PrintJson::provide_std_printers() { + provide_tagged_ptr_printer(this); + provide_bool_printer(this); provide_integer_printer(this);