Skip to content

Commit fcf0055

Browse files
committed
Add test for std::variant
Closes #4
1 parent b82c823 commit fcf0055

File tree

5 files changed

+142
-1
lines changed

5 files changed

+142
-1
lines changed

types/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
* [`fundamental`](fundamental): fundamental column types
44
* [`string`](string): `std::string` with all `[Split]Index{32,64}` column types
5+
* [`variant`](variant): `std::variant` with `Switch` column type
56
* [`vector`](vector): `std::vector` with all `[Split]Index{32,64}` column types

types/fundamental/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
__Covered under a different category:__
88
* `[Split]Index{32,64}`: with [`std::string`](../string) and [`std::vector`](../vector)
9-
* `Switch`
9+
* `Switch`: with [`std::variant`](../variant)
1010

1111
__Missing:__
1212
* `Real32Trunc`

types/variant/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# `std::variant`
2+
3+
## Fields
4+
5+
* `f` of type `std::variant<std::int32_t, std::string, std::vector<std::int32_t>>`
6+
7+
## Entries
8+
9+
1. `std::int32_t` with value `1`
10+
2. `std::string` with value `"abc"`
11+
3. `std::vector<std::int32_t>` with value `{1, 2, 3}`
12+
4. Empty `std::string`
13+
5. Empty `std::vector`
14+
15+
__Missing:__
16+
A `std::variant` that is `valueless_by_exception`.
17+
This requires a type that throws during move assignment.

types/variant/read.C

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <ROOT/REntry.hxx>
2+
#include <ROOT/RNTupleReader.hxx>
3+
4+
using ROOT::Experimental::REntry;
5+
using ROOT::Experimental::RNTupleReader;
6+
7+
#include <cstdint>
8+
#include <fstream>
9+
#include <ostream>
10+
#include <string>
11+
#include <string_view>
12+
#include <variant>
13+
#include <vector>
14+
15+
using Vector = std::vector<std::int32_t>;
16+
using Variant = std::variant<std::int32_t, std::string, Vector>;
17+
18+
static void PrintVariantValue(const REntry &entry, std::string_view name,
19+
std::ostream &os, bool last = false) {
20+
Variant &value = *entry.GetPtr<Variant>(name);
21+
os << " \"" << name << "\": ";
22+
if (value.index() == 0) {
23+
os << std::get<std::int32_t>(value);
24+
} else if (value.index() == 1) {
25+
os << "\"" << std::get<std::string>(value) << "\"";
26+
} else if (value.index() == 2) {
27+
Vector &vectorValue = std::get<Vector>(value);
28+
os << "[";
29+
bool first = true;
30+
for (auto element : vectorValue) {
31+
if (first) {
32+
first = false;
33+
} else {
34+
os << ",";
35+
}
36+
os << "\n " << element;
37+
}
38+
if (!vectorValue.empty()) {
39+
os << "\n ";
40+
}
41+
os << "]";
42+
}
43+
44+
if (!last) {
45+
os << ",";
46+
}
47+
os << "\n";
48+
}
49+
50+
void read(std::string_view input = "types.variant.root",
51+
std::string_view output = "types.variant.json") {
52+
std::ofstream os(std::string{output});
53+
os << "[\n";
54+
55+
auto reader = RNTupleReader::Open("ntpl", input);
56+
auto &entry = reader->GetModel().GetDefaultEntry();
57+
bool first = true;
58+
for (auto index : *reader) {
59+
reader->LoadEntry(index);
60+
61+
if (first) {
62+
first = false;
63+
} else {
64+
os << ",\n";
65+
}
66+
os << " {\n";
67+
68+
PrintVariantValue(entry, "f", os, /*last=*/true);
69+
70+
os << " }";
71+
// Newline is intentionally missing, may need to print a comma before the
72+
// next entry.
73+
}
74+
os << "\n";
75+
os << "]\n";
76+
}

types/variant/write.C

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <ROOT/RNTupleModel.hxx>
2+
#include <ROOT/RNTupleWriteOptions.hxx>
3+
#include <ROOT/RNTupleWriter.hxx>
4+
5+
using ROOT::Experimental::RNTupleModel;
6+
using ROOT::Experimental::RNTupleWriteOptions;
7+
using ROOT::Experimental::RNTupleWriter;
8+
9+
#include <cstdint>
10+
#include <string>
11+
#include <string_view>
12+
#include <variant>
13+
#include <vector>
14+
15+
using Vector = std::vector<std::int32_t>;
16+
using Variant = std::variant<std::int32_t, std::string, Vector>;
17+
18+
void write(std::string_view filename = "types.variant.root") {
19+
auto model = RNTupleModel::Create();
20+
21+
auto value = model->MakeField<Variant>("f");
22+
23+
RNTupleWriteOptions options;
24+
options.SetCompression(0);
25+
auto writer =
26+
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);
27+
28+
// First entry: std::int32_t
29+
*value = 1;
30+
writer->Fill();
31+
32+
// Second entry: std::string
33+
*value = "abc";
34+
writer->Fill();
35+
36+
// Third entry: std::vector<std::int32_t>
37+
*value = Vector{1, 2, 3};
38+
writer->Fill();
39+
40+
// Fourth entry: empty std::string
41+
*value = "";
42+
writer->Fill();
43+
44+
// Fifth entry: empty std::vector
45+
*value = {};
46+
writer->Fill();
47+
}

0 commit comments

Comments
 (0)