forked from BlueBrain/HighFive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_datatype.cpp
More file actions
118 lines (93 loc) · 3.96 KB
/
Copy pathcreate_datatype.cpp
File metadata and controls
118 lines (93 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <highfive/H5File.hpp>
#include <highfive/H5DataType.hpp>
using namespace HighFive;
static const std::string FILE_NAME("create_datatype_example.h5");
static const std::string DATASET_NAME("test_dataset");
// Struct representation of custom type (type v below)
typedef struct {
char a;
short b;
unsigned long long c;
} csl;
bool operator==(csl x, csl y) {
return x.a == y.a && x.b == y.b && x.c == y.c;
}
bool operator!=(csl x, csl y) {
return !(x == y);
}
// Tell HighFive how to create the HDF5 datatype for this base type by
// using the HIGHFIVE_REGISTER_TYPE macro
CompoundType create_compound_csl() {
return {{"u1", AtomicType<unsigned char>{}},
{"u2", AtomicType<short>{}},
{"u3", AtomicType<unsigned long long>{}}};
}
HIGHFIVE_REGISTER_TYPE(csl, create_compound_csl)
int main(void) {
try {
File file(FILE_NAME, File::ReadWrite | File::Create | File::Truncate);
// Create a simple compound type with automatic alignment of the
// members. For this the type alignment is trivial.
std::vector<CompoundType::member_def> t_members({
{"real", AtomicType<int>{}},
{"imag", AtomicType<int>{}}
});
CompoundType t(t_members);
t.commit(file, "new_type1");
// Create a complex nested datatype with manual alignment
CompoundType u({{"u1", t, 0},
{"u2", t, 9},
{"u3", AtomicType<int>{}, 20}},
26);
u.commit(file, "new_type3");
// Create a more complex type with automatic alignment. For this the
// type alignment is more complex.
CompoundType v_aligned{{"u1", AtomicType<unsigned char>{}},
{"u2", AtomicType<short>{}},
{"u3", AtomicType<unsigned long long>{}}};
// introspect the compound type
std::cout << "v_aligned size: " << v_aligned.getSize();
for (const auto& member : v_aligned.getMembers()) {
std::cout << " field " << member.name << " offset: " << member.offset
<< std::endl;
}
v_aligned.commit(file, "new_type2_aligned");
// Create a more complex type with a fully packed alignment. The
// equivalent type is created with a standard struct alignment in the
// implementation of HighFive::create_datatype above
CompoundType v_packed({{"u1", AtomicType<unsigned char>{}, 0},
{"u2", AtomicType<short>{}, 1},
{"u3", AtomicType<unsigned long long>{}, 3}},
11);
v_packed.commit(file, "new_type2_packed");
// Initialise some data
std::vector<csl> data;
data.push_back({'f', 1, 4});
data.push_back({'g', -4, 18});
// Write the data into the file in a fully packed form
DataSet dataset = file.createDataSet(DATASET_NAME, DataSpace(2), v_packed);
dataset.write(data);
file.flush();
// Read a subset of the data back
std::vector<csl> result;
dataset.select({0}, {2}).read(result);
for(size_t i = 0; i < data.size(); ++i) {
if (result[i] != data[i]) {
std::cout << "result[" << i << "]:" << std::endl;
std::cout << " " << result[i].a << std::endl;
std::cout << " " << result[i].b << std::endl;
std::cout << " " << result[i].c << std::endl;
std::cout << "data[" << i << "]:" << std::endl;
std::cout << " " << data[i].a << std::endl;
std::cout << " " << data[i].b << std::endl;
std::cout << " " << data[i].c << std::endl;
}
}
} catch (const Exception& err) {
// catch and print any HDF5 error
std::cerr << err.what() << std::endl;
return 1;
}
return 0; // successfully terminated
}