Skip to content

Commit 5f6b7fb

Browse files
committed
Add test for std::optional
Closes #15
1 parent 077ae65 commit 5f6b7fb

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

types/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Types
22

33
* [`fundamental`](fundamental): fundamental column types
4+
* [`optional`](optional): `std::optional` with different element types
45
* [`string`](string): `std::string` with all `[Split]Index{32,64}` column types
56
* [`variant`](variant): `std::variant` with `Switch` column type
67
* [`vector`](vector): `std::vector` with all `[Split]Index{32,64}` column types

types/optional/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# `std::optional`
2+
3+
## Fields
4+
5+
* `Int32`
6+
* `String`
7+
* `Vector`
8+
9+
with the corresponding element type and the default column types.
10+
11+
## Entries
12+
13+
1. Simple values
14+
2. No value
15+
3. Null / empty values

types/optional/read.C

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 <optional>
10+
#include <ostream>
11+
#include <string>
12+
#include <string_view>
13+
#include <vector>
14+
15+
using VectorTy = std::vector<std::int32_t>;
16+
17+
template <typename T> static void PrintValue(const T &value, std::ostream &os);
18+
19+
template <> void PrintValue(const std::int32_t &value, std::ostream &os) {
20+
os << value;
21+
}
22+
23+
template <> void PrintValue(const std::string &value, std::ostream &os) {
24+
os << "\"" << value << "\"";
25+
}
26+
27+
template <> void PrintValue(const VectorTy &value, std::ostream &os) {
28+
os << "[";
29+
bool first = true;
30+
for (auto element : value) {
31+
if (first) {
32+
first = false;
33+
} else {
34+
os << ",";
35+
}
36+
os << "\n " << element;
37+
}
38+
if (!value.empty()) {
39+
os << "\n ";
40+
}
41+
os << "]";
42+
}
43+
44+
template <typename T>
45+
static void PrintOptionalValue(const REntry &entry, std::string_view name,
46+
std::ostream &os, bool last = false) {
47+
auto &value = *entry.GetPtr<std::optional<T>>(name);
48+
os << " \"" << name << "\": ";
49+
if (!value.has_value()) {
50+
os << "null";
51+
} else {
52+
PrintValue(*value, os);
53+
}
54+
if (!last) {
55+
os << ",";
56+
}
57+
os << "\n";
58+
}
59+
60+
void read(std::string_view input = "types.optional.root",
61+
std::string_view output = "types.optional.json") {
62+
std::ofstream os(std::string{output});
63+
os << "[\n";
64+
65+
auto reader = RNTupleReader::Open("ntpl", input);
66+
auto &entry = reader->GetModel().GetDefaultEntry();
67+
bool first = true;
68+
for (auto index : *reader) {
69+
reader->LoadEntry(index);
70+
71+
if (first) {
72+
first = false;
73+
} else {
74+
os << ",\n";
75+
}
76+
os << " {\n";
77+
78+
PrintOptionalValue<std::int32_t>(entry, "Int32", os);
79+
PrintOptionalValue<std::string>(entry, "String", os);
80+
PrintOptionalValue<VectorTy>(entry, "Vector", os, /*last=*/true);
81+
82+
os << " }";
83+
// Newline is intentionally missing, may need to print a comma before the
84+
// next entry.
85+
}
86+
os << "\n";
87+
os << "]\n";
88+
}

types/optional/write.C

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

0 commit comments

Comments
 (0)