-
Notifications
You must be signed in to change notification settings - Fork 0
/
aux.d
312 lines (256 loc) · 7.87 KB
/
aux.d
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import std.traits;
import std.typetuple;
import hdf5.hdf5;
private
{
alias AllowedTypes = TypeTuple!(float, int, double);
enum string[] VectorHdf5Types =
[
"H5T_NATIVE_FLOAT",
"H5T_NATIVE_INT",
"H5T_NATIVE_DOUBLE",
];
template typeToHdf5Type(T)
{
alias U = Unqual!T;
enum index = staticIndexOf!(U, AllowedTypes);
static if (index == -1)
{
static assert(false, "Could not use " ~ T.stringof ~ ", there is no corresponding hdf5 data type");
}
else
{
enum typeToHdf5Type = VectorHdf5Types[index];
}
}
}
struct DataAttribute
{
hid_t type;
size_t offset;
string varName;
}
struct DataSpecification(Data)
{
alias DataType = Data;
@disable this();
static make()
{
alias TT = FieldTypeTuple!Data;
DataAttribute[] attributes;
// Создаем атрибуты
hid_t createStruct(D)(ref DataAttribute[] attributes) if(is(Data == struct))
{
auto tid = H5Tcreate (H5T_class_t.H5T_COMPOUND, D.sizeof);
foreach (member; __traits(allMembers, D))
{
enum fullName = "D." ~ member;
enum hdf5Name = fullyQualifiedName!D ~ "." ~ member;
mixin("alias T = typeof(" ~ fullName ~ ");");
static if (staticIndexOf!(T, TT) != -1)
{
static if(is(T == enum))
{
static assert(is(T : int), "hdf5 supports only enumeration based on integer type.");
// Create enum type
alias BaseEnumType = OriginalType!T;
mixin("hid_t hdf5Type = H5Tenum_create (" ~ typeToHdf5Type!BaseEnumType ~ ");");
foreach (enumMember; EnumMembers!T)
{
auto val = enumMember;
auto status = H5Tenum_insert (hdf5Type, enumMember.stringof, &val);
assert(status >= 0);
}
// Add the attribute
mixin("string varName = \"" ~ hdf5Name ~ "\";");
mixin("enum offset = D." ~ member ~ ".offsetof;");
attributes ~= DataAttribute(hdf5Type, offset, varName);
}
else static if(is(T == struct))
{
DataAttribute[] da;
auto hdf5Type = createStruct!T(da);
insertAttributes(hdf5Type, da);
mixin("string varName = \"" ~ hdf5Name ~ "\";");
mixin("enum offset = D." ~ member ~ ".offsetof;");
attributes ~= DataAttribute(hdf5Type, offset, varName);
}
else
{
mixin("hid_t hdf5Type = " ~ typeToHdf5Type!T ~ ";");
mixin("string varName = \"" ~ hdf5Name ~ "\";");
mixin("enum offset = D." ~ member ~ ".offsetof;");
attributes ~= DataAttribute(hdf5Type, offset, varName);
}
}
}
return tid;
}
// Insert attributes of the structure into the datatype
auto insertAttributes(hid_t hdf5Type, DataAttribute[] da)
{
foreach(attr; da)
{
auto status = H5Tinsert(hdf5Type, attr.varName.ptr, attr.offset, attr.type);
assert(status >= 0);
}
}
auto tid = createStruct!Data(attributes);
insertAttributes(tid, attributes);
return DataSpecification!Data(tid, attributes);
}
this(const(hid_t) tid, DataAttribute[] attributes)
{
_tid = tid;
_attributes = attributes;
}
~this()
{
H5Tclose(_tid);
}
auto tid() const
{
return _tid;
}
private:
DataAttribute[] _attributes;
immutable hid_t _tid;
const(Data*) _data_ptr;
}
struct Dataset(Data)
{
this(ref const(H5File) file, string name, ref const(DataSpace) space)
{
_data_spec = DataSpecification!Data.make();
_dataset = H5Dcreate2(file._file, name.ptr, _data_spec.tid, space._space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
assert(_dataset >= 0);
}
this(ref const(H5File) file, string name)
{
_data_spec = DataSpecification!Data.make();
_dataset = H5Dopen2(file._file, name.ptr, H5P_DEFAULT);
assert(_dataset >= 0);
}
/*
* Wtite data to the dataset;
*/
auto write(ref Data data)
{
auto status = H5Dwrite(_dataset, _data_spec.tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &data);
assert(status >= 0);
}
/*
* Read data from the dataset.
*/
auto read(ref Data data)
{
auto status = H5Dread(_dataset, _data_spec.tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &data);
assert(status >= 0);
}
~this()
{
H5Dclose(_dataset);
}
private:
hid_t _dataset;
DataSpecification!Data _data_spec;
}
struct DataSpace
{
this(int rank, hsize_t[] dim)
{
_space = H5Screate_simple(rank, dim.ptr, null);
assert(_space >= 0);
}
~this()
{
H5Sclose(_space);
}
private:
hid_t _space;
}
struct H5File
{
@disable this();
enum Access
{
ReadOnly = H5F_ACC_RDONLY, /*absence of rdwr => rd-only */
ReadWrite = H5F_ACC_RDWR, /*open for read and write */
Trunc = H5F_ACC_TRUNC, /*overwrite existing files */
Exclude = H5F_ACC_EXCL, /*fail if file already exists*/
Debug = H5F_ACC_DEBUG, /*print debug info */
Create = H5F_ACC_CREAT, /*create non-existing files */
};
this(string filename, uint flags, hid_t fapl_id = H5P_DEFAULT, hid_t fcpl_id = H5P_DEFAULT)
{
// remove Access.Debug flag if any
auto f = flags & ~Access.Debug;
if(((f == Access.Trunc) && (f != Access.Exclude)) ||
((f != Access.Trunc) && (f == Access.Exclude)))
{
_file = H5Fcreate(filename.ptr, flags, fcpl_id, fapl_id);
assert(_file >= 0);
}
else
if(((f == Access.ReadOnly) && (f != Access.ReadWrite)) ||
((f != Access.ReadOnly) && (f == Access.ReadWrite)))
{
_file = H5Fopen(filename.ptr, flags, fapl_id);
assert(_file >= 0);
}
else
assert(0, "Unknown flags combination.");
}
~this()
{
/*
* Release resources
*/
H5Fclose(_file);
}
private:
hid_t _file;
}
void main()
{
import std.stdio;
enum RANK = 1;
enum LENGTH = 1;
hsize_t[1] dim = [ LENGTH ]; /* Dataspace dimensions */
string filename = "autocompound.h5";
string datasetName = "dataset";
H5open();
struct Bar
{
int i;
float f;
double d;
}
enum TestEnum { a, b, c, d }
static struct Foo
{
int i;
float f;
double d;
TestEnum test_enum;
float f2;
TestEnum test_enum2;
Bar bar;
}
Bar bar = Bar(123, 12.3, 1.23);
Foo foo = Foo(17, 9., 0.197, TestEnum.d, 0.3, TestEnum.c, bar);
Foo foor;
{
auto space = DataSpace(RANK, dim);
auto file = H5File(filename, H5File.Access.Trunc);
auto dataset = Dataset!Foo(file, datasetName, space);
dataset.write(foo);
}
{
auto file = H5File(filename, H5File.Access.ReadOnly);
auto dataset = Dataset!Foo(file, datasetName);
dataset.read(foor);
writeln(foor);
assert(foor == foo);
}
}