Skip to content

Commit 850210c

Browse files
committed
Add test for std::unique_ptr
Closes #16
1 parent b57c187 commit 850210c

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

types/README.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# `std::unique_ptr`
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. Zero / empty values

types/unique_ptr/read.C

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

types/unique_ptr/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 <string>
16+
#include <string_view>
17+
#include <vector>
18+
19+
using VectorTy = std::vector<std::int32_t>;
20+
21+
void write(std::string_view filename = "types.unique_ptr.root") {
22+
auto model = RNTupleModel::Create();
23+
24+
auto Int32 = model->MakeField<std::unique_ptr<std::int32_t>>("Int32");
25+
auto String = model->MakeField<std::unique_ptr<std::string>>("String");
26+
auto Vector = model->MakeField<std::unique_ptr<VectorTy>>("Vector");
27+
28+
RNTupleWriteOptions options;
29+
options.SetCompression(0);
30+
auto writer =
31+
RNTupleWriter::Recreate(std::move(model), "ntpl", filename, options);
32+
33+
// First entry: simple values
34+
*Int32 = std::make_unique<std::int32_t>(1);
35+
*String = std::make_unique<std::string>("abc");
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+
Vector->reset();
44+
writer->Fill();
45+
46+
// Third entry: zero / empty value
47+
*Int32 = std::make_unique<std::int32_t>(0);
48+
*String = std::make_unique<std::string>("");
49+
*Vector = std::make_unique<VectorTy>();
50+
writer->Fill();
51+
}

0 commit comments

Comments
 (0)