Alsvinn  0.5.3
The fast FVM simulator with UQ support
hdf5_utils.hpp
Go to the documentation of this file.
1 /* Copyright (c) 2018 ETH Zurich, Kjetil Olsen Lye
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 
16 #pragma once
18 #include <hdf5.h>
22 
23 
24 #define HDF5_SAFE_CALL(x) {\
25  if (x < 0) { \
26  THROW("HDF5 error, call looked like: " << #x); \
27  } \
28 }
29 
32 #define HDF5_MAKE_RESOURCE(holder, expression, closer) { \
33  auto hidValue = expression; \
34  if (hidValue < 0) { \
35  THROW("HDF5 error in running\n\t" << #expression \
36  <<"\n\n" << __FILE__ << ": " << __LINE__); \
37  } \
38  else { \
39  holder.reset(new HDF5Resource(hidValue, closer)); \
40  } \
41 }
42 
43 namespace alsfvm {
44 namespace io {
45 
46 
47 
51 class HDF5Resource {
52 public:
53  typedef herr_t (*delete_function)(hid_t);
54 
69  inline HDF5Resource(hid_t hdf5Resource, delete_function deleter)
70  : hdf5Resource(hdf5Resource), deleter(deleter) {
71  // empty
72  }
73 
74  inline ~HDF5Resource() noexcept(false) {
75  HDF5_SAFE_CALL(deleter(hdf5Resource));
76  }
77 
78  inline hid_t hid() {
79  return hdf5Resource;
80  }
81 
82 
83 
84 private:
85  // We do not want to be able to copy this
86  HDF5Resource(const HDF5Resource& other) : hdf5Resource(0), deleter(NULL) {}
87  void operator=(const HDF5Resource& other) {}
88  const hid_t hdf5Resource;
89  const delete_function deleter;
90 
91 };
92 }
93 }
hid_t hid()
Definition: hdf5_utils.hpp:78
HDF5Resource(hid_t hdf5Resource, delete_function deleter)
HDF5Resource constructs a new HDF5Resource.
Definition: hdf5_utils.hpp:69
#define HDF5_SAFE_CALL(x)
Definition: hdf5_utils.hpp:24
~HDF5Resource() noexcept(false)
Definition: hdf5_utils.hpp:74
Various utility functions to implement the tecno flux.
Definition: types.hpp:30
herr_t(* delete_function)(hid_t)
Definition: hdf5_utils.hpp:53
The HDF5Resource class is a unique_ptr for hdf5 resources.
Definition: hdf5_utils.hpp:51