forked from Dr15Jones/root_serialization
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathHDFCxx.h
More file actions
203 lines (184 loc) · 5.67 KB
/
HDFCxx.h
File metadata and controls
203 lines (184 loc) · 5.67 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#if !defined(HDFCxx_h)
#define HDFCxx_h
#include "hdf5.h"
#include <stdexcept>
#include <vector>
namespace cce::tf::hdf5 {
inline hid_t string_type() {
auto atype = H5Tcopy(H5T_C_S1);
H5Tset_size(atype, H5T_VARIABLE);
H5Tset_cset(atype, H5T_CSET_UTF8);
H5Tset_strpad(atype,H5T_STR_NULLTERM);
return atype;
}
template<typename T> constexpr hid_t H5filetype_for = -1;
template<> inline hid_t H5filetype_for<int> = H5T_STD_I32LE;
template<> inline hid_t H5filetype_for<unsigned int> = H5T_STD_U32LE;
template<> inline hid_t H5filetype_for<char> = H5T_STD_I8LE;
template<> inline hid_t H5filetype_for<size_t> = H5T_STD_U64LE;
template<> inline hid_t H5filetype_for<unsigned long long> = H5T_STD_U64LE;
template<typename T> constexpr hid_t H5memtype_for = -1;
template<> inline hid_t H5memtype_for<int> = H5T_NATIVE_INT;
template<> inline hid_t H5memtype_for<unsigned int> = H5T_NATIVE_UINT;
template<> inline hid_t H5memtype_for<char> = H5T_NATIVE_CHAR;
template<> inline hid_t H5memtype_for<size_t> = H5T_NATIVE_ULLONG;
template<> inline hid_t H5memtype_for<unsigned long long> = H5T_NATIVE_ULLONG;
template<> inline hid_t H5memtype_for<std::string> = string_type();
//wrapper for File
class File {
public:
static File create(const char *name) {
return File(H5Fcreate(name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT));
}
static File open(const char *name) {
return File(H5Fopen(name, H5F_ACC_RDONLY, H5P_DEFAULT));
}
~File() {
H5Fclose(file_);
}
operator hid_t() const {return file_;}
private:
explicit File(hid_t fid):file_(fid) {
if (file_ < 0) {
throw std::runtime_error("Unable to create/open the file\n");
}
}
hid_t file_;
};
//wrapper for Group
class Group {
public:
static Group create(hid_t id, const char *name) {
return Group(H5Gcreate2(id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT));
}
static Group open(hid_t id, const char *name) {
return Group(H5Gopen2(id, name, H5P_DEFAULT));
}
~Group() {
H5Gclose(group_);
}
operator hid_t() const {return group_;}
private:
explicit Group(hid_t gid):group_(gid) {
if (group_ < 0) {
throw std::runtime_error("Unable to create/open the group\n");
}
}
hid_t group_;
};
//wrapper for Dataset
class Dataset {
public:
template<typename T>
static Dataset create(hid_t id, const char *name, hid_t space_id, hid_t dcpl_id) {
return Dataset(H5Dcreate2(id, name, H5filetype_for<T>, space_id, H5P_DEFAULT, dcpl_id, H5P_DEFAULT));
}
static Dataset open(hid_t id, const char *name){
return Dataset(H5Dopen2(id, name, H5P_DEFAULT));}
~Dataset() {
H5Dclose(dataset_);
}
operator hid_t() const {return dataset_;}
template<typename T>
auto
write(hid_t dspace, hid_t filespace, std::vector<T> const & data) {
return H5Dwrite(dataset_, H5memtype_for<T>, dspace, filespace, H5P_DEFAULT, &data[0]);
}
auto set_extent(hsize_t const *dims) {
auto err = H5Dset_extent(dataset_, dims);
if (err < 0) {
throw std::runtime_error("Unable to extend the dataset\n");
}
}
private:
explicit Dataset(hid_t ds_id):dataset_(ds_id) {
if (dataset_ < 0) {
throw std::runtime_error("Unable to create/open the dataset\n");
}
}
hid_t dataset_;
};
//wrapper for Attributes
class Attribute {
public:
template<typename T>
static Attribute create(hid_t id, const char *name, hid_t aid) {
return Attribute(H5Acreate2(id, name, H5memtype_for<T>, aid, H5P_DEFAULT, H5P_DEFAULT));
}
static Attribute open(hid_t id, const char *name){
return Attribute(H5Aopen(id, name, H5P_DEFAULT));
}
~Attribute() {
H5Aclose(attribute_);
}
operator hid_t() const {return attribute_;}
template<typename T>
auto write(T const & data){
return H5Awrite(attribute_, H5memtype_for<T>, &data);
}
private:
explicit Attribute(hid_t a_id):attribute_(a_id) {
if (attribute_ < 0) {
throw std::runtime_error("Unable to create/open the attribute\n");
}
}
hid_t attribute_;
};
//wrapper for Dataspace
//
class Dataspace {
public:
static Dataspace create_simple(hsize_t ndims, hsize_t const *dims, hsize_t const *maxdims) {
return Dataspace(H5Screate_simple(ndims, dims, maxdims));
}
static Dataspace get_space(hid_t hid) {
return Dataspace(H5Dget_space(hid));
}
static Dataspace create_scalar() {
return Dataspace(H5Screate(H5S_SCALAR));
}
auto select_hyperslab(hsize_t const *dims, hsize_t const *slab) {
auto err = H5Sselect_hyperslab(dspace_, H5S_SELECT_SET, dims, NULL, slab, NULL);
if (err < 0) {
throw std::runtime_error("Unable to select hyperslab\n");
}
}
~Dataspace() {
H5Sclose(dspace_);
}
operator hid_t() const {return dspace_;}
private:
explicit Dataspace(hid_t s):dspace_(s) {
if (dspace_ < 0) {
throw std::runtime_error("Unable to create/open the dataspace\n");
}
}
hid_t dspace_;
};
//wrapper for Propertylist
//
class Property {
public:
static Property create() {
return Property(H5Pcreate(H5P_DATASET_CREATE));
}
void set_chunk(hsize_t ndims, hsize_t const *dims) {
auto err = H5Pset_chunk(prop_, ndims, dims);
if (err < 0) {
throw std::runtime_error("Unable to set chunk size\n");
}
}
~Property() {
H5Pclose(prop_);
}
operator hid_t() const {return prop_;}
private:
explicit Property(hid_t p):prop_(p) {
if (prop_ < 0) {
throw std::runtime_error("Unable to create property\n");
}
}
hid_t prop_;
};
}
#endif