HighFive 3.1.1
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;
57class IntegerType;
58
62class DataType: public Object {
63 public:
64 bool operator==(const DataType& other) const;
65
66 bool operator!=(const DataType& other) const;
67
71 DataTypeClass getClass() const;
72
79 size_t getSize() const;
80
84 std::string string() const;
85
89 bool isVariableStr() const;
90
94 bool isFixedLenStr() const;
95
100
105
109 bool empty() const noexcept;
110
112 bool isReference() const;
113
116 return details::get_plist<DataTypeCreateProps>(*this, H5Tget_create_plist);
117 }
118
119 protected:
120 using Object::Object;
121
122 friend class Attribute;
123 friend class File;
124 friend class DataSet;
125 friend class CompoundType;
126 template <typename Derivate>
127 friend class NodeTraits;
128};
129
130
131enum class CharacterSet : std::underlying_type<H5T_cset_t>::type {
132 Ascii = H5T_CSET_ASCII,
133 Utf8 = H5T_CSET_UTF8,
134};
135
136class StringType: public DataType {
137 public:
142
147
148 protected:
149 using DataType::DataType;
150 friend class DataType;
151};
152
154 public:
175 FixedLengthStringType(size_t size,
176 StringPadding padding,
177 CharacterSet character_set = CharacterSet::Ascii);
178};
179
181 public:
186};
187
194class IntegerType: public DataType {
195 public:
196 bool isSigned() {
197 return detail::h5t_get_sign(getId()) != H5T_SGN_NONE;
198 }
199
200 protected:
201 using DataType::DataType;
202 friend class DataType;
203};
204
210template <typename T>
211class AtomicType: public DataType {
212 public:
213 AtomicType();
214
215 using basic_type = T;
216};
217
218
222class CompoundType: public DataType {
223 public:
226 struct member_def {
227 member_def(std::string t_name, DataType t_base_type, size_t t_offset = 0)
228 : name(std::move(t_name))
229 , base_type(std::move(t_base_type))
230 , offset(t_offset) {}
231 std::string name;
233 size_t offset;
234 };
235
240 inline CompoundType(const std::vector<member_def>& t_members, size_t size = 0)
241 : members(t_members) {
242 create(size);
243 }
244 inline CompoundType(std::vector<member_def>&& t_members, size_t size = 0)
245 : members(std::move(t_members)) {
246 create(size);
247 }
248 inline CompoundType(const std::initializer_list<member_def>& t_members, size_t size = 0)
249 : members(t_members) {
250 create(size);
251 }
252
256 inline explicit CompoundType(DataType&& type)
257 : DataType(type) {
259 std::ostringstream ss;
260 ss << "hid " << _hid << " does not refer to a compound data type";
261 throw DataTypeException(ss.str());
262 }
263 size_t n_members = static_cast<size_t>(detail::h5t_get_nmembers(_hid));
264 members.reserve(n_members);
265 for (unsigned i = 0; i < n_members; i++) {
266 char* name = detail::h5t_get_member_name(_hid, i);
267 size_t offset = detail::h5t_get_member_offset(_hid, i);
268 hid_t member_hid = detail::h5t_get_member_type(_hid, i);
269 DataType member_type{member_hid};
270 members.emplace_back(std::string(name), member_type, offset);
271
272 detail::h5_free_memory(name);
273 }
274 }
275
279 inline void commit(const Object& object, const std::string& name) const;
280
282 inline const std::vector<member_def>& getMembers() const noexcept {
283 return members;
284 }
285
286 private:
288 std::vector<member_def> members;
289
293 void create(size_t size = 0);
294};
295
317template <typename T>
318class EnumType: public DataType {
319 public:
322 struct member_def {
323 member_def(const std::string& t_name, T t_value)
324 : name(t_name)
325 , value(std::move(t_value)) {}
326 std::string name;
328 };
329
330 EnumType(const EnumType& other) = default;
331
332 EnumType(const std::vector<member_def>& t_members)
333 : members(t_members) {
334 static_assert(std::is_enum<T>::value, "EnumType<T>::create takes only enum");
335 if (members.empty()) {
336 HDF5ErrMapper::ToException<DataTypeException>(
337 "Could not create an enum without members");
338 }
339 create();
340 }
341
342 EnumType(std::initializer_list<member_def> t_members)
343 : EnumType(std::vector<member_def>(t_members)) {}
344
348 void commit(const Object& object, const std::string& name) const;
349
350 private:
351 std::vector<member_def> members;
352
353 void create();
354};
355
356
358template <typename T>
359DataType create_datatype();
360
361
363template <typename T>
365} // namespace HighFive
366
367
383#define HIGHFIVE_REGISTER_TYPE(type, function) \
384 template <> \
385 inline HighFive::DataType HighFive::create_datatype<type>() { \
386 return function(); \
387 }
388
create an HDF5 DataType from a C++ type
Definition H5DataType.hpp:211
AtomicType()
Definition H5DataType_misc.hpp:240
T basic_type
Definition H5DataType.hpp:215
Class representing an Attribute of a DataSet or Group.
Definition H5Attribute.hpp:47
Create a compound HDF5 datatype.
Definition H5DataType.hpp:222
void commit(const Object &object, const std::string &name) const
Commit datatype into the given Object.
Definition H5DataType_misc.hpp:345
const std::vector< member_def > & getMembers() const noexcept
Get read access to the CompoundType members.
Definition H5DataType.hpp:282
CompoundType(const std::initializer_list< member_def > &t_members, size_t size=0)
Definition H5DataType.hpp:248
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:240
CompoundType(DataType &&type)
Initializes a compound type from a DataType.
Definition H5DataType.hpp:256
CompoundType(std::vector< member_def > &&t_members, size_t size=0)
Definition H5DataType.hpp:244
Class representing a dataset.
Definition H5DataSet.hpp:30
Exception specific to HighFive DataType interface.
Definition H5Exception.hpp:97
HDF5 Data Type.
Definition H5DataType.hpp:62
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:115
size_t getSize() const
Returns the length (in bytes) of this type elements.
Definition H5DataType_misc.hpp:40
IntegerType asIntegerType() const
Returns this datatype as a IntegerType.
Definition H5DataType_misc.hpp:76
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:88
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:318
EnumType(std::initializer_list< member_def > t_members)
Definition H5DataType.hpp:342
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:362
EnumType(const std::vector< member_def > &t_members)
Definition H5DataType.hpp:332
File class.
Definition H5File.hpp:25
Definition H5DataType.hpp:153
An Integer datatype (i.e. H5T_INTEGER).
Definition H5DataType.hpp:194
bool isSigned()
Definition H5DataType.hpp:196
NodeTraits: Base class for Group and File.
Definition H5Node_traits.hpp:28
Definition H5Object.hpp:36
hid_t getId() const noexcept
getId
Definition H5Object_misc.hpp:75
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:136
StringPadding getPadding() const
For fixed length stings return the padding.
Definition H5DataType_misc.hpp:92
CharacterSet getCharacterSet() const
For stings return the character set.
Definition H5DataType_misc.hpp:96
Definition H5DataType.hpp:180
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:450
DataType create_datatype()
Create a DataType instance representing type T.
Definition H5DataType_misc.hpp:443
CharacterSet
Definition H5DataType.hpp:131
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:226
size_t offset
Definition H5DataType.hpp:233
member_def(std::string t_name, DataType t_base_type, size_t t_offset=0)
Definition H5DataType.hpp:227
DataType base_type
Definition H5DataType.hpp:232
std::string name
Definition H5DataType.hpp:231
Use for defining a member of enum type.
Definition H5DataType.hpp:322
T value
Definition H5DataType.hpp:327
std::string name
Definition H5DataType.hpp:326
member_def(const std::string &t_name, T t_value)
Definition H5DataType.hpp:323