Skip to content

Commit 06cdeef

Browse files
committed
Test std::vector<std::variant<...>>
Closes #48
1 parent 63d9408 commit 06cdeef

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

types/variant/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
## Fields
44

55
* `f` of type `std::variant<std::int32_t, std::string, std::vector<std::int32_t>>`
6+
* `Vector` of type `std::vector<std::variant<std::int32_t, std::string>`
67

78
## Entries
89

910
1. `std::int32_t` with value `1`
1011
2. `std::string` with value `"abc"`
11-
3. `std::vector<std::int32_t>` with value `{1, 2, 3}`
12+
3. `std::vector` with values `{1, 2, 3}`
1213
4. Empty `std::string`
1314
5. Empty `std::vector`
1415

types/variant/read.C

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ using ROOT::Experimental::RNTupleReader;
1212
#include <variant>
1313
#include <vector>
1414

15-
using Vector = std::vector<std::int32_t>;
16-
using Variant = std::variant<std::int32_t, std::string, Vector>;
15+
using VectorInt32 = std::vector<std::int32_t>;
16+
using Variant = std::variant<std::int32_t, std::string, VectorInt32>;
17+
using Vector = std::vector<std::variant<std::int32_t, std::string>>;
1718

1819
static void PrintVariantValue(const REntry &entry, std::string_view name,
1920
std::ostream &os, bool last = false) {
@@ -24,7 +25,7 @@ static void PrintVariantValue(const REntry &entry, std::string_view name,
2425
} else if (value.index() == 1) {
2526
os << "\"" << std::get<std::string>(value) << "\"";
2627
} else if (value.index() == 2) {
27-
Vector &vectorValue = std::get<Vector>(value);
28+
VectorInt32 &vectorValue = std::get<VectorInt32>(value);
2829
os << "[";
2930
bool first = true;
3031
for (auto element : vectorValue) {
@@ -47,6 +48,35 @@ static void PrintVariantValue(const REntry &entry, std::string_view name,
4748
os << "\n";
4849
}
4950

51+
static void PrintVectorValue(const REntry &entry, std::string_view name,
52+
std::ostream &os, bool last = false) {
53+
Vector &value = *entry.GetPtr<Vector>(name);
54+
os << " \"" << name << "\": [";
55+
bool first = true;
56+
for (auto &element : value) {
57+
if (first) {
58+
first = false;
59+
} else {
60+
os << ",";
61+
}
62+
os << "\n ";
63+
if (element.index() == 0) {
64+
os << std::get<std::int32_t>(element);
65+
} else if (element.index() == 1) {
66+
os << "\"" << std::get<std::string>(element) << "\"";
67+
}
68+
}
69+
if (!value.empty()) {
70+
os << "\n ";
71+
}
72+
os << "]";
73+
74+
if (!last) {
75+
os << ",";
76+
}
77+
os << "\n";
78+
}
79+
5080
void read(std::string_view input = "types.variant.root",
5181
std::string_view output = "types.variant.json") {
5282
std::ofstream os(std::string{output});
@@ -65,7 +95,8 @@ void read(std::string_view input = "types.variant.root",
6595
}
6696
os << " {\n";
6797

68-
PrintVariantValue(entry, "f", os, /*last=*/true);
98+
PrintVariantValue(entry, "f", os);
99+
PrintVectorValue(entry, "Vector", os, /*last=*/true);
69100

70101
os << " }";
71102
// Newline is intentionally missing, may need to print a comma before the

types/variant/write.C

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ using ROOT::Experimental::RNTupleWriter;
1212
#include <variant>
1313
#include <vector>
1414

15-
using Vector = std::vector<std::int32_t>;
16-
using Variant = std::variant<std::int32_t, std::string, Vector>;
15+
using VectorInt32 = std::vector<std::int32_t>;
16+
using Variant = std::variant<std::int32_t, std::string, VectorInt32>;
17+
using Vector = std::vector<std::variant<std::int32_t, std::string>>;
1718

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

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

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

2830
// First entry: std::int32_t
2931
*value = 1;
32+
*vector = {1};
3033
writer->Fill();
3134

3235
// Second entry: std::string
3336
*value = "abc";
37+
*vector = {"abc"};
3438
writer->Fill();
3539

3640
// Third entry: std::vector<std::int32_t>
37-
*value = Vector{1, 2, 3};
41+
*value = VectorInt32{1, 2, 3};
42+
*vector = {1, "2", 3};
3843
writer->Fill();
3944

4045
// Fourth entry: empty std::string
4146
*value = "";
47+
*vector = {""};
4248
writer->Fill();
4349

4450
// Fifth entry: empty std::vector
4551
*value = {};
52+
*vector = {};
4653
writer->Fill();
4754
}

0 commit comments

Comments
 (0)