hops
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;
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;
30 };
31 
32 // details implementation
33 template <typename SrcStrT>
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>
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
HighFive::details::BufferInfo::is_char_array
static constexpr bool is_char_array
Definition: H5ReadWrite_misc.hpp:22
HighFive::details::BufferInfo::char_array_t
typename details::type_char_array< type_no_const >::type char_array_t
Definition: H5ReadWrite_misc.hpp:21
HighFive::details::BufferInfo::is_fixed_len_string
const bool is_fixed_len_string
Definition: H5ReadWrite_misc.hpp:27
HighFive::DataType
HDF5 Data Type.
Definition: H5DataType.hpp:42
HighFive::details::BufferInfo::elem_type
typename details::type_of_array< type_no_const >::type elem_type
Definition: H5ReadWrite_misc.hpp:20
HighFive::details::array_dims
Definition: H5Utils.hpp:44
HighFive::details::BufferInfo::n_dimensions
const size_t n_dimensions
Definition: H5ReadWrite_misc.hpp:28
HighFive::details::string_type_checker
Definition: H5ReadWrite_misc.hpp:34
HighFive::details::string_type_checker< char * >::getDataType
static DataType getDataType(const DataType &, bool ds_fixed_str)
Definition: H5ReadWrite_misc.hpp:52
HighFive::DataType::string
std::string string() const
Returns a friendly description of the type (e.g. Float32)
Definition: H5DataType_misc.hpp:65
HighFive::details::type_char_array::type
void type
Definition: H5Utils.hpp:183
HighFive::DataSetException
Exception specific to HighFive DataSet interface.
Definition: H5Exception.hpp:115
HighFive::details::BufferInfo
Definition: H5ReadWrite_misc.hpp:18
HighFive::AtomicType
create an HDF5 DataType from a C++ type
Definition: H5_definitions.hpp:36
HighFive::details::type_of_array::type
unqualified_t< T > type
Definition: H5Utils.hpp:137
HighFive::details::BufferInfo::data_type
const DataType data_type
Definition: H5ReadWrite_misc.hpp:29
HighFive::details::BufferInfo::BufferInfo
BufferInfo(const DataType &dtype)
Definition: H5ReadWrite_misc.hpp:59
HighFive::create_datatype
DataType create_datatype()
Create a DataType instance representing type T.
Definition: H5DataType_misc.hpp:399
HighFive::DataType::getClass
DataTypeClass getClass() const
Return the fundamental type.
Definition: H5DataType_misc.hpp:32
HighFive::details::string_type_checker< char[FixedLen]>::getDataType
static DataType getDataType(const DataType &element_type, bool ds_fixed_str)
Definition: H5ReadWrite_misc.hpp:46
HighFive::details::string_type_checker::getDataType
static DataType getDataType(const DataType &, bool)
H5Utils.hpp
HighFive::details::string_type_checker< void >::getDataType
static DataType getDataType(const DataType &element_type, bool)
Definition: H5ReadWrite_misc.hpp:40
HighFive::details::BufferInfo::type_no_const
typename std::remove_const< T >::type type_no_const
Definition: H5ReadWrite_misc.hpp:19
HighFive
Definition: H5_definitions.hpp:15