HighFive 3.0.0
HighFive - Header-only C++ HDF5 interface
Loading...
Searching...
No Matches
H5DataType.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
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 <type_traits>
12#include <vector>
13
14#include <H5Tpublic.h>
15
16#include "H5Object.hpp"
17#include "bits/H5Utils.hpp"
18
20#include "H5PropertyList.hpp"
21
22#include "bits/h5_wrapper.hpp"
23#include "bits/h5t_wrapper.hpp"
24
25namespace HighFive {
26
27
31enum class DataTypeClass {
32 Time = 1 << 1,
33 Integer = 1 << 2,
34 Float = 1 << 3,
35 String = 1 << 4,
36 BitField = 1 << 5,
37 Opaque = 1 << 6,
38 Compound = 1 << 7,
39 Reference = 1 << 8,
40 Enum = 1 << 9,
41 VarLen = 1 << 10,
42 Array = 1 << 11,
43 Invalid = 0
44};
45
47 using T = std::underlying_type<DataTypeClass>::type;
48 return static_cast<DataTypeClass>(static_cast<T>(lhs) | static_cast<T>(rhs));
49}
50
52 using T = std::underlying_type<DataTypeClass>::type;
53 return static_cast<DataTypeClass>(static_cast<T>(lhs) & static_cast<T>(rhs));
54}
55
56class StringType;
57
61class DataType: public Object {
62 public:
63 bool operator==(const DataType& other) const;
64
65 bool operator!=(const DataType& other) const;
66
70 DataTypeClass getClass() const;
71
78 size_t getSize() const;
79
83 std::string string() const;
84
88 bool isVariableStr() const;
89
93 bool isFixedLenStr() const;
94
99
103 bool empty() const noexcept;
104
106 bool isReference() const;
107
110 return details::get_plist<DataTypeCreateProps>(*this, H5Tget_create_plist);
111 }
112
113 protected:
114 using Object::Object;
115
116 friend class Attribute;
117 friend class File;
118 friend class DataSet;
119 friend class CompoundType;
120 template <typename Derivate>
121 friend class NodeTraits;
122};
123
124
125enum class CharacterSet : std::underlying_type<H5T_cset_t>::type {
126 Ascii = H5T_CSET_ASCII,
127 Utf8 = H5T_CSET_UTF8,
128};
129
130class StringType: public DataType {
131 public:
136
141
142 protected:
143 using DataType::DataType;
144 friend class DataType;
145};
146
148 public:
169 FixedLengthStringType(size_t size,
170 StringPadding padding,
171 CharacterSet character_set = CharacterSet::Ascii);
172};
173
175 public:
180};
181
182
188template <typename T>
189class AtomicType: public DataType {
190 public:
191 AtomicType();
192
193 using basic_type = T;
194};
195
196
200class CompoundType: public DataType {
201 public:
204 struct member_def {
205 member_def(std::string t_name, DataType t_base_type, size_t t_offset = 0)
206 : name(std::move(t_name))
207 , base_type(std::move(t_base_type))
208 , offset(t_offset) {}
209 std::string name;
211 size_t offset;
212 };
213
218 inline CompoundType(const std::vector<member_def>& t_members, size_t size = 0)
219 : members(t_members) {
220 create(size);
221 }
222 inline CompoundType(std::vector<member_def>&& t_members, size_t size = 0)
223 : members(std::move(t_members)) {
224 create(size);
225 }
226 inline CompoundType(const std::initializer_list<member_def>& t_members, size_t size = 0)
227 : members(t_members) {
228 create(size);
229 }
230
234 inline explicit CompoundType(DataType&& type)
235 : DataType(type) {
237 std::ostringstream ss;
238 ss << "hid " << _hid << " does not refer to a compound data type";
239 throw DataTypeException(ss.str());
240 }
241 size_t n_members = static_cast<size_t>(detail::h5t_get_nmembers(_hid));
242 members.reserve(n_members);
243 for (unsigned i = 0; i < n_members; i++) {
244 char* name = detail::h5t_get_member_name(_hid, i);
245 size_t offset = detail::h5t_get_member_offset(_hid, i);
246 hid_t member_hid = detail::h5t_get_member_type(_hid, i);
247 DataType member_type{member_hid};
248 members.emplace_back(std::string(name), member_type, offset);
249
250 detail::h5_free_memory(name);
251 }
252 }
253
257 inline void commit(const Object& object, const std::string& name) const;
258
260 inline const std::vector<member_def>& getMembers() const noexcept {
261 return members;
262 }
263
264 private:
266 std::vector<member_def> members;
267
271 void create(size_t size = 0);
272};
273
295template <typename T>
296class EnumType: public DataType {
297 public:
300 struct member_def {
301 member_def(const std::string& t_name, T t_value)
302 : name(t_name)
303 , value(std::move(t_value)) {}
304 std::string name;
306 };
307
308 EnumType(const EnumType& other) = default;
309
310 EnumType(const std::vector<member_def>& t_members)
311 : members(t_members) {
312 static_assert(std::is_enum<T>::value, "EnumType<T>::create takes only enum");
313 if (members.empty()) {
314 HDF5ErrMapper::ToException<DataTypeException>(
315 "Could not create an enum without members");
316 }
317 create();
318 }
319
320 EnumType(std::initializer_list<member_def> t_members)
321 : EnumType(std::vector<member_def>(t_members)) {}
322
326 void commit(const Object& object, const std::string& name) const;
327
328 private:
329 std::vector<member_def> members;
330
331 void create();
332};
333
334
336template <typename T>
337DataType create_datatype();
338
339
341template <typename T>
343} // namespace HighFive
344
345
361#define HIGHFIVE_REGISTER_TYPE(type, function) \
362 template <> \
363 inline HighFive::DataType HighFive::create_datatype<type>() { \
364 return function(); \
365 }
366
create an HDF5 DataType from a C++ type
Definition H5DataType.hpp:189
AtomicType()
Definition H5DataType_misc.hpp:228
T basic_type
Definition H5DataType.hpp:193
Class representing an Attribute of a DataSet or Group.
Definition H5Attribute.hpp:47
Create a compound HDF5 datatype.
Definition H5DataType.hpp:200
void commit(const Object &object, const std::string &name) const
Commit datatype into the given Object.
Definition H5DataType_misc.hpp:333
const std::vector< member_def > & getMembers() const noexcept
Get read access to the CompoundType members.
Definition H5DataType.hpp:260
CompoundType(const std::initializer_list< member_def > &t_members, size_t size=0)
Definition H5DataType.hpp:226
CompoundType(const std::vector< member_def > &t_members, size_t size=0)
Initializes a compound type from a vector of member definitions.
Definition H5DataType.hpp:218
CompoundType(DataType &&type)
Initializes a compound type from a DataType.
Definition H5DataType.hpp:234
CompoundType(std::vector< member_def > &&t_members, size_t size=0)
Definition H5DataType.hpp:222
Class representing a dataset.
Definition H5DataSet.hpp:30
Exception specific to HighFive DataType interface.
Definition H5Exception.hpp:97
HDF5 Data Type.
Definition H5DataType.hpp:61
bool operator==(const DataType &other) const
Definition H5DataType_misc.hpp:44
bool isFixedLenStr() const
Returns whether the type is a fixed-length string.
Definition H5DataType_misc.hpp:56
DataTypeCreateProps getCreatePropertyList() const
Get the list of properties for creation of this DataType.
Definition H5DataType.hpp:109
size_t getSize() const
Returns the length (in bytes) of this type elements.
Definition H5DataType_misc.hpp:40
bool isVariableStr() const
Returns whether the type is a variable-length string.
Definition H5DataType_misc.hpp:52
bool empty() const noexcept
Check the DataType was default constructed.
Definition H5DataType_misc.hpp:32
std::string string() const
Returns a friendly description of the type (e.g. Float32)
Definition H5DataType_misc.hpp:76
DataTypeClass getClass() const
Return the fundamental type.
Definition H5DataType_misc.hpp:36
bool isReference() const
Returns whether the type is a Reference.
Definition H5DataType_misc.hpp:60
StringType asStringType() const
Returns this datatype as a StringType.
Definition H5DataType_misc.hpp:64
bool operator!=(const DataType &other) const
Definition H5DataType_misc.hpp:48
Create a enum HDF5 datatype.
Definition H5DataType.hpp:296
EnumType(std::initializer_list< member_def > t_members)
Definition H5DataType.hpp:320
EnumType(const EnumType &other)=default
void commit(const Object &object, const std::string &name) const
Commit datatype into the given Object.
Definition H5DataType_misc.hpp:350
EnumType(const std::vector< member_def > &t_members)
Definition H5DataType.hpp:310
File class.
Definition H5File.hpp:25
Definition H5DataType.hpp:147
NodeTraits: Base class for Group and File.
Definition H5Node_traits.hpp:28
Definition H5Object.hpp:36
Object()
Definition H5Object_misc.hpp:20
hid_t _hid
Definition H5Object.hpp:98
HDF5 property Lists.
Definition H5PropertyList.hpp:138
An HDF5 (object) reference type.
Definition H5Reference.hpp:33
Definition H5DataType.hpp:130
StringPadding getPadding() const
For fixed length stings return the padding.
Definition H5DataType_misc.hpp:80
CharacterSet getCharacterSet() const
For stings return the character set.
Definition H5DataType_misc.hpp:84
Definition H5DataType.hpp:174
Definition assert_compatible_spaces.hpp:15
DataType create_and_check_datatype()
Create a DataType instance representing type T and perform a sanity check on its size.
Definition H5DataType_misc.hpp:438
DataType create_datatype()
Create a DataType instance representing type T.
Definition H5DataType_misc.hpp:431
CharacterSet
Definition H5DataType.hpp:125
DataTypeClass operator|(DataTypeClass lhs, DataTypeClass rhs)
Definition H5DataType.hpp:46
DataTypeClass operator&(DataTypeClass lhs, DataTypeClass rhs)
Definition H5DataType.hpp:51
DataTypeClass
Enum of Fundamental data classes.
Definition H5DataType.hpp:31
StringPadding
Definition string_padding.hpp:7
Use for defining a sub-type of compound type.
Definition H5DataType.hpp:204
size_t offset
Definition H5DataType.hpp:211
member_def(std::string t_name, DataType t_base_type, size_t t_offset=0)
Definition H5DataType.hpp:205
DataType base_type
Definition H5DataType.hpp:210
std::string name
Definition H5DataType.hpp:209
Use for defining a member of enum type.
Definition H5DataType.hpp:300
T value
Definition H5DataType.hpp:305
std::string name
Definition H5DataType.hpp:304
member_def(const std::string &t_name, T t_value)
Definition H5DataType.hpp:301