|
H5CPP
v1.14.0
Modern C++ templates for HDF5 serial and parallel I/O
|
|
Templated attribute I/O on any HDF5 parent that can carry metadata — file, group, dataset, opaque object, committed datatype.
Attributes are small named metadata items attached to a parent HDF5 object. They live in the parent's object header (not as standalone datasets), do not chunk, and do not support partial I/O — each attribute is read and written in a single shot.
Use attributes for things that describe the data (units, labels, provenance, schema versions, small calibration constants) — not for the data itself. If the value is large, changes over time, or needs slicing, use a dataset instead.
| Operation | Function / syntax | Returns |
|---|---|---|
| Allocate | h5::create<T>(parent, name, args...) | h5::at_t |
| Open existing | h5::open(parent, name, acpl?) | h5::at_t |
Read into T | h5::aread<T>(parent, name, acpl?) | T |
| Write a value | h5::awrite(parent, name, value, acpl?) | h5::at_t |
| Write a brace-list | h5::awrite(parent, name, {a, b, c}, acpl?) | h5::at_t |
| Delete by name | h5::adelete(parent, name) | void |
| Bracket-syntax write | parent["name"] = value | h5::at_t |
| Bracket-syntax read | T v = parent["name"] | T |
All free functions accept any parent satisfying h5::impl::is_valid_attr<P>: raw hid_t, h5::gr_t, h5::ds_t, h5::ob_t, or h5::dt_t<T>. Typed h5::fd_t is not in the trait — pass static_cast<::hid_t>(fd) to attach to the file root.
The bracket-syntax forms live on h5::ds_t, h5::gr_t, and h5::ob_t.
h5::create<T> — allocate a new attributeAllocates the on-disk slot for an attribute of element type T. The value itself is later deposited with h5::awrite or read with h5::aread<T>. Most call sites skip h5::create and let h5::awrite create-on-demand; explicit h5::create is useful when the shape is non-default or the slot must exist before any value is written.
Parameters
| Name | Type | Description |
|---|---|---|
parent | hid_t / gr_t / ds_t / ob_t / dt_t<T> | Open parent handle (compile-time enforced via is_valid_attr). |
path | const std::string& | Attribute name (UTF-8). |
args... | variadic | h5::current_dims (shape; defaults to scalar) and/or h5::acpl_t (ACPL). |
Returns — h5::at_t RAII handle.
Throws — h5::error::io::attribute::create on H5Acreate2 failure; h5::error::property_list::misc on invalid ACPL.
Example
h5::open — open an existing attributeReturns an RAII-managed h5::at_t suitable for passing to h5::aread<T> or the low-level h5::awrite(at_t, T*) form.
Throws — h5::error::io::attribute::open (not present, parent invalid, bad ACPL).
Example
h5::aread<T> — read attribute value as TT determines how the on-disk value is materialised — see Supported Types. HDF5 has no fixed-length ↔ VLEN string conversion, so a fixed-length string attribute reads into std::string or char[N] but not into std::vector<std::string> unless the on-disk type is VLEN.
Throws — h5::error::io::attribute::open if the attribute is not present; h5::error::io::attribute::read on H5Aread failure.
Example
h5::awrite — write a value as an attributeShape is derived from ref via access_traits_t<T>::size. Attributes do not chunk and do not support partial I/O.
Throws — h5::error::io::attribute::write on H5Awrite failure; h5::error::io::attribute::create if create-on-demand failed.
Example
h5::adelete — remove an attributeRemoves the named attribute from parent. The operation is final — there is no rollback once H5Adelete has succeeded.
Throws — h5::error::io::attribute::delete_ on failure.
Example
h5::ds_t, h5::gr_t, and h5::ob_t expose operator[](const char*) returning a transient h5::at_t carrying the parent handle and attribute name. Combined with at_t::operator=(V) (write) and the templated at_t::operator V() const (read), this gives a compact dict-like API:
Caveats
auto v = gr["x"] deduces at_t, not the attribute value. The implicit conversion needs a concrete LHS type to fire.at_t carries only ds + name; if the parent handle is closed before the conversion runs, the read throws h5::error::io::attribute::read.h5::at_t, parent handles, ACPLh5::error::io::attribute::* hierarchyfd_t is one of the valid parents (with cast)ds_t is the most common attribute parentgr_t is the other common attribute parent