HighFive 2.10.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;
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
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
214 CompoundType(const CompoundType& other) = default;
215
220 inline CompoundType(const std::vector<member_def>& t_members, size_t size = 0)
221 : members(t_members) {
222 create(size);
223 }
224 inline CompoundType(std::vector<member_def>&& t_members, size_t size = 0)
225 : members(std::move(t_members)) {
226 create(size);
227 }
228 inline CompoundType(const std::initializer_list<member_def>& t_members, size_t size = 0)
229 : members(t_members) {
230 create(size);
231 }
232
236 inline CompoundType(DataType&& type)
237 : DataType(type) {
239 std::ostringstream ss;
240 ss << "hid " << _hid << " does not refer to a compound data type";
241 throw DataTypeException(ss.str());
242 }
243 size_t n_members = static_cast<size_t>(detail::h5t_get_nmembers(_hid));
244 members.reserve(n_members);
245 for (unsigned i = 0; i < n_members; i++) {
246 char* name = detail::h5t_get_member_name(_hid, i);
247 size_t offset = detail::h5t_get_member_offset(_hid, i);
248 hid_t member_hid = detail::h5t_get_member_type(_hid, i);
249 DataType member_type{member_hid};
250 members.emplace_back(std::string(name), member_type, offset);
251
252 detail::h5_free_memory(name);
253 }
254 }
255
259 inline void commit(const Object& object, const std::string& name) const;
260
262 inline const std::vector<member_def>& getMembers() const noexcept {
263 return members;
264 }
265
266 private:
268 std::vector<member_def> members;
269
273 void create(size_t size = 0);
274};
275
297template <typename T>
298class EnumType: public DataType {
299 public:
302 struct member_def {
303 member_def(const std::string& t_name, T t_value)
304 : name(t_name)
305 , value(std::move(t_value)) {}
306 std::string name;
308 };
309
310 EnumType(const EnumType& other) = default;
311
312 EnumType(const std::vector<member_def>& t_members)
313 : members(t_members) {
314 static_assert(std::is_enum<T>::value, "EnumType<T>::create takes only enum");
315 if (members.empty()) {
317 "Could not create an enum without members");
318 }
319 create();
320 }
321
322 EnumType(std::initializer_list<member_def> t_members)
323 : EnumType(std::vector<member_def>(t_members)) {}
324
328 void commit(const Object& object, const std::string& name) const;
329
330 private:
331 std::vector<member_def> members;
332
333 void create();
334};
335
336
338template <typename T>
339DataType create_datatype();
340
341
343template <typename T>
345
346
347namespace deprecated {
357template <std::size_t N>
359 public:
361
367 FixedLenStringArray(const char array[][N], std::size_t n_strings);
368
374 explicit FixedLenStringArray(const std::vector<std::string>& vec);
375
376 FixedLenStringArray(const std::string* iter_begin, const std::string* iter_end);
377
378 FixedLenStringArray(const std::initializer_list<std::string>&);
379
383 void push_back(const std::string&);
384
385 void push_back(const std::array<char, N>&);
386
390 std::string getString(std::size_t index) const;
391
392 // Container interface
393 inline const char* operator[](std::size_t i) const noexcept {
394 return datavec[i].data();
395 }
396 inline const char* at(std::size_t i) const {
397 return datavec.at(i).data();
398 }
399 inline bool empty() const noexcept {
400 return datavec.empty();
401 }
402 inline std::size_t size() const noexcept {
403 return datavec.size();
404 }
405 inline void resize(std::size_t n) {
406 datavec.resize(n);
407 }
408 inline const char* front() const {
409 return datavec.front().data();
410 }
411 inline const char* back() const {
412 return datavec.back().data();
413 }
414 inline char* data() noexcept {
415 return datavec[0].data();
416 }
417 inline const char* data() const noexcept {
418 return datavec[0].data();
419 }
420
421 private:
422 using vector_t = typename std::vector<std::array<char, N>>;
423
424 public:
425 // Use the underlying iterator
426 using iterator = typename vector_t::iterator;
427 using const_iterator = typename vector_t::const_iterator;
428 using reverse_iterator = typename vector_t::reverse_iterator;
429 using const_reverse_iterator = typename vector_t::const_reverse_iterator;
430 using value_type = typename vector_t::value_type;
431
432 inline iterator begin() noexcept {
433 return datavec.begin();
434 }
435 inline iterator end() noexcept {
436 return datavec.end();
437 }
438 inline const_iterator begin() const noexcept {
439 return datavec.begin();
440 }
441 inline const_iterator cbegin() const noexcept {
442 return datavec.cbegin();
443 }
444 inline const_iterator end() const noexcept {
445 return datavec.end();
446 }
447 inline const_iterator cend() const noexcept {
448 return datavec.cend();
449 }
450 inline reverse_iterator rbegin() noexcept {
451 return datavec.rbegin();
452 }
453 inline reverse_iterator rend() noexcept {
454 return datavec.rend();
455 }
456 inline const_reverse_iterator rbegin() const noexcept {
457 return datavec.rbegin();
458 }
459 inline const_reverse_iterator rend() const noexcept {
460 return datavec.rend();
461 }
462
463 private:
464 vector_t datavec;
465};
466} // namespace deprecated
467
468template <size_t N>
469using FixedLenStringArray H5_DEPRECATED_USING("Use 'std::vector<std::string>'.") =
470 deprecated::FixedLenStringArray<N>;
471
472} // namespace HighFive
473
474
490#define HIGHFIVE_REGISTER_TYPE(type, function) \
491 template <> \
492 inline HighFive::DataType HighFive::create_datatype<type>() { \
493 return function(); \
494 }
495
#define H5_DEPRECATED_USING(msg)
Definition H5_definitions.hpp:16
create an HDF5 DataType from a C++ type
Definition H5DataType.hpp:189
AtomicType()
Definition H5DataType_misc.hpp:235
T basic_type
Definition H5DataType.hpp:193
Class representing an Attribute of a DataSet or Group.
Definition H5Attribute.hpp:46
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:381
const std::vector< member_def > & getMembers() const noexcept
Get read access to the CompoundType members.
Definition H5DataType.hpp:262
CompoundType(const CompoundType &other)=default
CompoundType(const std::initializer_list< member_def > &t_members, size_t size=0)
Definition H5DataType.hpp:228
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:220
CompoundType(DataType &&type)
Initializes a compound type from a DataType.
Definition H5DataType.hpp:236
CompoundType(std::vector< member_def > &&t_members, size_t size=0)
Definition H5DataType.hpp:224
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:298
EnumType(std::initializer_list< member_def > t_members)
Definition H5DataType.hpp:322
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:312
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:174
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:358
typename vector_t::const_iterator const_iterator
Definition H5DataType.hpp:427
const_iterator end() const noexcept
Definition H5DataType.hpp:444
const_iterator cend() const noexcept
Definition H5DataType.hpp:447
void resize(std::size_t n)
Definition H5DataType.hpp:405
bool empty() const noexcept
Definition H5DataType.hpp:399
const char * front() const
Definition H5DataType.hpp:408
const_iterator cbegin() const noexcept
Definition H5DataType.hpp:441
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:411
typename vector_t::reverse_iterator reverse_iterator
Definition H5DataType.hpp:428
const char * at(std::size_t i) const
Definition H5DataType.hpp:396
const char * operator[](std::size_t i) const noexcept
Definition H5DataType.hpp:393
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:453
typename vector_t::const_reverse_iterator const_reverse_iterator
Definition H5DataType.hpp:429
const_iterator begin() const noexcept
Definition H5DataType.hpp:438
const_reverse_iterator rbegin() const noexcept
Definition H5DataType.hpp:456
iterator begin() noexcept
Definition H5DataType.hpp:432
char * data() noexcept
Definition H5DataType.hpp:414
typename vector_t::value_type value_type
Definition H5DataType.hpp:430
reverse_iterator rbegin() noexcept
Definition H5DataType.hpp:450
iterator end() noexcept
Definition H5DataType.hpp:435
const_reverse_iterator rend() const noexcept
Definition H5DataType.hpp:459
typename vector_t::iterator iterator
Definition H5DataType.hpp:426
std::size_t size() const noexcept
Definition H5DataType.hpp:402
const char * data() const noexcept
Definition H5DataType.hpp:417
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: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:302
T value
Definition H5DataType.hpp:307
std::string name
Definition H5DataType.hpp:306
member_def(const std::string &t_name, T t_value)
Definition H5DataType.hpp:303
static void ToException(const std::string &prefix_msg)
Definition H5Exception_misc.hpp:43