|
H5CPP
v1.14.0
Modern C++ templates for HDF5 serial and parallel I/O
|
|
Create, open, traverse, and link HDF5 groups — the directory-like structure inside an HDF5 container. Includes the h5::ls / h5::dfs / h5::bfs traversal helpers and the soft / hard / external link API.
Groups are HDF5's namespace mechanism. They behave like POSIX directories: each group can hold other groups, datasets, attributes, and committed datatypes, addressed by /-separated paths inside the file. The file's root group / is implicit and always present.
Groups in H5CPP are also one of the standard attribute parents — anything you can write as an attribute on a dataset, you can also write on a group (useful for per-group metadata: schema, source, time-range, etc.).
| Operation | Function | Returns |
|---|---|---|
| Create new group | h5::gcreate(parent, path, lcpl?) | h5::gr_t |
| Open existing group | h5::gopen(parent, path) | h5::gr_t |
| Close | RAII (let h5::gr_t destruct) | — |
| Check existence | h5::exists(loc, path) | bool |
| List children | h5::ls(fd, path) | std::vector<std::string> |
| Depth-first traversal | h5::dfs(fd, path) | std::vector<std::string> |
| Breadth-first traversal | h5::bfs(fd, path) | std::vector<std::string> |
| Create soft link | h5::link_soft(target_path, loc, link_name) | void |
| Create hard link | h5::link_hard(target_loc, target_path, loc, link_name) | void |
| Create external link | h5::link_external(target_file, target_path, loc, link_name) | void |
| Move / rename | h5::move(src_loc, src_name, dst_loc, dst_name) | void |
| Copy | h5::copy(src_loc, src_name, dst_loc, dst_name) | void |
| Unlink (delete) | h5::unlink(loc, path) | void |
| Attribute on a group | h5::awrite(gr, "name", value) or gr["name"] = … | h5::at_t |
gcreate and gopen are templated on the parent type via h5::impl::is_valid_group_parent (file h5::fd_t or group h5::gr_t). Nested groups compose naturally — h5::gopen(gr, "sub") returns a gr_t you can pass back as the parent of another gopen / gcreate. The traversal and link APIs take any Loc satisfying h5::impl::is_valid_loc<Loc> (file, group, or other location handle).
h5::gcreate — create a new groupWith the default h5::default_lcpl, intermediate groups along the path are created automatically (e.g. gcreate(fd, "a/b/c") creates a, then a/b, then a/b/c) and link names are UTF-8 encoded.
Throws — h5::error::io::group::create on H5Gcreate2 failure.
h5::gopen — open an existing groupThrows — h5::error::io::group::open if the group does not exist.
h5::exists — does a path exist?Returns true if path resolves to any object (group, dataset, datatype, link) relative to loc. Quiet on missing — returns false rather than throwing.
The lightweight pattern for "does this child exist?" — avoids the exception cost of the call-fail probe.
h5::ls — list children of a groupReturns the names of every direct child of directory (one level deep) in H5_ITER_INC order — same as ls /path on POSIX.
Throws — std::runtime_error on H5Literate failure (parent not present, bad path).
h5::dfs / h5::bfs — recursive traversalRecursive equivalents of ls: return every object's path relative to directory, walked depth-first (dfs) or breadth-first (bfs). Built on H5Lvisit with the matching iteration order.
| Variant | When it wins |
|---|---|
dfs | "Process the whole subtree, leaf-by-leaf" — log analyzers, validators |
bfs | "Find this object by name without descending unnecessarily" — search-style |
h5::link_soft — symbolic linkCreates loc/link_name as a symbolic link to target_path. Resolved at access time — if the target doesn't exist when the link is created (or is later deleted), the link dangles silently until accessed.
Soft links can point to anywhere — including paths that don't yet exist, and across H5O_TYPE boundaries.
Throws — std::runtime_error on H5Lcreate_soft failure.
h5::link_hard — hard link (alias)Creates loc/link_name as a second name for the object at target_loc/target_path. Both names refer to the same HDF5 object — no copy, no indirection, no dangling. Deleting one name leaves the object reachable through the other.
Hard links can't span files (use external links for that) and can't point at groups in some HDF5 versions.
h5::link_external — cross-file referenceCreates loc/link_name as a reference to an object in another HDF5 file. The link is resolved by re-opening the target file at access time. Useful for splitting large datasets across files while keeping a unified fd_t view.
h5::move / h5::copy — rename and duplicateh5::move is H5Lmove — the link is moved without copying the underlying object. Atomic on a single file.
h5::copy is H5Ocopy — the object itself is duplicated. Suitable for cross-file copies; respects HDF5's H5P_OBJECT_COPY property list for filter/chunk preservation.
h5::unlink — remove a linkRemoves the link at loc/path. If it was the last reference to the underlying object (no hard links pointing at it elsewhere), the object itself becomes unreachable and HDF5 reclaims its space on the next file repack.
Note: HDF5 doesn't truly delete data on H5Ldelete — the bytes remain in the file until you repack with h5repack. Plan accordingly for sensitive data.
Anything you can hang on a dataset, you can hang on a group:
This is the canonical pattern for per-section metadata: create a logical group, attach descriptive attributes, then put the heavyweight datasets underneath.
h5::gr_t taxonomyh5::lcpl_t referenceh5::error::io::group::* hierarchyfd_t the typical parent of a top-level group