IODA Bundle
HH-types.cpp
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2020-2021 UCAR
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  */
7 /*! \addtogroup ioda_internals_engines_hh
8  *
9  * @{
10  * \file HH-types.cpp
11  * \brief HDF5 engine implementation of Type.
12  */
13 #include "./HH/HH-types.h"
14 
15 #include <map>
16 
17 #include "./HH/Handles.h"
18 #include "ioda/Types/Type.h"
19 #include "ioda/Exception.h"
20 #include "ioda/defs.h"
21 
22 namespace ioda {
23 namespace detail {
24 namespace Engines {
25 namespace HH {
26 HH_Type::~HH_Type() = default;
27 HH_Type::HH_Type(HH_hid_t h) : handle(h) {}
28 
29 size_t HH_Type::getSize() const {
30  size_t res = H5Tget_size(handle.get());
31  if (res == 0) throw Exception("H5Tget_size failed.", ioda_Here());
32  return res;
33 }
34 
37  static HH_Type_Provider provider;
38  return &provider;
39 }
40 
42  static const std::map<std::type_index, HH_hid_t> fundamental_types
43  = {{typeid(bool), {H5T_NATIVE_HBOOL}},
44  {typeid(short int), {H5T_NATIVE_SHORT}}, // NOLINT
45  {typeid(unsigned short int), {H5T_NATIVE_USHORT}}, // NOLINT
46  {typeid(int), {H5T_NATIVE_INT}}, // NOLINT
47  {typeid(unsigned int), {H5T_NATIVE_UINT}}, // NOLINT
48  {typeid(long int), {H5T_NATIVE_LONG}}, // NOLINT
49  {typeid(unsigned long int), {H5T_NATIVE_ULONG}}, // NOLINT
50  {typeid(long long int), {H5T_NATIVE_LLONG}}, // NOLINT
51  {typeid(unsigned long long int), {H5T_NATIVE_ULLONG}}, // NOLINT
52  {typeid(signed char), {H5T_NATIVE_SCHAR}}, // NOLINT
53  //{typeid(unsigned char), {H5T_NATIVE_U}},
54  {typeid(char), {H5T_NATIVE_CHAR}}, // NOLINT
55  //{typeid(wchar_t), {H5T_NATIVE_HBOOL}},
56  //{typeid(char16_t), {H5T_NATIVE_HBOOL}},
57  //{typeid(char32_t), {H5T_NATIVE_HBOOL}},
58  //{typeid(char8_t), {H5T_NATIVE_HBOOL}},
59  {typeid(float), {H5T_NATIVE_FLOAT}},
60  {typeid(double), {H5T_NATIVE_DOUBLE}},
61  {typeid(long double), {H5T_NATIVE_LDOUBLE}}};
62 
63  if (!fundamental_types.count(type)) throw Exception("HDF5 does not implement this type as a fundamental type.", ioda_Here());
64  return fundamental_types.at(type);
65 }
66 
69  return Type{std::make_shared<HH_Type>(t), type};
70 }
71 
72 /// \todo typeOuter, typeInner
73 Type HH_Type_Provider::makeArrayType(std::initializer_list<Dimensions_t> dimensions,
74  std::type_index typeOuter, std::type_index typeInner) const {
75  /// \todo Allow for arrays of string types!
76  HH_hid_t fundamental_type = getFundamentalHHType(typeInner);
77 
78  std::vector<hsize_t> hdims;
79  for (const auto& d : dimensions) hdims.push_back(gsl::narrow<hsize_t>(d));
80  hid_t t = H5Tarray_create2(fundamental_type(), gsl::narrow<unsigned int>(dimensions.size()),
81  hdims.data());
82  if (t < 0) throw Exception("Failed call to H5Tarray_create2.", ioda_Here());
84 
85  return Type{std::make_shared<HH_Type>(hnd), typeOuter};
86 }
87 
88 Type HH_Type_Provider::makeStringType(size_t string_length, std::type_index typeOuter) const {
89  if (string_length == Types::constants::_Variable_Length) string_length = H5T_VARIABLE;
90  hid_t t = H5Tcreate(H5T_STRING, string_length);
91  if (t < 0) throw Exception("Failed call to H5Tcreate.", ioda_Here());
92  if (H5Tset_cset(t, H5T_CSET_UTF8) < 0)
93  throw Exception("Failed call to H5Tset_cset.", ioda_Here());
95 
96  return Type{std::make_shared<HH_Type>(hnd), typeOuter};
97 }
98 
99 } // namespace HH
100 } // namespace Engines
101 } // namespace detail
102 } // namespace ioda
103 
104 /// @}
IODA's error system.
HDF5 engine implementation of ioda::detail::Type_Provider.
HDF5 resource handles in C++.
Interfaces for ioda::Type and related classes.
The ioda exception class.
Definition: Exception.h:54
Represents the "type" (i.e. integer, string, float) of a piece of data.
Definition: Type.h:123
This is the implementation of Type_Provider using HDF5. Do not use outside of IODA.
Definition: HH-types.h:27
static HH_hid_t getFundamentalHHType(std::type_index type)
Definition: HH-types.cpp:41
Type makeStringType(size_t string_length, std::type_index typeOuter) const final
Make a variable-length string type.
Definition: HH-types.cpp:88
Type makeArrayType(std::initializer_list< Dimensions_t > dimensions, std::type_index typeOuter, std::type_index typeInner) const final
Definition: HH-types.cpp:73
Type makeFundamentalType(std::type_index type) const final
Make a basic object type, like a double, a float, or a char.
Definition: HH-types.cpp:67
static HH_Type_Provider * instance()
Definition: HH-types.cpp:36
size_t getSize() const final
Get the size of a single element of a type, in bytes.
Definition: HH-types.cpp:29
A class to wrap HDF5's hid_t resource handles.
Definition: Handles.h:92
Common preprocessor definitions used throughout IODA.
constexpr size_t _Variable_Length
Definition: Type.h:182
#define ioda_Here()