Skip to content

Commit

Permalink
HDF5: Fix Char Type Matching (#1433)
Browse files Browse the repository at this point in the history
* HDF5: Fix Char Type Matching

In HDF5, there are only the signed and unsigned char type.
The third `char` type is an alias to one or the other, different
to the C/C++ fundamental types.

This tries to fix the type matching order to be platform independent
between ppc64le/aarch64/x86-64.

* Add failing HDF5 test

* loadChunk(): consider equivalent char types for casting

* Doc strings

* newline & comment

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Franz Pöschel <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 17, 2023
1 parent e533776 commit ef8b999
Show file tree
Hide file tree
Showing 7 changed files with 495 additions and 283 deletions.
74 changes: 74 additions & 0 deletions include/openPMD/Datatype.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,41 @@ inline bool isSameInteger(Datatype d)
return false;
}

/**
* Determines if d represents a char type.
*
* @param d An openPMD datatype.
* @return true If d is a scalar char, signed char or unsigned char.
* @return false Otherwise.
*/
constexpr bool isChar(Datatype d)
{
switch (d)
{
case Datatype::CHAR:
case Datatype::SCHAR:
case Datatype::UCHAR:
return true;
default:
return false;
}
}

/**
* Determines if d and T_Char are char types of same representation.
*
* Same representation means that on platforms with signed `char` type, `char`
* and `signed char` are considered to be eqivalent, similarly on platforms
* with unsigned `char` type.
*
* @tparam T_Char A type, as template parameter.
* @param d A type, as openPMD datatype.
* @return true If both types are chars of the same representation.
* @return false Otherwise.
*/
template <typename T_Char>
constexpr bool isSameChar(Datatype d);

/** Comparison for two Datatypes
*
* Besides returning true for the same types, identical implementations on
Expand Down Expand Up @@ -710,6 +745,43 @@ void warnWrongDtype(std::string const &key, Datatype store, Datatype request);

std::ostream &operator<<(std::ostream &, openPMD::Datatype const &);

/**
* Generalizes switching over an openPMD datatype.
*
* Will call the function template found at Action::call< T >(), instantiating T
* with the C++ internal datatype corresponding to the openPMD datatype.
*
* @tparam ReturnType The function template's return type.
* @tparam Action The struct containing the function template.
* @tparam Args The function template's argument types.
* @param dt The openPMD datatype.
* @param args The function template's arguments.
* @return Passes on the result of invoking the function template with the given
* arguments and with the template parameter specified by dt.
*/
template <typename Action, typename... Args>
constexpr auto switchType(Datatype dt, Args &&...args)
-> decltype(Action::template call<char>(std::forward<Args>(args)...));

/**
* Generalizes switching over an openPMD datatype.
*
* Will call the function template found at Action::call< T >(), instantiating T
* with the C++ internal datatype corresponding to the openPMD datatype.
* Ignores vector and array types.
*
* @tparam ReturnType The function template's return type.
* @tparam Action The struct containing the function template.
* @tparam Args The function template's argument types.
* @param dt The openPMD datatype.
* @param args The function template's arguments.
* @return Passes on the result of invoking the function template with the given
* arguments and with the template parameter specified by dt.
*/
template <typename Action, typename... Args>
constexpr auto switchNonVectorType(Datatype dt, Args &&...args)
-> decltype(Action::template call<char>(std::forward<Args>(args)...));

} // namespace openPMD

#if !defined(_MSC_VER)
Expand Down Expand Up @@ -737,3 +809,5 @@ inline bool operator!=(openPMD::Datatype d, openPMD::Datatype e)
/** @}
*/
#endif

#include "openPMD/Datatype.tpp"
Loading

0 comments on commit ef8b999

Please sign in to comment.