echelon  0.8.0
hdf5/group.hpp
1 // Copyright (c) 2012-2014 Christopher Hinz
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef ECHELON_HDF5_GROUP_HPP
7 #define ECHELON_HDF5_GROUP_HPP
8 
9 #include <echelon/hdf5/object.hpp>
10 #include <echelon/hdf5/type.hpp>
11 #include <echelon/hdf5/type_factory.hpp>
12 #include <echelon/hdf5/precursor/group.hpp>
13 #include <echelon/hdf5/attribute_repository.hpp>
14 #include <echelon/hdf5/dataset.hpp>
15 #include <echelon/hdf5/scalar_dataset.hpp>
16 #include <echelon/hdf5/object_reference.hpp>
17 #include <echelon/hdf5/utility.hpp>
18 #include <echelon/hdf5/link.hpp>
19 
20 #include <echelon/hdf5/broken_contract_exception.hpp>
21 
22 #include <echelon/hdf5/precursor/dataspace.hpp> // for unlimited
23 
24 #include <string>
25 #include <vector>
26 #include <map>
27 #include <memory>
28 #include <exception>
29 #include <functional>
30 
31 namespace echelon
32 {
33 namespace hdf5
34 {
35 class file;
36 
37 using precursor::unlimited;
38 
41 class non_existing_member_exception : public std::exception
42 {
43 public:
48  non_existing_member_exception(std::string what_) : what_(std::move(what_))
49  {
50  }
51 
55  {
56  }
57 
60  const char* what() const noexcept override
61  {
62  return what_.c_str();
63  }
64 
65 private:
66  std::string what_;
67 };
68 
73 {
74 public:
82  {
83  auto_chunking_ = value;
84 
85  return *this;
86  }
87 
95  {
96  compression_level_ = value;
97 
98  return *this;
99  }
100 
104  {
105  shuffle_filter_ = value;
106 
107  return *this;
108  }
109 
116  dataset_options& chunk_shape(std::vector<hsize_t> value)
117  {
118  chunk_shape_ = std::move(value);
119 
120  return *this;
121  }
122 
125  bool auto_chunking() const
126  {
127  return auto_chunking_;
128  }
129 
132  int compression_level() const
133  {
134  return compression_level_;
135  }
136 
139  bool shuffle_filter() const
140  {
141  return shuffle_filter_;
142  }
143 
146  const std::vector<hsize_t>& chunk_shape() const
147  {
148  return chunk_shape_;
149  }
150 
151 private:
152  bool auto_chunking_ = false;
153  bool shuffle_filter_ = false;
154  int compression_level_ = -1;
155  std::vector<hsize_t> chunk_shape_ = {};
156 };
157 
161 class group
162 {
163 public:
166  using native_handle_type = hdf5::precursor::group;
167 
168  friend class file;
169 
172  group() = default;
173 
174  explicit group(native_handle_type group_wrapper_);
175 
181  group create_group(const std::string& name);
182 
198  dataset create_dataset(const std::string& name, const type& datatype,
199  const std::vector<hsize_t>& dims, const dataset_options& options = {});
200 
221  dataset create_dataset(const std::string& name, const type& datatype,
222  const std::vector<hsize_t>& dims, const std::vector<hsize_t>& max_dims,
223  const dataset_options& options = {});
224 
242  template <typename T>
243  dataset create_dataset(const std::string& name, const std::vector<hsize_t>& dims,
244  const dataset_options& options = {})
245  {
246  return create_dataset(name, get_hdf5_type<T>(), dims, options);
247  }
248 
271  template <typename T>
272  dataset create_dataset(const std::string& name, const std::vector<hsize_t>& dims,
273  const std::vector<hsize_t>& max_dims,const dataset_options& options = {})
274  {
275  return create_dataset(name, get_hdf5_type<T>(), dims, max_dims, options);
276  }
277 
285  scalar_dataset create_scalar_dataset(const std::string& name, const type& datatype);
286 
296  template <typename T>
297  scalar_dataset create_scalar_dataset(const std::string& name)
298  {
299  return create_scalar_dataset(name, get_hdf5_type<T>());
300  }
301 
313  template <typename T>
314  scalar_dataset create_scalar_dataset(const std::string& name, const T& value)
315  {
316  scalar_dataset ds = create_scalar_dataset<T>(name);
317 
318  ds <<= value;
319 
320  return ds;
321  }
322 
329  object operator[](const std::string& name) const;
330 
336  void remove(const std::string& name) const;
337 
346  group require_group(const std::string& name);
347 
378  dataset require_dataset(const std::string& name, const type& datatype,
379  const std::vector<hsize_t>& dims, const dataset_options& options = {});
380 
413  template <typename T>
414  dataset require_dataset(const std::string& name, const std::vector<hsize_t>& dims,
415  const dataset_options& options = {})
416  {
417  return require_dataset(name, get_hdf5_type<T>(), dims, options);
418  }
419 
442  scalar_dataset require_scalar_dataset(const std::string& name, const type& datatype);
443 
468  template <typename T>
469  scalar_dataset require_scalar_dataset(const std::string& name)
470  {
471  return require_scalar_dataset(name, get_hdf5_type<T>());
472  }
473 
500  template <typename T>
501  scalar_dataset require_scalar_dataset(const std::string& name, const T& value)
502  {
503  type datatype = get_hdf5_type<T>();
504 
505  if (exists(*this, name) &&
506  get_object_type_by_name(*this, name) == object_type::scalar_dataset)
507  {
508  scalar_dataset ds(hdf5::precursor::dataset(native_handle().id(), name,
509  hdf5::precursor::default_property_list));
510 
511  if (ds.datatype() != datatype)
512  throw broken_contract_exception("The required datatype doesn't "
513  "match the datatype of the dataset.");
514 
515  return ds;
516  }
517  else
518  {
519  scalar_dataset ds = create_scalar_dataset(name, datatype);
520 
521  ds <<= value;
522 
523  return ds;
524  }
525  }
526 
532  void iterate_links(const std::function<void(const link&)>& op) const;
533 
539  void visit_links(const std::function<void(const link&)>& visitor) const;
540 
547  void visit_objects(const std::function<void(const object&)>& visitor) const;
548 
551  object_reference ref() const;
552 
555  const native_handle_type& native_handle() const;
556 
559  explicit operator bool() const;
560 private:
561  friend class constructor_access;
562  friend class object;
563 
564  enum class creation_mode
565  {
566  open,
567  create
568  };
569 
570  explicit group(const file& loc, const std::string& name = "/");
571  group(const object& parent, const std::string& name, creation_mode mode);
572 
573  dataset create_dataset(const std::string& name, const type& datatype,
574  const std::vector<hsize_t>& dims, const std::vector<hsize_t>& max_dims,
575  int comp_level, bool auto_chunking, bool shuffle_filter,
576  const std::vector<hsize_t> chunk_shape);
577 
578  hdf5::precursor::group group_wrapper_;
579 
580 public:
583  attribute_repository<group> attributes() const;
584 };
585 }
586 }
587 
588 #endif
echelon&#39;s core namespace
Definition: attribute.cpp:10
A handle to an HDF5 dataset.
Definition: hdf5/dataset.hpp:62
~non_existing_member_exception() noexcept
The destructor.
Definition: hdf5/group.hpp:54
Polymorphic handle to an HDF5 object.
Definition: hdf5/object.hpp:57
scalar_dataset create_scalar_dataset(const std::string &name)
Creates a new HDF5 scalar dataset within this group.
Definition: hdf5/group.hpp:297
scalar_dataset require_scalar_dataset(const std::string &name)
Returns the requested scalar dataset, if it already exists, otherwise the scalar dataset is created...
Definition: hdf5/group.hpp:469
const std::vector< hsize_t > & chunk_shape() const
chunk shape
Definition: hdf5/group.hpp:146
dataset_options & auto_chunking(bool value)
Enables/Disables auto-chunking.
Definition: hdf5/group.hpp:81
dataset create_dataset(const std::string &name, const std::vector< hsize_t > &dims, const dataset_options &options={})
Creates a new HDF5 dataset within this group.
Definition: hdf5/group.hpp:243
A handle to an HDF5 group object.
Definition: hdf5/group.hpp:161
dataset create_dataset(const std::string &name, const std::vector< hsize_t > &dims, const std::vector< hsize_t > &max_dims, const dataset_options &options={})
Creates a new HDF5 dataset within this group.
Definition: hdf5/group.hpp:272
Exception, which is thrown, if a requested object does not exist.
Definition: hdf5/group.hpp:41
A handle to an HDF5 file object.
Definition: hdf5/file.hpp:26
type datatype() const
The value type of the scalar dataset.
Definition: hdf5/scalar_dataset.cpp:27
dataset require_dataset(const std::string &name, const std::vector< hsize_t > &dims, const dataset_options &options={})
Returns the requested dataset, if it already exists, otherwise a new dataset is created.
Definition: hdf5/group.hpp:414
dataset_options & compression_level(int value)
Sets the gzip compression level (0 - 9) of the dataset.
Definition: hdf5/group.hpp:94
non_existing_member_exception(std::string what_)
Creates a new exception with a given error description.
Definition: hdf5/group.hpp:48
hdf5::precursor::group native_handle_type
Type of the underlying HDF5 low-level handle.
Definition: hdf5/group.hpp:166
A handle to an HDF5 type.
Definition: hdf5/type.hpp:23
Attribute manager, which should be embedded into a parent object, which supports attributes.
Definition: hdf5/attribute_repository.hpp:52
scalar_dataset require_scalar_dataset(const std::string &name, const T &value)
Returns the requested scalar dataset, if it already exists, otherwise a new scalar dataset is created...
Definition: hdf5/group.hpp:501
bool auto_chunking() const
auto-chunking option
Definition: hdf5/group.hpp:125
dataset_options & chunk_shape(std::vector< hsize_t > value)
Sets the chunk shape of the dataset.
Definition: hdf5/group.hpp:116
dataset_options & shuffle_filter(bool value)
Enables/disables the shuffle filter.
Definition: hdf5/group.hpp:103
scalar_dataset create_scalar_dataset(const std::string &name, const T &value)
Creates a new HDF5 scalar dataset within this group and initializes it with a given value...
Definition: hdf5/group.hpp:314
A reference to an HDF5 object.
Definition: hdf5/object_reference.hpp:25
int compression_level() const
gzip compression level
Definition: hdf5/group.hpp:132
A handle to an HDF5 scalar dataset.
Definition: hdf5/scalar_dataset.hpp:27
Additional options for the dataset creation.
Definition: hdf5/group.hpp:72
bool shuffle_filter() const
shuffle filter option
Definition: hdf5/group.hpp:139
Exception, which is thrown, if a require_*-type method can&#39;t fulfill the contract.
Definition: broken_contract_exception.hpp:20
const char * what() const noexceptoverride
An associated error description.
Definition: hdf5/group.hpp:60