Line data Source code
1 1 : // Distributed under the MIT License.
2 : // See LICENSE.txt for details.
3 :
4 : /// \file
5 : /// Defines function for retrieving the HDF5 type for a template parameter
6 :
7 : #pragma once
8 :
9 : #include <hdf5.h>
10 : #include <string>
11 : #include <type_traits>
12 :
13 : #include "IO/H5/CheckH5.hpp"
14 : #include "Utilities/ForceInline.hpp"
15 :
16 : namespace h5 {
17 : /*!
18 : * \ingroup HDF5Group
19 : * \brief Returns the HDF5 datatype for the corresponding type `T`
20 : *
21 : * \requires `T` is a valid type with a corresponding HDF5 datatype, i.e.
22 : * a fundamental or a std::string
23 : * \returns the HDF5 datatype for the corresponding type `T`
24 : *
25 : * \note For strings, the returned type must be released with H5Tclose
26 : */
27 : template <typename T>
28 1 : SPECTRE_ALWAYS_INLINE hid_t h5_type();
29 : /// \cond HIDDEN_SYMBOLS
30 : template <>
31 : SPECTRE_ALWAYS_INLINE hid_t h5_type<float>() {
32 : return H5T_NATIVE_FLOAT; // LCOV_EXCL_LINE
33 : }
34 : template <>
35 : SPECTRE_ALWAYS_INLINE hid_t h5_type<double>() {
36 : return H5T_NATIVE_DOUBLE; // LCOV_EXCL_LINE
37 : }
38 : template <>
39 : SPECTRE_ALWAYS_INLINE hid_t h5_type<short>() {
40 : return H5T_NATIVE_SHORT; // LCOV_EXCL_LINE
41 : }
42 : template <>
43 : SPECTRE_ALWAYS_INLINE hid_t h5_type<unsigned short>() {
44 : return H5T_NATIVE_USHORT; // LCOV_EXCL_LINE
45 : }
46 : template <>
47 : SPECTRE_ALWAYS_INLINE hid_t h5_type<int>() {
48 : return H5T_NATIVE_INT; // LCOV_EXCL_LINE
49 : }
50 : template <>
51 : SPECTRE_ALWAYS_INLINE hid_t h5_type<unsigned int>() {
52 : return H5T_NATIVE_UINT; // LCOV_EXCL_LINE
53 : }
54 : template <>
55 : SPECTRE_ALWAYS_INLINE hid_t h5_type<long>() {
56 : return H5T_NATIVE_LONG; // LCOV_EXCL_LINE
57 : }
58 : template <>
59 : SPECTRE_ALWAYS_INLINE hid_t h5_type<unsigned long>() {
60 : return H5T_NATIVE_ULONG; // LCOV_EXCL_LINE
61 : }
62 : template <>
63 : SPECTRE_ALWAYS_INLINE hid_t h5_type<long long>() {
64 : return H5T_NATIVE_LLONG; // LCOV_EXCL_LINE
65 : }
66 : template <>
67 : SPECTRE_ALWAYS_INLINE hid_t h5_type<unsigned long long>() {
68 : return H5T_NATIVE_ULLONG; // LCOV_EXCL_LINE
69 : }
70 : template <>
71 : SPECTRE_ALWAYS_INLINE hid_t h5_type<char>() {
72 : // Work around issue https://github.com/sxs-collaboration/spectre/issues/5029
73 : // by always writing/reading signed chars. See issue for more suggestions for
74 : // more permanent solutions.
75 : return H5T_NATIVE_SCHAR; // LCOV_EXCL_LINE
76 : }
77 : template <>
78 : SPECTRE_ALWAYS_INLINE hid_t h5_type<bool>() {
79 : return H5T_NATIVE_HBOOL; // LCOV_EXCL_LINE
80 : }
81 : template <>
82 : SPECTRE_ALWAYS_INLINE hid_t h5_type<std::string>() {
83 : hid_t datatype = H5Tcopy(H5T_C_S1);
84 : CHECK_H5(datatype, "Failed to allocate string.");
85 : #pragma GCC diagnostic push
86 : #pragma GCC diagnostic ignored "-Wold-style-cast"
87 : CHECK_H5(H5Tset_size(datatype, H5T_VARIABLE),
88 : "Failed to set size of string.");
89 : CHECK_H5(H5Tset_cset(datatype, H5T_CSET_ASCII),
90 : "Failed to set string to ASCII encoding.");
91 : CHECK_H5(H5Tset_strpad(datatype, H5T_STR_NULLTERM),
92 : "Failed to set string to null terminate");
93 : #pragma GCC diagnostic pop
94 : return datatype;
95 : }
96 : /// \endcond
97 :
98 : /*!
99 : * \ingroup HDF5Group
100 : * \brief Create an H5 FORTRAN string.
101 : */
102 1 : SPECTRE_ALWAYS_INLINE hid_t fortran_string() {
103 : hid_t datatype = H5Tcopy(H5T_FORTRAN_S1);
104 : CHECK_H5(datatype, "Failed to allocate string.");
105 : #pragma GCC diagnostic push
106 : #pragma GCC diagnostic ignored "-Wold-style-cast"
107 : CHECK_H5(H5Tset_size(datatype, H5T_VARIABLE),
108 : "Failed to set size of string.");
109 : #pragma GCC diagnostic pop
110 : return datatype;
111 : }
112 : } // namespace h5
|