Skip to content

Commit 7361d8f

Browse files
committed
TreeBuilder v2: Extend Element with a data member
1 parent c657a41 commit 7361d8f

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

include/oi/result/Element.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
#include <optional>
2020
#include <span>
21+
#include <string>
2122
#include <string_view>
23+
#include <variant>
2224
#include <vector>
2325

2426
namespace oi::result {
@@ -31,6 +33,12 @@ struct Element {
3133
struct IsSetStats {
3234
bool is_set;
3335
};
36+
struct Pointer {
37+
uintptr_t p;
38+
};
39+
struct Scalar {
40+
uint64_t n;
41+
};
3442

3543
std::string_view name;
3644
std::vector<std::string_view>
@@ -40,6 +48,8 @@ struct Element {
4048
size_t exclusive_size;
4149

4250
std::optional<uintptr_t> pointer;
51+
std::variant<std::nullopt_t, Pointer, Scalar, std::string> data = {
52+
std::nullopt};
4353
std::optional<ContainerStats> container_stats;
4454
std::optional<IsSetStats> is_set_stats;
4555
};

oi/exporters/Json.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <algorithm>
1919
#include <stdexcept>
20+
#include <variant>
2021

2122
template <class>
2223
inline constexpr bool always_false_v = false;
@@ -99,6 +100,18 @@ void Json::print(IntrospectionResult::const_iterator& it,
99100
out_ << tab << "\"pointer\":" << space << *(it->pointer) << ',' << endl
100101
<< indent;
101102
}
103+
104+
if (auto* s = std::get_if<result::Element::Scalar>(&it->data)) {
105+
out_ << tab << "\"data\":" << space << s->n << ',' << endl << indent;
106+
} else if (auto* p = std::get_if<result::Element::Pointer>(&it->data)) {
107+
out_ << tab << "\"data\":" << space << "\"0x" << std::hex << p->p
108+
<< std::dec << "\"," << endl
109+
<< indent;
110+
} else if (auto* str = std::get_if<std::string>(&it->data)) {
111+
out_ << tab << "\"data\":" << space << "\"" << *str << "\"," << endl
112+
<< indent;
113+
}
114+
102115
if (it->container_stats.has_value()) {
103116
out_ << tab << "\"length\":" << space << it->container_stats->length
104117
<< ',' << endl

0 commit comments

Comments
 (0)