HighFive  2.2.2
HighFive - Header-only C++ HDF5 interface
H5ReadWrite_misc.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Blue Brain Project
3  *
4  * Distributed under the Boost Software License, Version 1.0.
5  * (See accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  */
9 #pragma once
10 
11 #include "H5Utils.hpp"
12 
13 namespace HighFive {
14 
15 namespace details {
16 
17 template <typename T>
18 struct BufferInfo {
19  using type_no_const = typename std::remove_const<T>::type;
20  using elem_type = typename details::type_of_array<type_no_const>::type;
21  using char_array_t = typename details::type_char_array<type_no_const>::type;
22  static constexpr bool is_char_array = ! std::is_same<char_array_t, void>::value;
23 
24  BufferInfo(const DataType& dtype);
25 
26  // member data for info depending on the destination dataset type
27  const bool is_fixed_len_string;
28  const size_t n_dimensions;
29  const DataType data_type;
30 };
31 
32 // details implementation
33 template <typename SrcStrT>
34 struct string_type_checker {
35  static DataType getDataType(const DataType&, bool);
36 };
37 
38 template <>
39 struct string_type_checker<void> {
40 inline static DataType getDataType(const DataType& element_type, bool) {
41  return element_type;
42 }};
43 
44 template <std::size_t FixedLen>
45 struct string_type_checker<char[FixedLen]> {
46 inline static DataType getDataType(const DataType& element_type, bool ds_fixed_str) {
47  return ds_fixed_str ? AtomicType<char[FixedLen]>() : element_type;
48 }};
49 
50 template <>
51 struct string_type_checker<char*> {
52 inline static DataType getDataType(const DataType&, bool ds_fixed_str) {
53  if (ds_fixed_str)
54  throw DataSetException("Can't output variable-length to fixed-length strings");
55  return AtomicType<std::string>();
56 }};
57 
58 template <typename T>
59 BufferInfo<T>::BufferInfo(const DataType& dtype)
60  : is_fixed_len_string(dtype.isFixedLenStr())
61  // In case we are using Fixed-len strings we need to subtract one dimension
62  , n_dimensions(details::array_dims<type_no_const>::value -
63  ((is_fixed_len_string && is_char_array) ? 1 : 0))
64  , data_type(string_type_checker<char_array_t>::getDataType(
65  create_datatype<elem_type>(), is_fixed_len_string)) {
66  if (is_fixed_len_string && std::is_same<elem_type, std::string>::value) {
67  throw DataSetException("Can't output std::string as fixed-length. "
68  "Use raw arrays or FixedLenStringArray");
69  }
70  // We warn. In case they are really not convertible an exception will rise on read/write
71  if (dtype.getClass() != data_type.getClass()) {
72  std::cerr << "HighFive WARNING: data and hdf5 dataset have different types: "
73  << data_type.string() << " -> " << dtype.string() << std::endl;
74  }
75 }
76 
77 } // namespace details
78 
79 } // namespace HighFive
Definition: H5_definitions.hpp:15
DataType create_datatype()
Create a DataType instance representing type T.
Definition: H5DataType_misc.hpp:399