h5gt 0.2.0
C++ wrapper for HDF5 library (based on HighFive project)
Loading...
Searching...
No Matches
H5Exception_misc.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 H5EXCEPTION_MISC_HPP
10#define H5EXCEPTION_MISC_HPP
11
12#include <cstdlib>
13#include <sstream>
14
15#include <H5Epublic.h>
16
17namespace h5gt {
18
20
21 template <typename ExceptionType>
22 static inline herr_t stackWalk(unsigned n, const H5E_error2_t* err_desc,
23 void* client_data) {
24 auto** e_iter = static_cast<ExceptionType**>(client_data);
25 (void)n;
26
27 const char* major_err = H5Eget_major(err_desc->maj_num);
28 const char* minor_err = H5Eget_minor(err_desc->min_num);
29
30 std::ostringstream oss;
31 oss << '(' << major_err << ") " << minor_err;
32
33 auto* e = new ExceptionType(oss.str());
34 e->_err_major = err_desc->maj_num;
35 e->_err_minor = err_desc->min_num;
36 (*e_iter)->_next.reset(e);
37 *e_iter = e;
38 return 0;
39 }
40
41 template <typename ExceptionType>
42 [[noreturn]] static inline void ToException(const std::string& prefix_msg) {
43
44 hid_t err_stack = H5Eget_current_stack();
45 if (err_stack >= 0) {
46 ExceptionType e("");
47 ExceptionType* e_iter = &e;
48
49 H5Ewalk2(err_stack, H5E_WALK_UPWARD,
50 &HDF5ErrMapper::stackWalk<ExceptionType>, &e_iter);
51 H5Eclear2(err_stack);
52
53 const char* next_err_msg = (e.nextException() != NULL)
54 ? (e.nextException()->what())
55 : ("");
56
57 e.setErrorMsg(prefix_msg + " " + next_err_msg);
58 throw e;
59 }
60 // throw generic error, unrecognized error
61 throw ExceptionType(prefix_msg + ": Unknown HDF5 error");
62 }
63};
64
65} // namespace h5gt
66
67#endif // H5OBJECT_MISC_HPP
Definition H5Exception_misc.hpp:19