|
H5CPP
v1.14.0
Modern C++ templates for HDF5 serial and parallel I/O
|
|
This example shows the small core of H5CPP: typed descriptors, RAII-managed HDF5 handles, composable property lists, error handling, file creation, and dataset creation. The point is simple: keep the HDF5 C API available, but make the common path look like C++.
| File | Purpose |
|---|---|
basic.cpp | Demonstrates basic H5CPP operations |
001.h5 | Minimal file creation example |
002.h5 | File creation with custom creation properties |
003.h5 | File creation using raw HDF5 default property lists |
004.h5 | Dataset creation examples |
h5cpp/all pulls in the high-level H5CPP API, wrappers, property-list helpers, and type mappings.
H5CPP maps C++ types to HDF5 datatype descriptors through templates.
h5::dt_t<int> creates a managed copy of H5T_NATIVE_INT.
The descriptor behaves like the other H5CPP handles:
hid_t for C API interop;hid_t must be treated as borrowed, not manually closed.Types also have compile-time and runtime names:
For custom types, register a mapping:
For POD compound types, this is usually generated by the H5CPP compiler.
Property lists are ordinary H5CPP objects, but small configuration fragments can be composed with |.
They can also be merged:
Common examples:
Defaults are available as h5::default_fcpl, h5::default_fapl, h5::default_lcpl, h5::default_dcpl, and similar.
HDF5 prints its own diagnostic stack by default. For expected failures, mute it temporarily and catch the H5CPP exception.
This keeps intentional error tests from spraying noise onto stderr.
All H5CPP handles own HDF5 resources.
When fd leaves scope, H5CPP calls the matching HDF5 close function. The hid_t cast exists for C API interop. Treat it as a managed reference.
Basic file creation:
Create with file creation properties:
You can also pass raw HDF5 property-list identifiers:
The file handle closes automatically when it leaves scope.
Create a dataset with explicit current dimensions, maximum dimensions, link creation properties, dataset creation properties, and dataset access properties:
h5::create_path creates missing intermediate groups. h5::utf8 marks link names as UTF-8.
The same operation can be written with a named dataset creation property list:
Or with defaults made explicit:
If only max_dims is given, H5CPP derives the current dimensions from it. For unlimited dimensions, the current size starts at zero:
This creates a dataset with logical shape:
That is useful for append-style storage such as packet tables.
The basic H5CPP workflow is:
That is the core idea:
No manual H5Tclose, H5Sclose, H5Pclose, H5Dclose, or H5Fclose on the normal path.
basics.cpp — rendered with syntax highlighting