h5gt 0.2.0
C++ wrapper for HDF5 library (based on HighFive project)
Loading...
Searching...
No Matches
H5ReadWrite_misc.hpp
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
13namespace h5gt {
14
15namespace details {
16
17template <typename T>
18struct BufferInfo {
19 using type_no_const = typename std::remove_const<T>::type;
20 using elem_type = typename details::inspector<type_no_const>::base_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
33template <typename SrcStrT>
35 static DataType getDataType(const DataType&, bool);
36};
37
38template <>
39struct string_type_checker<void> {
40 inline static DataType getDataType(const DataType& element_type, bool) {
41 return element_type;
42 }
43};
44
45template <std::size_t FixedLen>
46struct string_type_checker<char[FixedLen]> {
47 inline static DataType getDataType(const DataType& element_type, bool ds_fixed_str) {
48 return ds_fixed_str ? AtomicType<char[FixedLen]>() : element_type;
49 }
50};
51
52template <>
53struct string_type_checker<char*> {
54 inline static DataType getDataType(const DataType&, bool ds_fixed_str) {
55 if (ds_fixed_str)
56 throw DataSetException("Can't output variable-length to fixed-length strings");
58 }
59};
60
61//template <> // added by kerim, do I need this?
62//struct string_type_checker<const char*> {
63// inline static DataType getDataType(const DataType&, bool ds_fixed_str) {
64// if (ds_fixed_str)
65// throw DataSetException("Can't output variable-length to fixed-length strings");
66// return AtomicType<std::string>();
67// }
68//};
69
70template <typename T>
72 : is_fixed_len_string(dtype.isFixedLenStr())
73 // In case we are using Fixed-len strings we need to subtract one dimension
74 , n_dimensions(details::inspector<type_no_const>::recursive_ndim -
75 ((is_fixed_len_string && is_char_array) ? 1 : 0))
76 , data_type(string_type_checker<char_array_t>::getDataType(
77 create_datatype<elem_type>(), is_fixed_len_string)) {
78 if (is_fixed_len_string && std::is_same<elem_type, std::string>::value) {
79 throw DataSetException("Can't output std::string as fixed-length. "
80 "Use raw arrays or FixedLenStringArray");
81 }
82 // We warn. In case they are really not convertible an exception will rise on read/write
83 if (dtype.getClass() != data_type.getClass()) {
84 std::cerr << "h5gt WARNING: data and hdf5 dataset have different types: "
85 << data_type.string() << " -> " << dtype.string() << std::endl;
86 }
87}
88
89} // namespace details
90
91} // namespace h5gt
create an HDF5 DataType from a C++ type
Definition H5DataType.hpp:124
Exception specific to h5gt DataSet interface.
Definition H5Exception.hpp:115
HDF5 Data Type.
Definition H5DataType.hpp:48
std::string string() const
Returns a friendly description of the type (e.g. Float32)
Definition H5DataType_misc.hpp:80
DataTypeClass getClass() const
Return the fundamental type.
Definition H5DataType_misc.hpp:32
Definition H5ReadWrite_misc.hpp:18
Definition H5Utils.hpp:49
Definition H5ReadWrite_misc.hpp:34