TreeBuilder v2: Extend Element with a data member

This commit is contained in:
Alastair Robertson 2023-09-22 06:18:57 -07:00 committed by Alastair Robertson
parent c657a41d79
commit 7361d8fa29
2 changed files with 23 additions and 0 deletions

View File

@ -18,7 +18,9 @@
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <variant>
#include <vector>
namespace oi::result {
@ -31,6 +33,12 @@ struct Element {
struct IsSetStats {
bool is_set;
};
struct Pointer {
uintptr_t p;
};
struct Scalar {
uint64_t n;
};
std::string_view name;
std::vector<std::string_view>
@ -40,6 +48,8 @@ struct Element {
size_t exclusive_size;
std::optional<uintptr_t> pointer;
std::variant<std::nullopt_t, Pointer, Scalar, std::string> data = {
std::nullopt};
std::optional<ContainerStats> container_stats;
std::optional<IsSetStats> is_set_stats;
};

View File

@ -17,6 +17,7 @@
#include <algorithm>
#include <stdexcept>
#include <variant>
template <class>
inline constexpr bool always_false_v = false;
@ -99,6 +100,18 @@ void Json::print(IntrospectionResult::const_iterator& it,
out_ << tab << "\"pointer\":" << space << *(it->pointer) << ',' << endl
<< indent;
}
if (auto* s = std::get_if<result::Element::Scalar>(&it->data)) {
out_ << tab << "\"data\":" << space << s->n << ',' << endl << indent;
} else if (auto* p = std::get_if<result::Element::Pointer>(&it->data)) {
out_ << tab << "\"data\":" << space << "\"0x" << std::hex << p->p
<< std::dec << "\"," << endl
<< indent;
} else if (auto* str = std::get_if<std::string>(&it->data)) {
out_ << tab << "\"data\":" << space << "\"" << *str << "\"," << endl
<< indent;
}
if (it->container_stats.has_value()) {
out_ << tab << "\"length\":" << space << it->container_stats->length
<< ',' << endl