4#ifndef DUNE_FUFEM_HDF5_FILE_HH
5#define DUNE_FUFEM_HDF5_FILE_HH
11#if __has_include(<hdf5.h>)
21 return H5Lexists(c_obj(),
name.c_str(), H5P_DEFAULT);
25 return H5Dopen(c_obj(),
name.c_str(), H5P_DEFAULT);
30 return H5Dcreate(c_obj(),
name.c_str(), datatype, file_space, H5P_DEFAULT,
34 virtual hid_t c_obj() = 0;
35 virtual bool isReadOnly()
const = 0;
38enum class Access { READONLY, READWRITE };
40class File :
public Grouplike {
42 File(
std::string filename, Access access = Access::READWRITE)
43 : readOnly_(access == Access::READONLY) {
51 H5F_libver_t
const minimumVersion = H5F_LIBVER_LATEST;
52 hid_t fapl = H5Pcreate(H5P_FILE_ACCESS);
53 H5Pset_libver_bounds(fapl, minimumVersion, H5F_LIBVER_LATEST);
55 bool const fileExists = stat(filename.
c_str(), &buffer) == 0;
56 bool const createFile = !fileExists;
57 if (createFile && readOnly_)
60 "Tried to open non-existing file in read-only mode: " << filename);
65 createFile ? H5F_ACC_EXCL : (readOnly_ ? H5F_ACC_RDONLY : H5F_ACC_RDWR);
66#if H5_VERSION_GE(1, 10, 0)
72 flags |= H5F_ACC_SWMR_READ;
75 file_ = createFile ? H5Fcreate(filename.
c_str(), flags, H5P_DEFAULT, fapl)
76 : H5Fopen(filename.c_str(), flags, fapl);
81 void flush() { H5Fflush(file_, H5F_SCOPE_LOCAL); }
83 ~File() { H5Fclose(file_); }
85 hid_t c_obj()
override {
return file_; }
86 bool isReadOnly()
const override {
return readOnly_; }
87 bool swmrEnabled() {
return swmrEnabled_; }
92#if H5_VERSION_GE(1, 10, 0)
95 H5Fstart_swmr_write(file_);
97 std::cerr <<
"WARNING: SWMR writing requested for a read-only file"
102 <<
"WARNING: SWMR requested but not supported by this version of HDF5"
109 bool const readOnly_;
110 bool swmrEnabled_ =
false;
113class Group :
public Grouplike {
115 Group(Grouplike &parent,
std::string groupname) : parent_(parent) {
116 group_ = H5Lexists(parent_.c_obj(), groupname.
c_str(), H5P_DEFAULT)
117 ? H5Gopen(parent_.c_obj(), groupname.
c_str(), H5P_DEFAULT)
118 : H5Gcreate(parent_.c_obj(), groupname.c_str(), H5P_DEFAULT,
119 H5P_DEFAULT, H5P_DEFAULT);
122 ~Group() { H5Gclose(group_); }
124 hid_t c_obj()
override {
return group_; }
125 bool isReadOnly()
const override {
return parent_.isReadOnly(); }
134 #warning Including the hdf5/file.hh but hdf5.h is missing.
#define DUNE_THROW(E,...)