HighFive 2.7.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 "H5Object.hpp"
15#include "bits/H5Utils.hpp"
16
17#include "H5PropertyList.hpp"
18
19namespace HighFive {
20
21
25enum class DataTypeClass {
26 Time = 1 << 1,
27 Integer = 1 << 2,
28 Float = 1 << 3,
29 String = 1 << 4,
30 BitField = 1 << 5,
31 Opaque = 1 << 6,
32 Compound = 1 << 7,
33 Reference = 1 << 8,
34 Enum = 1 << 9,
35 VarLen = 1 << 10,
36 Array = 1 << 11,
37 Invalid = 0
38};
39
41 using T = std::underlying_type<DataTypeClass>::type;
42 return static_cast<DataTypeClass>(static_cast<T>(lhs) | static_cast<T>(rhs));
43}
44
46 using T = std::underlying_type<DataTypeClass>::type;
47 return static_cast<DataTypeClass>(static_cast<T>(lhs) & static_cast<T>(rhs));
48}
49
50
54class DataType: public Object {
55 public:
56 bool operator==(const DataType& other) const;
57
58 bool operator!=(const DataType& other) const;
59
63 DataTypeClass getClass() const;
64
71 size_t getSize() const;
72
76 std::string string() const;
77
81 bool isVariableStr() const;
82
86 bool isFixedLenStr() const;
87
92 bool empty() const noexcept;
93
95 bool isReference() const;
96
99 return details::get_plist<DataTypeCreateProps>(*this, H5Tget_create_plist);
100 }
101
102 protected:
103 using Object::Object;
104
105 friend class Attribute;
106 friend class File;
107 friend class DataSet;
108 friend class CompoundType;
109};
110
116template <typename T>
117class AtomicType: public DataType {
118 public:
119 AtomicType();
120
121 using basic_type = T;
122};
123
124
128class CompoundType: public DataType {
129 public:
132 struct member_def {
133 member_def(std::string t_name, DataType t_base_type, size_t t_offset = 0)
134 : name(std::move(t_name))
135 , base_type(std::move(t_base_type))
136 , offset(t_offset) {}
137 std::string name;
139 size_t offset;
140 };
141
142 CompoundType(const CompoundType& other) = default;
143
148 inline CompoundType(const std::vector<member_def>& t_members, size_t size = 0)
149 : members(t_members) {
150 create(size);
151 }
152 inline CompoundType(std::vector<member_def>&& t_members, size_t size = 0)
153 : members(std::move(t_members)) {
154 create(size);
155 }
156 inline CompoundType(const std::initializer_list<member_def>& t_members, size_t size = 0)
157 : members(t_members) {
158 create(size);
159 }
160
164 inline CompoundType(DataType&& type)
165 : DataType(type) {
167 std::ostringstream ss;
168 ss << "hid " << _hid << " does not refer to a compound data type";
169 throw DataTypeException(ss.str());
170 }
171 int result = H5Tget_nmembers(_hid);
172 if (result < 0) {
173 throw DataTypeException("Could not get members of compound datatype");
174 }
175 size_t n_members = static_cast<size_t>(result);
176 members.reserve(n_members);
177 for (unsigned i = 0; i < n_members; i++) {
178 const char* name = H5Tget_member_name(_hid, i);
179 size_t offset = H5Tget_member_offset(_hid, i);
180 hid_t member_hid = H5Tget_member_type(_hid, i);
181 DataType member_type{member_hid};
182 members.emplace_back(name, member_type, offset);
183 }
184 }
185
189 inline void commit(const Object& object, const std::string& name) const;
190
192 inline const std::vector<member_def>& getMembers() const noexcept {
193 return members;
194 }
195
196 private:
198 std::vector<member_def> members;
199
203 void create(size_t size = 0);
204};
205
227template <typename T>
228class EnumType: public DataType {
229 public:
232 struct member_def {
233 member_def(const std::string& t_name, T t_value)
234 : name(t_name)
235 , value(std::move(t_value)) {}
236 std::string name;
238 };
239
240 EnumType(const EnumType& other) = default;
241
242 EnumType(const std::vector<member_def>& t_members)
243 : members(t_members) {
244 static_assert(std::is_enum<T>::value, "EnumType<T>::create takes only enum");
245 if (members.empty()) {
247 "Could not create an enum without members");
248 }
249 create();
250 }
251
252 EnumType(std::initializer_list<member_def> t_members)
253 : EnumType(std::vector<member_def>({t_members})) {}
254
258 void commit(const Object& object, const std::string& name) const;
259
260 private:
261 std::vector<member_def> members;
262
263 void create();
264};
265
266
268template <typename T>
269DataType create_datatype();
270
271
273template <typename T>
275
276
283template <std::size_t N>
285 public:
287
291 FixedLenStringArray(const char array[][N], std::size_t length);
292
298 explicit FixedLenStringArray(const std::vector<std::string>& vec);
299
300 FixedLenStringArray(const std::string* iter_begin, const std::string* iter_end);
301
302 FixedLenStringArray(const std::initializer_list<std::string>&);
303
307 void push_back(const std::string&);
308
309 void push_back(const std::array<char, N>&);
310
314 std::string getString(std::size_t index) const;
315
316 // Container interface
317 inline const char* operator[](std::size_t i) const noexcept {
318 return datavec[i].data();
319 }
320 inline const char* at(std::size_t i) const {
321 return datavec.at(i).data();
322 }
323 inline bool empty() const noexcept {
324 return datavec.empty();
325 }
326 inline std::size_t size() const noexcept {
327 return datavec.size();
328 }
329 inline void resize(std::size_t n) {
330 datavec.resize(n);
331 }
332 inline const char* front() const {
333 return datavec.front().data();
334 }
335 inline const char* back() const {
336 return datavec.back().data();
337 }
338 inline char* data() noexcept {
339 return datavec[0].data();
340 }
341 inline const char* data() const noexcept {
342 return datavec[0].data();
343 }
344
345 private:
346 using vector_t = typename std::vector<std::array<char, N>>;
347
348 public:
349 // Use the underlying iterator
350 using iterator = typename vector_t::iterator;
351 using const_iterator = typename vector_t::const_iterator;
352 using reverse_iterator = typename vector_t::reverse_iterator;
353 using const_reverse_iterator = typename vector_t::const_reverse_iterator;
354 using value_type = typename vector_t::value_type;
355
356 inline iterator begin() noexcept {
357 return datavec.begin();
358 }
359 inline iterator end() noexcept {
360 return datavec.end();
361 }
362 inline const_iterator begin() const noexcept {
363 return datavec.begin();
364 }
365 inline const_iterator cbegin() const noexcept {
366 return datavec.cbegin();
367 }
368 inline const_iterator end() const noexcept {
369 return datavec.end();
370 }
371 inline const_iterator cend() const noexcept {
372 return datavec.cend();
373 }
374 inline reverse_iterator rbegin() noexcept {
375 return datavec.rbegin();
376 }
377 inline reverse_iterator rend() noexcept {
378 return datavec.rend();
379 }
380 inline const_reverse_iterator rbegin() const noexcept {
381 return datavec.rbegin();
382 }
383 inline const_reverse_iterator rend() const noexcept {
384 return datavec.rend();
385 }
386
387 private:
388 vector_t datavec;
389};
390
391} // namespace HighFive
392
393
406#define HIGHFIVE_REGISTER_TYPE(type, function) \
407 template <> \
408 inline HighFive::DataType HighFive::create_datatype<type>() { \
409 return function(); \
410 }
411
create an HDF5 DataType from a C++ type
Definition H5DataType.hpp:117
AtomicType()
Definition H5DataType_misc.hpp:215
T basic_type
Definition H5DataType.hpp:121
Class representing an attribute of a dataset or group.
Definition H5Attribute.hpp:46
Create a compound HDF5 datatype.
Definition H5DataType.hpp:128
void commit(const Object &object, const std::string &name) const
Commit datatype into the given Object.
Definition H5DataType_misc.hpp:368
const std::vector< member_def > & getMembers() const noexcept
Get read access to the CompoundType members.
Definition H5DataType.hpp:192
CompoundType(const CompoundType &other)=default
CompoundType(const std::initializer_list< member_def > &t_members, size_t size=0)
Definition H5DataType.hpp:156
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:148
CompoundType(DataType &&type)
Initializes a compound type from a DataType.
Definition H5DataType.hpp:164
CompoundType(std::vector< member_def > &&t_members, size_t size=0)
Definition H5DataType.hpp:152
Class representing a dataset.
Definition H5DataSet.hpp:30
Exception specific to HighFive DataType interface.
Definition H5Exception.hpp:94
HDF5 Data Type.
Definition H5DataType.hpp:54
bool operator==(const DataType &other) const
Definition H5DataType_misc.hpp:47
bool isFixedLenStr() const
Returns whether the type is a fixed-length string.
Definition H5DataType_misc.hpp:63
DataTypeCreateProps getCreatePropertyList() const
Get the list of properties for creation of this DataType.
Definition H5DataType.hpp:98
size_t getSize() const
Returns the length (in bytes) of this type elements.
Definition H5DataType_misc.hpp:43
bool isVariableStr() const
Returns whether the type is a variable-length string.
Definition H5DataType_misc.hpp:55
bool empty() const noexcept
Check the DataType was default constructed. Such value might represent auto-detection of the datatype...
Definition H5DataType_misc.hpp:35
Object()
Definition H5Object_misc.hpp:24
std::string string() const
Returns a friendly description of the type (e.g. Float32)
Definition H5DataType_misc.hpp:71
DataTypeClass getClass() const
Return the fundamental type.
Definition H5DataType_misc.hpp:39
bool isReference() const
Returns whether the type is a Reference.
Definition H5DataType_misc.hpp:67
bool operator!=(const DataType &other) const
Definition H5DataType_misc.hpp:51
Create a enum HDF5 datatype.
Definition H5DataType.hpp:228
EnumType(std::initializer_list< member_def > t_members)
Definition H5DataType.hpp:252
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:389
EnumType(const std::vector< member_def > &t_members)
Definition H5DataType.hpp:242
File class.
Definition H5File.hpp:24
A structure representing a set of fixed-length strings.
Definition H5DataType.hpp:284
const char * front() const
Definition H5DataType.hpp:332
const_iterator cend() const noexcept
Definition H5DataType.hpp:371
const_reverse_iterator rend() const noexcept
Definition H5DataType.hpp:383
const_iterator begin() const noexcept
Definition H5DataType.hpp:362
const char * data() const noexcept
Definition H5DataType.hpp:341
typename vector_t::const_iterator const_iterator
Definition H5DataType.hpp:351
const_reverse_iterator rbegin() const noexcept
Definition H5DataType.hpp:380
const_iterator end() const noexcept
Definition H5DataType.hpp:368
const char * at(std::size_t i) const
Definition H5DataType.hpp:320
const char * back() const
Definition H5DataType.hpp:335
reverse_iterator rend() noexcept
Definition H5DataType.hpp:377
char * data() noexcept
Definition H5DataType.hpp:338
void resize(std::size_t n)
Definition H5DataType.hpp:329
reverse_iterator rbegin() noexcept
Definition H5DataType.hpp:374
std::string getString(std::size_t index) const
Retrieve a string from the structure as std::string.
Definition H5DataType_misc.hpp:266
const char * operator[](std::size_t i) const noexcept
Definition H5DataType.hpp:317
typename vector_t::value_type value_type
Definition H5DataType.hpp:354
std::size_t size() const noexcept
Definition H5DataType.hpp:326
iterator begin() noexcept
Definition H5DataType.hpp:356
void push_back(const std::string &)
Append an std::string to the buffer structure.
Definition H5DataType_misc.hpp:252
const_iterator cbegin() const noexcept
Definition H5DataType.hpp:365
iterator end() noexcept
Definition H5DataType.hpp:359
bool empty() const noexcept
Definition H5DataType.hpp:323
typename vector_t::reverse_iterator reverse_iterator
Definition H5DataType.hpp:352
typename vector_t::const_reverse_iterator const_reverse_iterator
Definition H5DataType.hpp:353
typename vector_t::iterator iterator
Definition H5DataType.hpp:350
Definition H5Object.hpp:54
Object()
Definition H5Object_misc.hpp:24
hid_t _hid
Definition H5Object.hpp:105
HDF5 property Lists.
Definition H5PropertyList.hpp:79
An HDF5 (object) reference type.
Definition H5Reference.hpp:33
Definition H5_definitions.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:479
DataType create_datatype()
Create a DataType instance representing type T.
Definition H5DataType_misc.hpp:472
DataTypeClass operator|(DataTypeClass lhs, DataTypeClass rhs)
Definition H5DataType.hpp:40
DataTypeClass operator&(DataTypeClass lhs, DataTypeClass rhs)
Definition H5DataType.hpp:45
DataTypeClass
Enum of Fundamental data classes.
Definition H5DataType.hpp:25
Use for defining a sub-type of compound type.
Definition H5DataType.hpp:132
size_t offset
Definition H5DataType.hpp:139
member_def(std::string t_name, DataType t_base_type, size_t t_offset=0)
Definition H5DataType.hpp:133
DataType base_type
Definition H5DataType.hpp:138
std::string name
Definition H5DataType.hpp:137
Use for defining a member of enum type.
Definition H5DataType.hpp:232
T value
Definition H5DataType.hpp:237
std::string name
Definition H5DataType.hpp:236
member_def(const std::string &t_name, T t_value)
Definition H5DataType.hpp:233
static void ToException(const std::string &prefix_msg)
Definition H5Exception_misc.hpp:42