From 08658613d54f98710963bf7b5b1837f737ed6352 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Mon, 26 Jan 2026 19:26:54 -0500 Subject: [PATCH] xo-reflect: + pretty printing for xo::reflect::TypeDescr --- include/xo/reflect/TypeDescr.hpp | 30 ++++++++++++++++++++++++++++-- src/reflect/TypeDescr.cpp | 13 +++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/include/xo/reflect/TypeDescr.hpp b/include/xo/reflect/TypeDescr.hpp index d39d64e..4b3ca87 100644 --- a/include/xo/reflect/TypeDescr.hpp +++ b/include/xo/reflect/TypeDescr.hpp @@ -3,7 +3,8 @@ #pragma once #include "TypeDescrExtra.hpp" -#include "xo/cxxutil/demangle.hpp" +#include +#include #include #include #include @@ -15,6 +16,8 @@ #include namespace xo { + namespace print { class ppindentinfo; } + namespace reflect { class TaggedPtr; /* see [reflect/TaggedPtr.hpp] */ @@ -202,6 +205,9 @@ namespace xo { /* run-time description for a native c++ type */ class TypeDescrBase { + public: + using ppindentinfo = xo::print::ppindentinfo; + public: /* type-description objects for a type T is unique, * --> can always use its address @@ -391,6 +397,9 @@ namespace xo { TypeDescr fn_arg(uint32_t i) const { return this->tdextra_->fn_arg(i); } bool fn_is_noexcept() const { return this->tdextra_->fn_is_noexcept(); } + /** pretty-printer support, using @p ppii **/ + bool pretty(const ppindentinfo & ppii) const; + void display(std::ostream & os) const; std::string display_string() const; @@ -543,7 +552,6 @@ namespace xo { return os; } - /* tag to drive overload resolution */ struct reflected_types_printer {}; @@ -567,6 +575,24 @@ namespace xo { static TypeDescrTable s_instance; }; } /*namespace reflect*/ + + namespace print { + template <> + struct ppdetail { + static bool print_pretty(const ppindentinfo & ppii, + const xo::reflect::TypeDescrBase & td) { + return td.pretty(ppii); + } + }; + + template <> + struct ppdetail { + static bool print_pretty(const ppindentinfo & ppii, + xo::reflect::TypeDescr td) { + return td ? td->pretty(ppii) : true; + } + }; + } /*namespace print*/ } /*namespace xo*/ namespace std { diff --git a/src/reflect/TypeDescr.cpp b/src/reflect/TypeDescr.cpp index de5f4ad..c6cb166 100644 --- a/src/reflect/TypeDescr.cpp +++ b/src/reflect/TypeDescr.cpp @@ -301,6 +301,19 @@ namespace xo { return this->tdextra_->child_tp(i, object); } /*child_tp*/ + bool + TypeDescrBase::pretty(const ppindentinfo & ppii) const + { + return ppii.pps()->pretty_struct + (ppii, + "TypeDescr", + refrtag("id", id_), + refrtag("canonical_name", canonical_name_), + refrtag("complete", complete_flag_), + refrtag("metatype", this->metatype())); + + } + void TypeDescrBase::display(std::ostream & os) const {