Skip to content

Commit a438fe6

Browse files
committed
Add test for std::unique_ptr
Closes #16
1 parent 9b6cf61 commit a438fe6

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed

types/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
* [`fundamental`](fundamental): fundamental column types
44
* [`optional`](optional): `std::optional` with different element types
55
* [`string`](string): `std::string` with all `[Split]Index{32,64}` column types
6+
* [`unique_ptr`](unique_ptr): `std::unique_ptr` with different element types
67
* [`variant`](variant): `std::variant` with `Switch` column type
78
* [`vector`](vector): `std::vector` with all `[Split]Index{32,64}` column types

types/unique_ptr/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# `std::unique_ptr`
2+
3+
## Fields
4+
5+
* `Int32`: `std::int32_t`
6+
* `String`: `std::string`
7+
* `Variant`: `std::variant<std::int32_t, std::string>`
8+
* `Vector`: `std::vector<std::int32_t>`
9+
10+
with the default column types.
11+
12+
## Entries
13+
14+
1. Simple values
15+
2. No value
16+
3. Zero / empty values

types/unique_ptr/read.C

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

types/unique_ptr/write.C

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 <memory>
11+
#include <string>
12+
#include <string_view>
13+
#include <variant>
14+
#include <vector>
15+
16+
using VariantTy = std::variant<std::int32_t, std::string>;
17+
using VectorTy = std::vector<std::int32_t>;
18+
19+
void write(std::string_view filename = "types.unique_ptr.root") {
20+
auto model = RNTupleModel::Create();
21+
22+
auto Int32 = model->MakeField<std::unique_ptr<std::int32_t>>("Int32");
23+
auto String = model->MakeField<std::unique_ptr<std::string>>("String");
24+
auto Variant = model->MakeField<std::unique_ptr<VariantTy>>("Variant");
25+
auto Vector = model->MakeField<std::unique_ptr<VectorTy>>("Vector");
26+
27+
RNTupleWriteOptions options;
28+
options.SetCompression(0);
29+
auto writer =
30+
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);
31+
32+
// First entry: simple values
33+
*Int32 = std::make_unique<std::int32_t>(1);
34+
*String = std::make_unique<std::string>("abc");
35+
*Variant = std::make_unique<VariantTy>("def");
36+
*Vector = std::make_unique<VectorTy>();
37+
**Vector = {1, 2, 3};
38+
writer->Fill();
39+
40+
// Second entry: no value
41+
Int32->reset();
42+
String->reset();
43+
Variant->reset();
44+
Vector->reset();
45+
writer->Fill();
46+
47+
// Third entry: zero / empty value
48+
*Int32 = std::make_unique<std::int32_t>(0);
49+
*String = std::make_unique<std::string>("");
50+
*Variant = std::make_unique<VariantTy>(0);
51+
*Vector = std::make_unique<VectorTy>();
52+
writer->Fill();
53+
}

0 commit comments

Comments
 (0)