h5gt 0.2.0
C++ wrapper for HDF5 library (based on HighFive project)
Loading...
Searching...
No Matches
H5DataType.hpp
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#ifndef H5DATATYPE_HPP
10#define H5DATATYPE_HPP
11
12#include <vector>
13
14#include "H5Object.hpp"
15#include "bits/H5Utils.hpp"
16
17namespace h5gt {
18
19
23enum class DataTypeClass {
24 Time,
25 Integer,
26 Float,
27 String,
28 BitField,
29 Opaque,
30 Compound,
31 Reference,
32 Enum,
33 VarLen,
34 Array,
35 Invalid
36};
37
38enum Endian {
39 Native,
40 Little,
41 Big
42};
43
44
48class DataType : public Object {
49public:
50
51 DataType() = default;
52
56 virtual bool isTypeEqual(const DataType& other) const;
57
61 DataTypeClass getClass() const;
62
69 size_t getSize() const;
70
74 std::string string() const;
75
79 bool isVariableStr() const;
80
84 bool isFixedLenStr() const;
85
90 bool empty() const noexcept;
91
93 bool isReference() const;
94
98 bool operator==(const DataType& other) const;
99 bool operator!=(const DataType& other) const;
100
101 static DataType FromId(const hid_t& id, const bool& increaseRefCount = false){
102 Object obj = Object(id, ObjectType::UserDataType, increaseRefCount);
103 return DataType(obj);
104 };
105
106protected:
107 DataType(const Object& obj) : Object(obj){};
108 using Object::Object;
109
110 friend class Attribute;
111 friend class File;
112 friend class Group;
113 friend class DataSet;
114 friend class NodeTraits<File>;
115 friend class NodeTraits<Group>;
116};
117
123template <typename T>
124class AtomicType : public DataType {
125public:
126 AtomicType(Endian endian = Endian::Native);
127
128 typedef T basic_type;
129
130 static AtomicType FromId(const hid_t& id, const bool& increaseRefCount = false){
131 DataType obj = DataType::FromId(id, increaseRefCount);
132 return AtomicType(obj);
133 };
134
135protected:
136 AtomicType(const DataType& obj) : DataType(obj){};
137};
138
139
143class CompoundType : public DataType {
144public:
149 struct member_def {
150 member_def(std::string t_name, DataType t_base_type, size_t t_offset = 0)
151 : name(std::move(t_name))
152 , base_type(std::move(t_base_type))
153 , offset(t_offset) {}
154 std::string name;
155 DataType base_type;
156 size_t offset;
157 };
158
159 CompoundType(const CompoundType& other) = default;
160
165 inline CompoundType(const std::vector<member_def>& t_members, size_t size = 0)
166 : members(t_members) {
167 create(size);
168 }
170 inline CompoundType(std::vector<member_def>&& t_members, size_t size = 0)
171 : members(std::move(t_members)) {
172 create(size);
173 }
175 inline CompoundType(const std::initializer_list<member_def>& t_members,
176 size_t size = 0)
177 : members(t_members) {
178 create(size);
179 }
180
184 inline void commit(const Object& object, const std::string& name) const;
185
187 inline const std::vector<member_def>& getMembers() const noexcept {
188 return members;
189 }
190
191 DataTypeClass getMemberClass(const unsigned& member_no) const;
192 int getMemberIndex(const std::string& field_name) const;
193 std::string getMemberName(const unsigned& field_idx) const;
194 size_t getMemberOffset(const unsigned& memb_no) const;
196 hid_t getMemberType(const unsigned& field_idx) const;
197 int getNMembers() const;
198
199
200 static CompoundType FromId(const hid_t& id, const bool& increaseRefCount = false){
201 DataType obj = DataType::FromId(id, increaseRefCount);
202 return CompoundType(obj);
203 };
204
205protected:
206 CompoundType(const DataType& obj) : DataType(obj){};
207
208private:
209
211 std::vector<member_def> members;
212
216 void create(size_t size = 0);
217};
218
240template<typename T>
241class EnumType: public DataType {
242public:
245 struct member_def {
246 member_def(const std::string& t_name, T t_value)
247 : name(t_name)
248 , value(std::move(t_value)) {}
249 std::string name;
250 T value;
251 };
252
253 EnumType(const EnumType& other) = default;
254
255 EnumType(const std::vector<member_def>& t_members)
256 : members(t_members) {
257 create();
258 }
259
260 EnumType(std::initializer_list<member_def> t_members)
261 : EnumType(std::vector<member_def>({t_members})) {}
262
266 void commit(const Object& object, const std::string& name) const;
267
268 int getMemberIndex(const std::string& field_name) const;
269 std::string getMemberName(const unsigned& field_idx) const;
270 void getMemberValue(const unsigned& memb_no, void *value) const;
271 int getNMembers() const;
272
273 static EnumType FromId(const hid_t& id, const bool& increaseRefCount = false){
274 DataType obj = DataType::FromId(id, increaseRefCount);
275 return EnumType(obj);
276 };
277
278protected:
279 EnumType(const DataType& obj) : DataType(obj){};
280
281private:
282 std::vector<member_def> members;
283
284 void create();
285};
286
287
289template <typename T>
290DataType create_datatype();
291
292
294template <typename T>
295DataType create_and_check_datatype();
296
297
304template <std::size_t N>
306public:
307 FixedLenStringArray() = default;
308
312 FixedLenStringArray(const char array[][N], std::size_t length);
313
319 explicit FixedLenStringArray(const std::vector<std::string> & vec);
320
321 FixedLenStringArray(const std::string* iter_begin, const std::string* iter_end);
322
323 FixedLenStringArray(const std::initializer_list<std::string> &);
324
328 void push_back(const std::string&);
329
330 void push_back(const std::array<char, N>&);
331
335 std::string getString(std::size_t index) const;
336
337 // Container interface
338 inline const char* operator[](std::size_t i) const noexcept {
339 return datavec[i].data();
340 }
341 inline const char* at(std::size_t i) const {
342 return datavec.at(i).data();
343 }
344 inline bool empty() const noexcept {
345 return datavec.empty();
346 }
347 inline std::size_t size() const noexcept {
348 return datavec.size();
349 }
350 inline void resize(std::size_t n) {
351 datavec.resize(n);
352 }
353 inline const char* front() const {
354 return datavec.front().data();
355 }
356 inline const char* back() const {
357 return datavec.back().data();
358 }
359 inline char* data() noexcept {
360 return datavec[0].data();
361 }
362 inline const char* data() const noexcept {
363 return datavec[0].data();
364 }
365
366private:
367 using vector_t = typename std::vector<std::array<char, N>>;
368
369public:
370 // Use the underlying iterator
371 using iterator = typename vector_t::iterator;
372 using const_iterator = typename vector_t::const_iterator;
373 using reverse_iterator = typename vector_t::reverse_iterator;
374 using const_reverse_iterator = typename vector_t::const_reverse_iterator;
375 using value_type = typename vector_t::value_type;
376
377 inline iterator begin() noexcept {
378 return datavec.begin();
379 }
380 inline iterator end() noexcept {
381 return datavec.end();
382 }
383 inline const_iterator begin() const noexcept {
384 return datavec.begin();
385 }
386 inline const_iterator cbegin() const noexcept {
387 return datavec.cbegin();
388 }
389 inline const_iterator end() const noexcept {
390 return datavec.end();
391 }
392 inline const_iterator cend() const noexcept {
393 return datavec.cend();
394 }
395 inline reverse_iterator rbegin() noexcept {
396 return datavec.rbegin();
397 }
398 inline reverse_iterator rend() noexcept {
399 return datavec.rend();
400 }
401 inline const_reverse_iterator rbegin() const noexcept {
402 return datavec.rbegin();
403 }
404 inline const_reverse_iterator rend() const noexcept {
405 return datavec.rend();
406 }
407
408private:
409 vector_t datavec;
410};
411
412} // namespace h5gt
413
414
433#define H5GT_REGISTER_TYPE(type, function) \
434 template<> \
435 inline h5gt::DataType h5gt::create_datatype<type>() { \
436 return function(); \
437 } \
438
439#include "bits/H5DataType_misc.hpp"
440
441#endif // H5DATATYPE_HPP
create an HDF5 DataType from a C++ type
Definition H5DataType.hpp:124
Class representing an attribute of a dataset or group.
Definition H5Attribute.hpp:22
Create a compound HDF5 datatype.
Definition H5DataType.hpp:143
void commit(const Object &object, const std::string &name) const
Commit datatype into the given Object.
Definition H5DataType_misc.hpp:454
CompoundType(const std::initializer_list< member_def > &t_members, size_t size=0)
Definition H5DataType.hpp:175
const std::vector< member_def > & getMembers() const noexcept
Get read access to the CompoundType members.
Definition H5DataType.hpp:187
CompoundType(std::vector< member_def > &&t_members, size_t size=0)
Definition H5DataType.hpp:170
hid_t getMemberType(const unsigned &field_idx) const
return predefined type specified by hdf5 macro like: H5T_C_S1, H5T_NATIVE_INT, H5T_IEEE_F32BE etc.
Definition H5DataType_misc.hpp:474
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:165
Class representing a dataset.
Definition H5DataSet.hpp:28
HDF5 Data Type.
Definition H5DataType.hpp:48
size_t getSize() const
Returns the length (in bytes) of this type elements.
Definition H5DataType_misc.hpp:36
std::string string() const
Returns a friendly description of the type (e.g. Float32)
Definition H5DataType_misc.hpp:80
bool operator==(const DataType &other) const
operator == Check if objects reside in the same file and equal to each other
Definition H5DataType_misc.hpp:72
bool isReference() const
Returns whether the type is a Reference.
Definition H5DataType_misc.hpp:68
bool empty() const noexcept
Check the DataType was default constructed. Such value might represent auto-detection of the datatype...
Definition H5DataType_misc.hpp:28
DataTypeClass getClass() const
Return the fundamental type.
Definition H5DataType_misc.hpp:32
virtual bool isTypeEqual(const DataType &other) const
isTypeEqual Unlike == operator this only checks if the data types are equal and do not check if they ...
Definition H5DataType_misc.hpp:42
bool isVariableStr() const
Returns whether the type is a variable-length string.
Definition H5DataType_misc.hpp:55
bool isFixedLenStr() const
Returns whether the type is a fixed-length string.
Definition H5DataType_misc.hpp:64
Create a enum HDF5 datatype.
Definition H5DataType.hpp:241
void commit(const Object &object, const std::string &name) const
Commit datatype into the given Object.
Definition H5DataType_misc.hpp:503
File class.
Definition H5File.hpp:25
A structure representing a set of fixed-length strings.
Definition H5DataType.hpp:305
void push_back(const std::string &)
Append an std::string to the buffer structure.
Definition H5DataType_misc.hpp:382
std::string getString(std::size_t index) const
Retrieve a string from the structure as std::string.
Definition H5DataType_misc.hpp:396
Represents an hdf5 group.
Definition H5Group.hpp:23
NodeTraits: Base class for Group and File.
Definition H5Node_traits.hpp:23
Definition H5Object.hpp:55
Use for defining a sub-type of compound type If your dtype contains 'std::string' then you need to ex...
Definition H5DataType.hpp:149
Use for defining a member of enum type.
Definition H5DataType.hpp:245