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<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<int>() {
40  return H5T_NATIVE_INT; // LCOV_EXCL_LINE
41 }
42 template <>
43 SPECTRE_ALWAYS_INLINE hid_t h5_type<unsigned int>() {
44  return H5T_NATIVE_UINT; // LCOV_EXCL_LINE
45 }
46 template <>
47 SPECTRE_ALWAYS_INLINE hid_t h5_type<long>() {
48  return H5T_NATIVE_LONG; // LCOV_EXCL_LINE
49 }
50 template <>
51 SPECTRE_ALWAYS_INLINE hid_t h5_type<unsigned long>() {
52  return H5T_NATIVE_ULONG; // LCOV_EXCL_LINE
53 }
54 template <>
55 SPECTRE_ALWAYS_INLINE hid_t h5_type<long long>() {
56  return H5T_NATIVE_LLONG; // LCOV_EXCL_LINE
57 }
58 template <>
59 SPECTRE_ALWAYS_INLINE hid_t h5_type<unsigned long long>() {
60  return H5T_NATIVE_ULLONG; // LCOV_EXCL_LINE
61 }
62 template <>
63 SPECTRE_ALWAYS_INLINE hid_t h5_type<char>() {
64  return H5T_NATIVE_CHAR; // LCOV_EXCL_LINE
65 }
66 template <>
67 SPECTRE_ALWAYS_INLINE hid_t h5_type<std::string>() {
68  hid_t datatype = H5Tcopy(H5T_C_S1);
69  CHECK_H5(datatype, "Failed to allocate string.");
70 #pragma GCC diagnostic push
71 #pragma GCC diagnostic ignored "-Wold-style-cast"
72  CHECK_H5(H5Tset_size(datatype, H5T_VARIABLE),
73  "Failed to set size of string.");
74 #pragma GCC diagnostic pop
75  return datatype;
76 }
77 /// \endcond
78 
79 /*!
80  * \ingroup HDF5Group
81  * \brief Create an H5 FORTRAN string.
82  */
84  hid_t datatype = H5Tcopy(H5T_FORTRAN_S1);
85  CHECK_H5(datatype, "Failed to allocate string.");
86 #pragma GCC diagnostic push
87 #pragma GCC diagnostic ignored "-Wold-style-cast"
88  CHECK_H5(H5Tset_size(datatype, H5T_VARIABLE),
89  "Failed to set size of string.");
90 #pragma GCC diagnostic pop
91  return datatype;
92 }
93 } // namespace h5
CheckH5.hpp
h5::h5_type
hid_t h5_type()
Returns the HDF5 datatype for the corresponding type T
SPECTRE_ALWAYS_INLINE
#define SPECTRE_ALWAYS_INLINE
Definition: ForceInline.hpp:16
CHECK_H5
#define CHECK_H5(h5_status, m)
Check if an HDF5 operation was successful.
Definition: CheckH5.hpp:20
h5
Contains functions and classes for manipulating HDF5 files.
Definition: AccessType.hpp:18
ForceInline.hpp
h5::fortran_string
hid_t fortran_string() noexcept
Create an H5 FORTRAN string.
Definition: Type.hpp:83
type_traits
string