Type.hpp
Go to the documentation of this file.
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"
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>
29 /// \cond HIDDEN_SYMBOLS
30 template <>
31 SPECTRE_ALWAYS_INLINE hid_t h5_type<double>() {
32  return H5T_NATIVE_DOUBLE; // LCOV_EXCL_LINE
33 }
34 template <>
35 SPECTRE_ALWAYS_INLINE hid_t h5_type<int>() {
36  return H5T_NATIVE_INT; // LCOV_EXCL_LINE
37 }
38 template <>
39 SPECTRE_ALWAYS_INLINE hid_t h5_type<unsigned int>() {
40  return H5T_NATIVE_UINT; // LCOV_EXCL_LINE
41 }
42 template <>
43 SPECTRE_ALWAYS_INLINE hid_t h5_type<long>() {
44  return H5T_NATIVE_LONG; // LCOV_EXCL_LINE
45 }
46 template <>
47 SPECTRE_ALWAYS_INLINE hid_t h5_type<unsigned long>() {
48  return H5T_NATIVE_ULONG; // LCOV_EXCL_LINE
49 }
50 template <>
51 SPECTRE_ALWAYS_INLINE hid_t h5_type<long long>() {
52  return H5T_NATIVE_LLONG; // LCOV_EXCL_LINE
53 }
54 template <>
55 SPECTRE_ALWAYS_INLINE hid_t h5_type<unsigned long long>() {
56  return H5T_NATIVE_ULLONG; // LCOV_EXCL_LINE
57 }
58 template <>
59 SPECTRE_ALWAYS_INLINE hid_t h5_type<char>() {
60  return H5T_NATIVE_CHAR; // LCOV_EXCL_LINE
61 }
62 template <>
63 SPECTRE_ALWAYS_INLINE hid_t h5_type<std::string>() {
64  hid_t datatype = H5Tcopy(H5T_C_S1);
65  CHECK_H5(datatype, "Failed to allocate string.");
66 #pragma GCC diagnostic push
67 #pragma GCC diagnostic ignored "-Wold-style-cast"
68  CHECK_H5(H5Tset_size(datatype, H5T_VARIABLE),
69  "Failed to set size of string.");
70 #pragma GCC diagnostic pop
71  return datatype;
72 }
73 /// \endcond
74 
75 /*!
76  * \ingroup HDF5Group
77  * \brief Create an H5 FORTRAN string.
78  */
80  hid_t datatype = H5Tcopy(H5T_FORTRAN_S1);
81  CHECK_H5(datatype, "Failed to allocate string.");
82 #pragma GCC diagnostic push
83 #pragma GCC diagnostic ignored "-Wold-style-cast"
84  CHECK_H5(H5Tset_size(datatype, H5T_VARIABLE),
85  "Failed to set size of string.");
86 #pragma GCC diagnostic pop
87  return datatype;
88 }
89 } // namespace h5
Contains functions and classes for manipulating HDF5 files.
Definition: AccessType.cpp:10
Defines macro CHECK_H5.
#define SPECTRE_ALWAYS_INLINE
Always inline a function. Only use this if you benchmarked the code.
Definition: ForceInline.hpp:20
hid_t h5_type()
Returns the HDF5 datatype for the corresponding type T
Defines macro to always inline a function.
hid_t fortran_string() noexcept
Create an H5 FORTRAN string.
Definition: Type.hpp:79
#define CHECK_H5(h5_status, m)
Check if an HDF5 operation was successful.
Definition: CheckH5.hpp:20