HighFive  2.3.1
HighFive - Header-only C++ HDF5 interface
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 #ifndef H5DATATYPE_HPP
10 #define H5DATATYPE_HPP
11 
12 #include <vector>
13 
14 #include "H5Object.hpp"
15 #include "bits/H5Utils.hpp"
16 
17 namespace HighFive {
18 
19 
23 enum 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 
38 
42 class DataType : public Object {
43  public:
44 
45  bool operator==(const DataType& other) const;
46 
47  bool operator!=(const DataType& other) const;
48 
52  DataTypeClass getClass() const;
53 
60  size_t getSize() const;
61 
65  std::string string() const;
66 
70  bool isVariableStr() const;
71 
75  bool isFixedLenStr() const;
76 
81  bool empty() const noexcept;
82 
84  bool isReference() const;
85 
86  protected:
87  using Object::Object;
88 
89  friend class Attribute;
90  friend class File;
91  friend class DataSet;
92 };
93 
99 template <typename T>
100 class AtomicType : public DataType {
101  public:
102  AtomicType();
103 
104  typedef T basic_type;
105 };
106 
107 
111 class CompoundType : public DataType {
112 public:
115  struct member_def {
116  member_def(std::string t_name, DataType t_base_type, size_t t_offset = 0)
117  : name(std::move(t_name))
118  , base_type(std::move(t_base_type))
119  , offset(t_offset) {}
120  std::string name;
122  size_t offset;
123  };
124 
125  CompoundType(const CompoundType& other) = default;
126 
131  inline CompoundType(const std::vector<member_def>& t_members, size_t size = 0)
132  : members(t_members) {
133  create(size);
134  }
135  inline CompoundType(std::vector<member_def>&& t_members, size_t size = 0)
136  : members(std::move(t_members)) {
137  create(size);
138  }
139  inline CompoundType(const std::initializer_list<member_def>& t_members,
140  size_t size = 0)
141  : members(t_members) {
142  create(size);
143  }
144 
148  inline void commit(const Object& object, const std::string& name) const;
149 
151  inline const std::vector<member_def>& getMembers() const noexcept {
152  return members;
153  }
154 
155 private:
156 
158  std::vector<member_def> members;
159 
163  void create(size_t size = 0);
164 };
165 
187 template<typename T>
188 class EnumType: public DataType {
189 public:
192  struct member_def {
193  member_def(const std::string& t_name, T t_value)
194  : name(t_name)
195  , value(std::move(t_value)) {}
196  std::string name;
197  T value;
198  };
199 
200  EnumType(const EnumType& other) = default;
201 
202  EnumType(const std::vector<member_def>& t_members)
203  : members(t_members) {
204  create();
205  }
206 
207  EnumType(std::initializer_list<member_def> t_members)
208  : EnumType(std::vector<member_def>({t_members})) {}
209 
213  void commit(const Object& object, const std::string& name) const;
214 
215 private:
216  std::vector<member_def> members;
217 
218  void create();
219 };
220 
221 
223 template <typename T>
224 DataType create_datatype();
225 
226 
228 template <typename T>
229 DataType create_and_check_datatype();
230 
231 
238 template <std::size_t N>
240  public:
241  FixedLenStringArray() = default;
242 
246  FixedLenStringArray(const char array[][N], std::size_t length);
247 
253  explicit FixedLenStringArray(const std::vector<std::string> & vec);
254 
255  FixedLenStringArray(const std::string* iter_begin, const std::string* iter_end);
256 
257  FixedLenStringArray(const std::initializer_list<std::string> &);
258 
262  void push_back(const std::string&);
263 
264  void push_back(const std::array<char, N>&);
265 
269  std::string getString(std::size_t index) const;
270 
271  // Container interface
272  inline const char* operator[](std::size_t i) const noexcept {
273  return datavec[i].data();
274  }
275  inline const char* at(std::size_t i) const {
276  return datavec.at(i).data();
277  }
278  inline bool empty() const noexcept {
279  return datavec.empty();
280  }
281  inline std::size_t size() const noexcept {
282  return datavec.size();
283  }
284  inline void resize(std::size_t n) {
285  datavec.resize(n);
286  }
287  inline const char* front() const {
288  return datavec.front().data();
289  }
290  inline const char* back() const {
291  return datavec.back().data();
292  }
293  inline char* data() noexcept {
294  return datavec[0].data();
295  }
296  inline const char* data() const noexcept {
297  return datavec[0].data();
298  }
299 
300  private:
301  using vector_t = typename std::vector<std::array<char, N>>;
302 
303  public:
304  // Use the underlying iterator
305  using iterator = typename vector_t::iterator;
306  using const_iterator = typename vector_t::const_iterator;
307  using reverse_iterator = typename vector_t::reverse_iterator;
308  using const_reverse_iterator = typename vector_t::const_reverse_iterator;
309  using value_type = typename vector_t::value_type;
310 
311  inline iterator begin() noexcept {
312  return datavec.begin();
313  }
314  inline iterator end() noexcept {
315  return datavec.end();
316  }
317  inline const_iterator begin() const noexcept {
318  return datavec.begin();
319  }
320  inline const_iterator cbegin() const noexcept {
321  return datavec.cbegin();
322  }
323  inline const_iterator end() const noexcept {
324  return datavec.end();
325  }
326  inline const_iterator cend() const noexcept {
327  return datavec.cend();
328  }
329  inline reverse_iterator rbegin() noexcept {
330  return datavec.rbegin();
331  }
332  inline reverse_iterator rend() noexcept {
333  return datavec.rend();
334  }
335  inline const_reverse_iterator rbegin() const noexcept {
336  return datavec.rbegin();
337  }
338  inline const_reverse_iterator rend() const noexcept {
339  return datavec.rend();
340  }
341 
342  private:
343  vector_t datavec;
344 };
345 
346 } // namespace HighFive
347 
348 
361 #define HIGHFIVE_REGISTER_TYPE(type, function) \
362  namespace HighFive { \
363  template<> \
364  DataType create_datatype<type>() { \
365  return function(); \
366  } \
367  }
368 
369 #include "bits/H5DataType_misc.hpp"
370 
371 #endif // H5DATATYPE_HPP
create an HDF5 DataType from a C++ type
Definition: H5DataType.hpp:100
T basic_type
Definition: H5DataType.hpp:104
AtomicType()
Definition: H5DataType_misc.hpp:181
Class representing an attribute of a dataset or group.
Definition: H5Attribute.hpp:25
Create a compound HDF5 datatype.
Definition: H5DataType.hpp:111
void commit(const Object &object, const std::string &name) const
Commit datatype into the given Object.
Definition: H5DataType_misc.hpp:320
CompoundType(const CompoundType &other)=default
CompoundType(const std::initializer_list< member_def > &t_members, size_t size=0)
Definition: H5DataType.hpp:139
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:131
CompoundType(std::vector< member_def > &&t_members, size_t size=0)
Definition: H5DataType.hpp:135
const std::vector< member_def > & getMembers() const noexcept
Get read access to the CompoundType members.
Definition: H5DataType.hpp:151
Class representing a dataset.
Definition: H5DataSet.hpp:31
HDF5 Data Type.
Definition: H5DataType.hpp:42
bool operator==(const DataType &other) const
Definition: H5DataType_misc.hpp:40
bool isFixedLenStr() const
Returns whether the type is a fixed-length string.
Definition: H5DataType_misc.hpp:57
size_t getSize() const
Returns the length (in bytes) of this type elements.
Definition: H5DataType_misc.hpp:36
bool isVariableStr() const
Returns whether the type is a variable-length string.
Definition: H5DataType_misc.hpp:48
bool empty() const noexcept
Check the DataType was default constructed. Such value might represent auto-detection of the datatype...
Definition: H5DataType_misc.hpp:28
Object()
Definition: H5Object_misc.hpp:16
std::string string() const
Returns a friendly description of the type (e.g. Float32)
Definition: H5DataType_misc.hpp:65
DataTypeClass getClass() const
Return the fundamental type.
Definition: H5DataType_misc.hpp:32
bool isReference() const
Returns whether the type is a Reference.
Definition: H5DataType_misc.hpp:61
bool operator!=(const DataType &other) const
Definition: H5DataType_misc.hpp:44
Create a enum HDF5 datatype.
Definition: H5DataType.hpp:188
EnumType(std::initializer_list< member_def > t_members)
Definition: H5DataType.hpp:207
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:343
EnumType(const std::vector< member_def > &t_members)
Definition: H5DataType.hpp:202
File class.
Definition: H5File.hpp:26
A structure representing a set of fixed-length strings.
Definition: H5DataType.hpp:239
char * data() noexcept
Definition: H5DataType.hpp:293
const_iterator cend() const noexcept
Definition: H5DataType.hpp:326
const_reverse_iterator rend() const noexcept
Definition: H5DataType.hpp:338
const_iterator begin() const noexcept
Definition: H5DataType.hpp:317
typename vector_t::const_iterator const_iterator
Definition: H5DataType.hpp:306
const_reverse_iterator rbegin() const noexcept
Definition: H5DataType.hpp:335
const_iterator end() const noexcept
Definition: H5DataType.hpp:323
const char * operator[](std::size_t i) const noexcept
Definition: H5DataType.hpp:272
reverse_iterator rend() noexcept
Definition: H5DataType.hpp:332
const char * data() const noexcept
Definition: H5DataType.hpp:296
void resize(std::size_t n)
Definition: H5DataType.hpp:284
reverse_iterator rbegin() noexcept
Definition: H5DataType.hpp:329
const char * back() const
Definition: H5DataType.hpp:290
std::string getString(std::size_t index) const
Retrieve a string from the structure as std::string.
Definition: H5DataType_misc.hpp:234
typename vector_t::value_type value_type
Definition: H5DataType.hpp:309
std::size_t size() const noexcept
Definition: H5DataType.hpp:281
iterator begin() noexcept
Definition: H5DataType.hpp:311
void push_back(const std::string &)
Append an std::string to the buffer structure.
Definition: H5DataType_misc.hpp:220
const_iterator cbegin() const noexcept
Definition: H5DataType.hpp:320
iterator end() noexcept
Definition: H5DataType.hpp:314
bool empty() const noexcept
Definition: H5DataType.hpp:278
typename vector_t::reverse_iterator reverse_iterator
Definition: H5DataType.hpp:307
const char * front() const
Definition: H5DataType.hpp:287
typename vector_t::const_reverse_iterator const_reverse_iterator
Definition: H5DataType.hpp:308
typename vector_t::iterator iterator
Definition: H5DataType.hpp:305
const char * at(std::size_t i) const
Definition: H5DataType.hpp:275
Definition: H5Object.hpp:36
Object()
Definition: H5Object_misc.hpp:16
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:434
DataType create_datatype()
Create a DataType instance representing type T.
Definition: H5DataType_misc.hpp:427
DataTypeClass
Enum of Fundamental data classes.
Definition: H5DataType.hpp:23
Use for defining a sub-type of compound type.
Definition: H5DataType.hpp:115
size_t offset
Definition: H5DataType.hpp:122
member_def(std::string t_name, DataType t_base_type, size_t t_offset=0)
Definition: H5DataType.hpp:116
DataType base_type
Definition: H5DataType.hpp:121
std::string name
Definition: H5DataType.hpp:120
Use for defining a member of enum type.
Definition: H5DataType.hpp:192
T value
Definition: H5DataType.hpp:197
std::string name
Definition: H5DataType.hpp:196
member_def(const std::string &t_name, T t_value)
Definition: H5DataType.hpp:193