xo-reflect: + pretty printing for xo::reflect::TypeDescr

This commit is contained in:
Roland Conybeare 2026-01-26 19:26:54 -05:00
commit 1b185a3680
3 changed files with 48 additions and 9 deletions

View file

@ -1,16 +1,16 @@
/* GcStatistics.hpp /** @file GcStatistics.hpp
* *
* author: Roland Conybeare, Aug 2025 * @author Roland Conybeare, Aug 2025
*/ **/
#pragma once #pragma once
#include "generation.hpp" #include "generation.hpp"
#include "CircularBuffer.hpp" #include "CircularBuffer.hpp"
#include "xo/reflect/TypeDescr.hpp" #include <xo/reflect/TypeDescr.hpp>
#include "xo/unit/quantity.hpp" #include <xo/unit/quantity.hpp>
#include "xo/unit/quantity_iostream.hpp" #include <xo/unit/quantity_iostream.hpp>
#include "xo/indentlog/print/pretty.hpp" #include <xo/indentlog/print/pretty.hpp>
#include <ostream> #include <ostream>
#include <array> #include <array>

View file

@ -3,7 +3,8 @@
#pragma once #pragma once
#include "TypeDescrExtra.hpp" #include "TypeDescrExtra.hpp"
#include "xo/cxxutil/demangle.hpp" #include <xo/indentlog/print/pretty.hpp>
#include <xo/cxxutil/demangle.hpp>
#include <iostream> #include <iostream>
#include <typeinfo> #include <typeinfo>
#include <unordered_map> #include <unordered_map>
@ -15,6 +16,8 @@
#include <cassert> #include <cassert>
namespace xo { namespace xo {
namespace print { class ppindentinfo; }
namespace reflect { namespace reflect {
class TaggedPtr; /* see [reflect/TaggedPtr.hpp] */ class TaggedPtr; /* see [reflect/TaggedPtr.hpp] */
@ -202,6 +205,9 @@ namespace xo {
/* run-time description for a native c++ type */ /* run-time description for a native c++ type */
class TypeDescrBase { class TypeDescrBase {
public:
using ppindentinfo = xo::print::ppindentinfo;
public: public:
/* type-description objects for a type T is unique, /* type-description objects for a type T is unique,
* --> can always use its address * --> can always use its address
@ -391,6 +397,9 @@ namespace xo {
TypeDescr fn_arg(uint32_t i) const { return this->tdextra_->fn_arg(i); } TypeDescr fn_arg(uint32_t i) const { return this->tdextra_->fn_arg(i); }
bool fn_is_noexcept() const { return this->tdextra_->fn_is_noexcept(); } 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; void display(std::ostream & os) const;
std::string display_string() const; std::string display_string() const;
@ -543,7 +552,6 @@ namespace xo {
return os; return os;
} }
/* tag to drive overload resolution */ /* tag to drive overload resolution */
struct reflected_types_printer {}; struct reflected_types_printer {};
@ -567,6 +575,24 @@ namespace xo {
static TypeDescrTable s_instance; static TypeDescrTable s_instance;
}; };
} /*namespace reflect*/ } /*namespace reflect*/
namespace print {
template <>
struct ppdetail<xo::reflect::TypeDescrBase> {
static bool print_pretty(const ppindentinfo & ppii,
const xo::reflect::TypeDescrBase & td) {
return td.pretty(ppii);
}
};
template <>
struct ppdetail<xo::reflect::TypeDescr> {
static bool print_pretty(const ppindentinfo & ppii,
xo::reflect::TypeDescr td) {
return td ? td->pretty(ppii) : true;
}
};
} /*namespace print*/
} /*namespace xo*/ } /*namespace xo*/
namespace std { namespace std {

View file

@ -301,6 +301,19 @@ namespace xo {
return this->tdextra_->child_tp(i, object); return this->tdextra_->child_tp(i, object);
} /*child_tp*/ } /*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 void
TypeDescrBase::display(std::ostream & os) const TypeDescrBase::display(std::ostream & os) const
{ {