HighFive 2.9.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:
167 FixedLengthStringType(size_t size,
168 StringPadding padding,
169 CharacterSet character_set = CharacterSet::Ascii);
170};
171
179
180
186template <typename T>
187class AtomicType: public DataType {
188 public:
189 AtomicType();
190
191 using basic_type = T;
192};
193
194
198class CompoundType: public DataType {
199 public:
202 struct member_def {
203 member_def(std::string t_name, DataType t_base_type, size_t t_offset = 0)
204 : name(std::move(t_name))
205 , base_type(std::move(t_base_type))
206 , offset(t_offset) {}
207 std::string name;
209 size_t offset;
210 };
211
212 CompoundType(const CompoundType& other) = default;
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 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()) {
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
344
345namespace deprecated {
355template <std::size_t N>
357 public:
359
365 FixedLenStringArray(const char array[][N], std::size_t n_strings);
366
372 explicit FixedLenStringArray(const std::vector<std::string>& vec);
373
374 FixedLenStringArray(const std::string* iter_begin, const std::string* iter_end);
375
376 FixedLenStringArray(const std::initializer_list<std::string>&);
377
381 void push_back(const std::string&);
382
383 void push_back(const std::array<char, N>&);
384
388 std::string getString(std::size_t index) const;
389
390 // Container interface
391 inline const char* operator[](std::size_t i) const noexcept {
392 return datavec[i].data();
393 }
394 inline const char* at(std::size_t i) const {
395 return datavec.at(i).data();
396 }
397 inline bool empty() const noexcept {
398 return datavec.empty();
399 }
400 inline std::size_t size() const noexcept {
401 return datavec.size();
402 }
403 inline void resize(std::size_t n) {
404 datavec.resize(n);
405 }
406 inline const char* front() const {
407 return datavec.front().data();
408 }
409 inline const char* back() const {
410 return datavec.back().data();
411 }
412 inline char* data() noexcept {
413 return datavec[0].data();
414 }
415 inline const char* data() const noexcept {
416 return datavec[0].data();
417 }
418
419 private:
420 using vector_t = typename std::vector<std::array<char, N>>;
421
422 public:
423 // Use the underlying iterator
424 using iterator = typename vector_t::iterator;
425 using const_iterator = typename vector_t::const_iterator;
426 using reverse_iterator = typename vector_t::reverse_iterator;
427 using const_reverse_iterator = typename vector_t::const_reverse_iterator;
428 using value_type = typename vector_t::value_type;
429
430 inline iterator begin() noexcept {
431 return datavec.begin();
432 }
433 inline iterator end() noexcept {
434 return datavec.end();
435 }
436 inline const_iterator begin() const noexcept {
437 return datavec.begin();
438 }
439 inline const_iterator cbegin() const noexcept {
440 return datavec.cbegin();
441 }
442 inline const_iterator end() const noexcept {
443 return datavec.end();
444 }
445 inline const_iterator cend() const noexcept {
446 return datavec.cend();
447 }
448 inline reverse_iterator rbegin() noexcept {
449 return datavec.rbegin();
450 }
451 inline reverse_iterator rend() noexcept {
452 return datavec.rend();
453 }
454 inline const_reverse_iterator rbegin() const noexcept {
455 return datavec.rbegin();
456 }
457 inline const_reverse_iterator rend() const noexcept {
458 return datavec.rend();
459 }
460
461 private:
462 vector_t datavec;
463};
464} // namespace deprecated
465
466template <size_t N>
467using FixedLenStringArray H5_DEPRECATED_USING("Use 'std::vector<std::string>'.") =
468 deprecated::FixedLenStringArray<N>;
469
470} // namespace HighFive
471
472
488#define HIGHFIVE_REGISTER_TYPE(type, function) \
489 template <> \
490 inline HighFive::DataType HighFive::create_datatype<type>() { \
491 return function(); \
492 }
493
#define H5_DEPRECATED_USING(msg)
Definition H5_definitions.hpp:16
create an HDF5 DataType from a C++ type
Definition H5DataType.hpp:187
AtomicType()
Definition H5DataType_misc.hpp:235
T basic_type
Definition H5DataType.hpp:191
Class representing an Attribute of a DataSet or Group.
Definition H5Attribute.hpp:46
Create a compound HDF5 datatype.
Definition H5DataType.hpp:198
void commit(const Object &object, const std::string &name) const
Commit datatype into the given Object.
Definition H5DataType_misc.hpp:381
const std::vector< member_def > & getMembers() const noexcept
Get read access to the CompoundType members.
Definition H5DataType.hpp:260
CompoundType(const CompoundType &other)=default
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:94
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:398
EnumType(const std::vector< member_def > &t_members)
Definition H5DataType.hpp:310
File class.
Definition H5File.hpp:24
Definition H5DataType.hpp:147
FixedLengthStringType(size_t size, StringPadding padding, CharacterSet character_set=CharacterSet::Ascii)
Create a fixed length string datatype.
Definition H5DataType_misc.hpp:88
NodeTraits: Base class for Group and File.
Definition H5Node_traits.hpp:28
Definition H5Object.hpp:54
Object()
Definition H5Object_misc.hpp:25
hid_t _hid
Definition H5Object.hpp:105
HDF5 property Lists.
Definition H5PropertyList.hpp:160
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:172
VariableLengthStringType(CharacterSet character_set=CharacterSet::Ascii)
Create a variable length string HDF5 datatype.
Definition H5DataType_misc.hpp:103
A structure representing a set of fixed-length strings.
Definition H5DataType.hpp:356
typename vector_t::const_iterator const_iterator
Definition H5DataType.hpp:425
const_iterator end() const noexcept
Definition H5DataType.hpp:442
const_iterator cend() const noexcept
Definition H5DataType.hpp:445
void resize(std::size_t n)
Definition H5DataType.hpp:403
bool empty() const noexcept
Definition H5DataType.hpp:397
const char * front() const
Definition H5DataType.hpp:406
const_iterator cbegin() const noexcept
Definition H5DataType.hpp:439
void push_back(const std::string &)
Append an std::string to the buffer structure.
Definition H5DataType_misc.hpp:268
const char * back() const
Definition H5DataType.hpp:409
typename vector_t::reverse_iterator reverse_iterator
Definition H5DataType.hpp:426
const char * at(std::size_t i) const
Definition H5DataType.hpp:394
const char * operator[](std::size_t i) const noexcept
Definition H5DataType.hpp:391
std::string getString(std::size_t index) const
Retrieve a string from the structure as std::string.
Definition H5DataType_misc.hpp:282
reverse_iterator rend() noexcept
Definition H5DataType.hpp:451
typename vector_t::const_reverse_iterator const_reverse_iterator
Definition H5DataType.hpp:427
const_iterator begin() const noexcept
Definition H5DataType.hpp:436
const_reverse_iterator rbegin() const noexcept
Definition H5DataType.hpp:454
iterator begin() noexcept
Definition H5DataType.hpp:430
char * data() noexcept
Definition H5DataType.hpp:412
typename vector_t::value_type value_type
Definition H5DataType.hpp:428
reverse_iterator rbegin() noexcept
Definition H5DataType.hpp:448
iterator end() noexcept
Definition H5DataType.hpp:433
const_reverse_iterator rend() const noexcept
Definition H5DataType.hpp:457
typename vector_t::iterator iterator
Definition H5DataType.hpp:424
std::size_t size() const noexcept
Definition H5DataType.hpp:400
const char * data() const noexcept
Definition H5DataType.hpp:415
Definition H5_definitions.hpp:22
DataType create_and_check_datatype()
Create a DataType instance representing type T and perform a sanity check on its size.
Definition H5DataType_misc.hpp:486
DataType create_datatype()
Create a DataType instance representing type T.
Definition H5DataType_misc.hpp:479
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:202
size_t offset
Definition H5DataType.hpp:209
member_def(std::string t_name, DataType t_base_type, size_t t_offset=0)
Definition H5DataType.hpp:203
DataType base_type
Definition H5DataType.hpp:208
std::string name
Definition H5DataType.hpp:207
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
static void ToException(const std::string &prefix_msg)
Definition H5Exception_misc.hpp:43