Skip to content

Fix complex scalar attribute read and add std::variant attribute support#41

Merged
Wentzell merged 4 commits into
unstablefrom
attribute-variant-and-complex-fix
Jun 9, 2026
Merged

Fix complex scalar attribute read and add std::variant attribute support#41
Wentzell merged 4 commits into
unstablefrom
attribute-variant-and-complex-fix

Conversation

@Wentzell

@Wentzell Wentzell commented Jun 9, 2026

Copy link
Copy Markdown
Member

Summary

Three small attribute-side improvements to the HDF5 wrappers:

  • Fix std::complex scalar attribute read. array_interface::write_attribute writes complex scalars with rank 1 (trailing-2 FLOAT convention), but read_attribute hard-coded rank == 0 and threw on read. The rank check now matches the view's rank, making the round-trip symmetric.
  • Add std::variant attribute support. New h5_write_attribute / h5_read_attribute overloads mirror the existing dataset-side dispatch: write visits the active alternative; read recovers it by matching the attribute's HDF5 datatype against each variant member via hdf5_type_equal. A small get_hdf5_type_attribute helper is added to object.{hpp,cpp} so stl/variant.hpp stays free of HDF5 headers.
  • Inline h5_read_variant_helper. Replace the recursive detail::h5_read_variant_helper in the dataset-side h5_read with an inline short-circuit fold over the variant alternatives, matching the form already used by h5_read_attribute. No behavior change.

Tests in test/c++/h5_complex.cpp and test/c++/h5_variant.cpp were extended accordingly.

Wentzell added 3 commits June 9, 2026 10:44
array_interface::write_attribute writes a std::complex scalar with rank
1 (trailing-2 FLOAT convention), but read_attribute hard-coded rank == 0
and threw on read. Match read_attribute's rank check to the view's rank.

Assisted-by: Claude <noreply@anthropic.com>
Add h5_write_attribute / h5_read_attribute overloads for std::variant,
mirroring the existing dataset-side dispatch: write visits the active
alternative, read recovers it by matching the attribute's HDF5 datatype
against each variant member via hdf5_type_equal.

A small get_hdf5_type_attribute helper is added to object.{hpp,cpp} so
the templated variant header stays free of HDF5 headers.

Assisted-by: Claude <noreply@anthropic.com>
Replace the recursive detail::h5_read_variant_helper with an inline
short-circuit fold over the variant alternatives, matching the form
already used by the attribute-side h5_read_attribute.

Assisted-by: Claude <noreply@anthropic.com>
@Wentzell Wentzell requested a review from Copilot June 9, 2026 15:21
@Wentzell Wentzell requested review from lukas-weber and removed request for Copilot June 9, 2026 15:21

@lukas-weber lukas-weber left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, but if there is a simple way to get a string representation of the allowed types, it could be helpful to add to the error message.

Comment thread c++/h5/stl/variant.hpp Outdated
@Wentzell Wentzell force-pushed the attribute-variant-and-complex-fix branch from a32008e to 76f281e Compare June 9, 2026 16:13
When h5_read / h5_read_attribute for std::variant fails to match the
stored HDF5 datatype against any alternative, include the names of the
allowed types (via the existing get_name_of_h5_type helper) in the
thrown error message to make the diagnostic actionable.

Assisted-by: Claude <noreply@anthropic.com>
@Wentzell Wentzell force-pushed the attribute-variant-and-complex-fix branch from 76f281e to f56c751 Compare June 9, 2026 16:16
@Thoemi09

Thoemi09 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Looks good!

@Wentzell Wentzell merged commit 9f77be5 into unstable Jun 9, 2026
4 checks passed
@Wentzell Wentzell deleted the attribute-variant-and-complex-fix branch June 9, 2026 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants