Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion types/variant/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
## Fields

* `f` of type `std::variant<std::int32_t, std::string, std::vector<std::int32_t>>`
* `Vector` of type `std::vector<std::variant<std::int32_t, std::string>`

## Entries

1. `std::int32_t` with value `1`
2. `std::string` with value `"abc"`
3. `std::vector<std::int32_t>` with value `{1, 2, 3}`
3. `std::vector` with values `{1, 2, 3}`
4. Empty `std::string`
5. Empty `std::vector`

Expand Down
39 changes: 35 additions & 4 deletions types/variant/read.C
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ using ROOT::Experimental::RNTupleReader;
#include <variant>
#include <vector>

using Vector = std::vector<std::int32_t>;
using Variant = std::variant<std::int32_t, std::string, Vector>;
using VectorInt32 = std::vector<std::int32_t>;
using Variant = std::variant<std::int32_t, std::string, VectorInt32>;
using Vector = std::vector<std::variant<std::int32_t, std::string>>;

static void PrintVariantValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
Expand All @@ -24,7 +25,7 @@ static void PrintVariantValue(const REntry &entry, std::string_view name,
} else if (value.index() == 1) {
os << "\"" << std::get<std::string>(value) << "\"";
} else if (value.index() == 2) {
Vector &vectorValue = std::get<Vector>(value);
VectorInt32 &vectorValue = std::get<VectorInt32>(value);
os << "[";
bool first = true;
for (auto element : vectorValue) {
Expand All @@ -47,6 +48,35 @@ static void PrintVariantValue(const REntry &entry, std::string_view name,
os << "\n";
}

static void PrintVectorValue(const REntry &entry, std::string_view name,
std::ostream &os, bool last = false) {
Vector &value = *entry.GetPtr<Vector>(name);
os << " \"" << name << "\": [";
bool first = true;
for (auto &element : value) {
if (first) {
first = false;
} else {
os << ",";
}
os << "\n ";
if (element.index() == 0) {
os << std::get<std::int32_t>(element);
} else if (element.index() == 1) {
os << "\"" << std::get<std::string>(element) << "\"";
}
}
if (!value.empty()) {
os << "\n ";
}
os << "]";

if (!last) {
os << ",";
}
os << "\n";
}

void read(std::string_view input = "types.variant.root",
std::string_view output = "types.variant.json") {
std::ofstream os(std::string{output});
Expand All @@ -65,7 +95,8 @@ void read(std::string_view input = "types.variant.root",
}
os << " {\n";

PrintVariantValue(entry, "f", os, /*last=*/true);
PrintVariantValue(entry, "f", os);
PrintVectorValue(entry, "Vector", os, /*last=*/true);

os << " }";
// Newline is intentionally missing, may need to print a comma before the
Expand Down
15 changes: 11 additions & 4 deletions types/variant/write.C
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ using ROOT::Experimental::RNTupleWriter;
#include <variant>
#include <vector>

using Vector = std::vector<std::int32_t>;
using Variant = std::variant<std::int32_t, std::string, Vector>;
using VectorInt32 = std::vector<std::int32_t>;
using Variant = std::variant<std::int32_t, std::string, VectorInt32>;
using Vector = std::vector<std::variant<std::int32_t, std::string>>;

void write(std::string_view filename = "types.variant.root") {
auto model = RNTupleModel::Create();

auto value = model->MakeField<Variant>("f");
auto vector = model->MakeField<Vector>("Vector");

RNTupleWriteOptions options;
options.SetCompression(0);
Expand All @@ -27,21 +29,26 @@ void write(std::string_view filename = "types.variant.root") {

// First entry: std::int32_t
*value = 1;
*vector = {1};
writer->Fill();

// Second entry: std::string
*value = "abc";
*vector = {"abc"};
writer->Fill();

// Third entry: std::vector<std::int32_t>
*value = Vector{1, 2, 3};
*value = VectorInt32{1, 2, 3};
*vector = {1, "2", 3};
writer->Fill();

// Fourth entry: empty std::string
*value = "";
*vector = {""};
writer->Fill();

// Fifth entry: empty std::vector
*value = {};
*value = VectorInt32{};
*vector = {};
writer->Fill();
}