diff --git a/types/variant/README.md b/types/variant/README.md index 66bd1d7..49fdaca 100644 --- a/types/variant/README.md +++ b/types/variant/README.md @@ -3,12 +3,13 @@ ## Fields * `f` of type `std::variant>` + * `Vector` of type `std::vector` ## Entries 1. `std::int32_t` with value `1` 2. `std::string` with value `"abc"` -3. `std::vector` with value `{1, 2, 3}` +3. `std::vector` with values `{1, 2, 3}` 4. Empty `std::string` 5. Empty `std::vector` diff --git a/types/variant/read.C b/types/variant/read.C index 77be0d3..e98b093 100644 --- a/types/variant/read.C +++ b/types/variant/read.C @@ -12,8 +12,9 @@ using ROOT::Experimental::RNTupleReader; #include #include -using Vector = std::vector; -using Variant = std::variant; +using VectorInt32 = std::vector; +using Variant = std::variant; +using Vector = std::vector>; static void PrintVariantValue(const REntry &entry, std::string_view name, std::ostream &os, bool last = false) { @@ -24,7 +25,7 @@ static void PrintVariantValue(const REntry &entry, std::string_view name, } else if (value.index() == 1) { os << "\"" << std::get(value) << "\""; } else if (value.index() == 2) { - Vector &vectorValue = std::get(value); + VectorInt32 &vectorValue = std::get(value); os << "["; bool first = true; for (auto element : vectorValue) { @@ -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(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(element); + } else if (element.index() == 1) { + os << "\"" << std::get(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}); @@ -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 diff --git a/types/variant/write.C b/types/variant/write.C index 5bdee4d..eed506a 100644 --- a/types/variant/write.C +++ b/types/variant/write.C @@ -12,13 +12,15 @@ using ROOT::Experimental::RNTupleWriter; #include #include -using Vector = std::vector; -using Variant = std::variant; +using VectorInt32 = std::vector; +using Variant = std::variant; +using Vector = std::vector>; void write(std::string_view filename = "types.variant.root") { auto model = RNTupleModel::Create(); auto value = model->MakeField("f"); + auto vector = model->MakeField("Vector"); RNTupleWriteOptions options; options.SetCompression(0); @@ -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 - *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(); }