|
H5CPP
v1.14.0
Modern C++ templates for HDF5 serial and parallel I/O
|
|
h5cpp ships a set of operator<< overloads (in h5cpp/H5Uall.hpp) that stream any STL-shaped container — and recursively stream its elements — to a std::ostream. No HDF5 is involved; this example exists purely to make container debugging painless and to back the diagnostics used throughout the other examples.
The whole pretty-print surface arrives by including any of the h5cpp umbrella headers (<h5cpp/all>, <h5cpp/core>).
| File | What it teaches |
|---|---|
pprint.cpp | Sample print of every supported container shape + recursive composition. |
No HDF5 file is written; output is pure stdout.
The dispatch uses Walter Brown's WG21 N4436 feature-detection idiom — each category is selected by which member functions the type exposes, not by listing types one by one.
| # | Detected via | Catches |
|---|---|---|
| 1 | begin() / end() | vector, list, forward_list, deque, set, multiset, unordered_set, unordered_multiset, map, multimap, unordered_map, array |
| 2 | top() / pop() / empty() (no iter) | std::stack, std::priority_queue |
| 3 | front() / pop() / empty() (no iter) | std::queue |
| 4 | std::pair<K, V> | rendered {key:value} |
| 5 | std::tuple<Ts...> | rendered <v0,v1,...,vN> |
Maps fall into category 1 (they're iterable) — but *it returns a pair<const K, V>, so the pair overload kicks in for the elements and you get [{k1:v1},{k2:v2},...].
The stack/queue dispatch is non-destructive: a copy of the adaptor is made, then popped element by element into a buffer. The original is untouched.
H5CPP_CONSOLE_WIDTH = 30)(Set / unordered_set / unordered_map element order varies because the underlying containers are hash-based.)
Long containers truncate at H5CPP_CONSOLE_WIDTH (default 10) with a trailing ", ..." so single-line output stays readable. Override the limit by #define-ing it before including the h5cpp header that pulls in H5Uall.hpp:
The cap is per inserter call: nested containers each truncate at the same width, so a map<string, vector<int>> with 100 outer entries and 100 inner entries shows the first 10 outer keys and within each shown value the first 10 elements.
Several other examples in this directory use the pretty-print operators as their verification output:
examples/optimized/** prints column slices as std::vector<int>.examples/multi-tu/** prints a vector of POD record indices.examples/datasets/** uses it throughout for compact dataset content display.Anywhere you have a container and want a one-liner std::cout << c, the operators work without further setup.
| Limitation | Why it's there |
|---|---|
| Single-line output only. | A printf-style "[" / "]" / "," scheme — no pretty indenting. |
Elements are streamed with their own operator<<. | If your element type has no operator<<, the print won't compile. |
Eigen / linalg types are vetoed via Scalar. | They have Scalar member typedefs that would otherwise match container detection — see H5Uall.hpp. Use the library's own print (std::cout << M) for those. |
| Set/unordered_set ordering is implementation-defined. | Inherited from the container; not something the inserter can control. |
| Target | Status |
|---|---|
examples-pprint | ✔ ok — every category prints, deep nesting renders. |
No external library dependencies.
h5cpp/H5Uall.hpp** — where every operator<< lives, along with H5CPP_CONSOLE_WIDTH and the Walter Brown detection idiom.examples/container/** — the same detection idiom applied to the I/O dispatch surface (Walter Brown for both pretty-print and h5::write / h5::read).examples/datasets/** — relies on these inserters to verify readbacks.pprint.cpp — rendered with syntax highlighting