Stxxl  1.3.2
error_handling.h
1 /***************************************************************************
2  * include/stxxl/bits/common/error_handling.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2007-2010 Andreas Beckmann <[email protected]>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #ifndef STXXL_ERROR_HANDLING_HEADER
14 #define STXXL_ERROR_HANDLING_HEADER
15 
16 #include <sstream>
17 #include <cerrno>
18 #include <cstring>
19 
20 #include <stxxl/bits/namespace.h>
21 #include <stxxl/bits/common/exceptions.h>
22 
23 
24 __STXXL_BEGIN_NAMESPACE
25 
26 #define _STXXL_STRING(x) # x
27 
28 #ifdef BOOST_MSVC
29  #define STXXL_PRETTY_FUNCTION_NAME __FUNCTION__
30 #else
31  #define STXXL_PRETTY_FUNCTION_NAME __PRETTY_FUNCTION__
32 #endif
33 
35 
36 #define STXXL_THROW(exception_type, location, error_message) \
37  do { \
38  std::ostringstream msg_; \
39  msg_ << "Error in " << location << ": " << error_message; \
40  throw exception_type(msg_.str()); \
41  } while (false)
42 
43 #define STXXL_THROW2(exception_type, error_message) \
44  STXXL_THROW(exception_type, "function " << STXXL_PRETTY_FUNCTION_NAME, \
45  "Info: " << error_message << " " << strerror(errno))
46 
47 #define STXXL_THROW_INVALID_ARGUMENT(error_message) \
48  STXXL_THROW(std::invalid_argument, \
49  "function " << STXXL_PRETTY_FUNCTION_NAME, \
50  error_message)
51 
52 #define STXXL_THROW_UNREACHABLE() \
53  STXXL_THROW(stxxl::unreachable, \
54  "file: " << __FILE__ << ", line: " << __LINE__, \
55  "must be unreachable code")
56 
58 
59 template <typename E>
60 inline void stxxl_util_function_error(const char * func_name, const char * expr = 0, const char * error = 0)
61 {
62  std::ostringstream str_;
63  str_ << "Error in function " << func_name << " " << (expr ? expr : strerror(errno));
64  if (error)
65  str_ << " " << error;
66  throw E(str_.str());
67 }
68 
69 #define stxxl_function_error(exception_type) \
70  stxxl::stxxl_util_function_error<exception_type>(STXXL_PRETTY_FUNCTION_NAME)
71 
72 template <typename E>
73 inline bool helper_check_success(bool success, const char * func_name, const char * expr = 0, const char * error = 0)
74 {
75  if (!success)
76  stxxl_util_function_error<E>(func_name, expr, error);
77  return success;
78 }
79 
80 template <typename E, typename INT>
81 inline bool helper_check_eq_0(INT res, const char * func_name, const char * expr, bool res_2_strerror = false)
82 {
83  return helper_check_success<E>(res == 0, func_name, expr, res_2_strerror ? strerror(res) : 0);
84 }
85 
86 #define check_pthread_call(expr) \
87  stxxl::helper_check_eq_0<stxxl::resource_error>(expr, STXXL_PRETTY_FUNCTION_NAME, _STXXL_STRING(expr), true)
88 
89 template <typename E, typename INT>
90 inline bool helper_check_ge_0(INT res, const char * func_name)
91 {
92  return helper_check_success<E>(res >= 0, func_name);
93 }
94 
95 #define stxxl_check_ge_0(expr, exception_type) \
96  stxxl::helper_check_ge_0<exception_type>(expr, STXXL_PRETTY_FUNCTION_NAME)
97 
98 template <typename E, typename INT>
99 inline bool helper_check_ne_0(INT res, const char * func_name)
100 {
101  return helper_check_success<E>(res != 0, func_name);
102 }
103 
104 #define stxxl_check_ne_0(expr, exception_type) \
105  stxxl::helper_check_ne_0<exception_type>(expr, STXXL_PRETTY_FUNCTION_NAME)
106 
107 __STXXL_END_NAMESPACE
108 
109 #endif // !STXXL_ERROR_HANDLING_HEADER