/home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/hdf5lib/dataset.hh Source File#

DiFfRG: /home/runner/work/DiFfRG_current/DiFfRG_current/DiFfRG/include/hdf5lib/dataset.hh Source File
DiFfRG
dataset.hh
Go to the documentation of this file.
1#pragma once
2
4#include <hdf5lib/datatype.hh>
6#include <hdf5lib/handle.hh>
7
8#include <string>
9#include <type_traits>
10#include <vector>
11
12namespace DiFfRG::hdf5
13{
14 class Dataset
15 {
16 public:
17 Dataset() = default;
18 explicit Dataset(hid_t id) : h_(id) {}
19 static Dataset take(hid_t id) { return Dataset(Handle::take(id)); }
20
21 hid_t id() const noexcept { return h_.get(); }
22 bool valid() const noexcept { return h_.valid(); }
23
25 {
26 hid_t t = H5Dget_type(h_.get());
27 throw_if_negative(t, "H5Dget_type failed");
28 return Datatype::take(t);
29 }
30
32 {
33 hid_t s = H5Dget_space(h_.get());
34 throw_if_negative(s, "H5Dget_space failed");
35 return Dataspace::take(s);
36 }
37
38 void resize(const Dims &dims)
39 {
40 throw_if_negative(H5Dset_extent(h_.get(), dims.data()), "H5Dset_extent failed");
41 }
42
43 // ----------------- write -----------------
44
47 template <class T>
48 requires(!std::is_same_v<std::decay_t<T>, std::string>)
49 void write(const T &value)
50 {
51 auto type = type_of<std::decay_t<T>>();
52 throw_if_negative(H5Dwrite(h_.get(), type.id(), H5S_ALL, H5S_ALL, H5P_DEFAULT, &value),
53 "H5Dwrite (scalar) failed");
54 }
55
59 template <class T>
60 requires(!std::is_same_v<std::decay_t<T>, std::string>)
61 void write_at(hsize_t offset, const T &value)
62 {
63 auto file_space = dataspace();
64 file_space.select_hyperslab({offset}, {1});
65 hsize_t one = 1;
66 hid_t mem_space = H5Screate_simple(1, &one, nullptr);
67 throw_if_negative(mem_space, "H5Screate_simple (write_at mem) failed");
68 auto type = type_of<std::decay_t<T>>();
69 herr_t e = H5Dwrite(h_.get(), type.id(), mem_space, file_space.id(), H5P_DEFAULT, &value);
70 H5Sclose(mem_space);
71 throw_if_negative(e, "H5Dwrite (write_at) failed");
72 }
73
75 void write_at(hsize_t offset, const std::string &value)
76 {
77 auto file_space = dataspace();
78 file_space.select_hyperslab({offset}, {1});
79 hsize_t one = 1;
80 hid_t mem_space = H5Screate_simple(1, &one, nullptr);
81 throw_if_negative(mem_space, "H5Screate_simple (write_at mem, string) failed");
82 auto type = type_of<std::string>();
83 const char *cstr = value.c_str();
84 herr_t e = H5Dwrite(h_.get(), type.id(), mem_space, file_space.id(), H5P_DEFAULT, &cstr);
85 H5Sclose(mem_space);
86 throw_if_negative(e, "H5Dwrite (write_at, string) failed");
87 }
88
90 void write(const std::string &value)
91 {
92 auto type = type_of<std::string>();
93 const char *cstr = value.c_str();
94 throw_if_negative(H5Dwrite(h_.get(), type.id(), H5S_ALL, H5S_ALL, H5P_DEFAULT, &cstr),
95 "H5Dwrite (string scalar) failed");
96 }
97
99 template <class T>
100 requires(!std::is_same_v<std::decay_t<T>, std::string>)
101 void write(const std::vector<T> &v)
102 {
103 write(v.data(), v.size());
104 }
105
107 void write(const std::vector<std::string> &v)
108 {
109 auto type = type_of<std::string>();
110 auto packed = detail::pack_strings(v.data(), v.size());
111 throw_if_negative(H5Dwrite(h_.get(), type.id(), H5S_ALL, H5S_ALL, H5P_DEFAULT, packed.data()),
112 "H5Dwrite (vector<string>) failed");
113 }
114
118 template <class T>
119 requires(!std::is_same_v<std::decay_t<T>, std::string>)
120 void write(const T *data, std::size_t /*n*/)
121 {
122 auto type = type_of<std::decay_t<T>>();
123 throw_if_negative(H5Dwrite(h_.get(), type.id(), H5S_ALL, H5S_ALL, H5P_DEFAULT, data),
124 "H5Dwrite (raw pointer) failed");
125 }
126
127 // ----------------- read -----------------
128
129 template <class T>
130 requires(!std::is_same_v<std::decay_t<T>, std::string>)
131 void read(T &value)
132 {
133 auto type = type_of<std::decay_t<T>>();
134 throw_if_negative(H5Dread(h_.get(), type.id(), H5S_ALL, H5S_ALL, H5P_DEFAULT, &value),
135 "H5Dread (scalar) failed");
136 }
137
138 void read(std::string &value)
139 {
140 auto type = type_of<std::string>();
141 detail::read_vlen_strings(h_.get(), H5S_ALL, H5S_ALL, type.id(), &value, 1);
142 }
143
144 template <class T>
145 requires(!std::is_same_v<std::decay_t<T>, std::string>)
146 void read(std::vector<T> &v)
147 {
148 read(v.data(), v.size());
149 }
150
151 void read(std::vector<std::string> &v)
152 {
153 auto type = type_of<std::string>();
154 detail::read_vlen_strings(h_.get(), H5S_ALL, H5S_ALL, type.id(), v.data(), v.size());
155 }
156
157 template <class T>
158 requires(!std::is_same_v<std::decay_t<T>, std::string>)
159 void read(T *data, std::size_t /*n*/)
160 {
161 auto type = type_of<std::decay_t<T>>();
162 throw_if_negative(H5Dread(h_.get(), type.id(), H5S_ALL, H5S_ALL, H5P_DEFAULT, data),
163 "H5Dread (raw pointer) failed");
164 }
165
166 private:
167 explicit Dataset(Handle h) : h_(std::move(h)) {}
169 };
170} // namespace DiFfRG::hdf5
Definition dataset.hh:15
void read(std::vector< T > &v)
Definition dataset.hh:146
void read(std::vector< std::string > &v)
Definition dataset.hh:151
void read(T *data, std::size_t)
Definition dataset.hh:159
static Dataset take(hid_t id)
Definition dataset.hh:19
void read(T &value)
Definition dataset.hh:131
void write(const T *data, std::size_t)
Definition dataset.hh:120
Handle h_
Definition dataset.hh:168
Dataspace dataspace() const
Definition dataset.hh:31
void resize(const Dims &dims)
Definition dataset.hh:38
void write(const T &value)
Definition dataset.hh:49
hid_t id() const noexcept
Definition dataset.hh:21
Dataset(Handle h)
Definition dataset.hh:167
bool valid() const noexcept
Definition dataset.hh:22
void write(const std::string &value)
Whole-dataset write of a single string.
Definition dataset.hh:90
void read(std::string &value)
Definition dataset.hh:138
void write_at(hsize_t offset, const T &value)
Definition dataset.hh:61
void write_at(hsize_t offset, const std::string &value)
Variable-length string write into a 1-element selection.
Definition dataset.hh:75
Datatype datatype() const
Definition dataset.hh:24
Dataset(hid_t id)
Definition dataset.hh:18
void write(const std::vector< std::string > &v)
Vector of strings.
Definition dataset.hh:107
void write(const std::vector< T > &v)
Vector write — element count must match the dataset's element count.
Definition dataset.hh:101
Definition dataspace.hh:12
static Dataspace take(hid_t id)
Definition dataspace.hh:16
Definition datatype.hh:12
static Datatype take(hid_t id)
Definition datatype.hh:16
Definition handle.hh:25
bool valid() const noexcept
Definition handle.hh:80
hid_t get() const noexcept
Definition handle.hh:79
static Handle take(hid_t id)
Definition handle.hh:37
std::vector< const char * > pack_strings(const std::string *src, std::size_t n)
Definition vlen_string.hh:14
void read_vlen_strings(hid_t dset, hid_t, hid_t, hid_t mem_type, std::string *dst, std::size_t n)
Definition vlen_string.hh:27
Definition hdf5.hh:12
std::vector< hsize_t > Dims
Definition dataspace.hh:9
Datatype type_of()
Convenience factory — type_of<T>() returns the HDF5 datatype for T.
Definition datatype.hh:56
void throw_if_negative(hid_t id, const char *what)
Definition handle.hh:11