Skip to content

Commit

Permalink
Clean up detail namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoemi09 authored and Wentzell committed Apr 24, 2024
1 parent 33bcbb3 commit 893a621
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
18 changes: 11 additions & 7 deletions c++/h5/complex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,25 @@ namespace h5 {
double i;
};

// Type trait to check if a type is std::complex.
template <typename T>
struct _is_complex : std::false_type {};
namespace detail {

// Specialization of h5::_is_complex for std::complex.
template <typename T>
struct _is_complex<std::complex<T>> : std::true_type {};
// Type trait to check if a type is std::complex.
template <typename T>
struct _is_complex : std::false_type {};

// Specialization of h5::_is_complex for std::complex.
template <typename T>
struct _is_complex<std::complex<T>> : std::true_type {};

} // namespace detail

/**
* @ingroup h5_types
* @brief Boolean type trait set to true for std::complex types.
* @tparam T Type to check.
*/
template <typename T>
constexpr bool is_complex_v = _is_complex<T>::value;
constexpr bool is_complex_v = detail::_is_complex<T>::value;

} // namespace h5

Expand Down
30 changes: 17 additions & 13 deletions c++/h5/stl/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,23 @@ namespace h5 {
std::visit([&](auto const &x) { h5_write(g, name, x); }, v);
}

// Helper function to read a std::variant from HDF5.
template <typename VT, typename U, typename... Ts>
void h5_read_variant_helper(VT &v, datatype dt, group g, std::string const &name) {
// finds the correct h5_read recursively
if (hdf5_type_equal(hdf5_type<U>(), dt)) {
v = VT{h5_read<U>(g, name)};
return;
namespace detail {

// Helper function to read a std::variant from HDF5.
template <typename VT, typename U, typename... Ts>
void h5_read_variant_helper(VT &v, datatype dt, group g, std::string const &name) {
// finds the correct h5_read recursively
if (hdf5_type_equal(hdf5_type<U>(), dt)) {
v = VT{h5_read<U>(g, name)};
return;
}
if constexpr (sizeof...(Ts) > 0)
h5_read_variant_helper<VT, Ts...>(v, dt, g, name);
else
throw std::runtime_error("Error in h5_read_variant_helper: Type stored in the variant has no corresponding HDF5 datatype");
}
if constexpr (sizeof...(Ts) > 0)
h5_read_variant_helper<VT, Ts...>(v, dt, g, name);
else
throw std::runtime_error("Error in h5_read_variant_helper: Type stored in the variant has no corresponding HDF5 datatype");
}

} // namespace detail

/**
* @brief Read a std::variant from an HDF5 dataset.
Expand All @@ -90,7 +94,7 @@ namespace h5 {
// assume for the moment, name is a dataset.
dataset ds = g.open_dataset(name);
datatype dt = get_hdf5_type(ds);
h5_read_variant_helper<std::variant<Ts...>, Ts...>(v, dt, g, name);
detail::h5_read_variant_helper<std::variant<Ts...>, Ts...>(v, dt, g, name);
}

/** @} */
Expand Down

0 comments on commit 893a621

Please sign in to comment.