STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
error_handling.h
Go to the documentation of this file.
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  * Copyright (C) 2013 Timo Bingmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #ifndef STXXL_COMMON_ERROR_HANDLING_HEADER
15 #define STXXL_COMMON_ERROR_HANDLING_HEADER
16 
17 /** \file error_handling.h
18  * Macros for convenient error checking and reporting via exception.
19  */
20 
21 #include <sstream>
22 #include <cerrno>
23 #include <cstring>
24 
25 #include <stxxl/bits/namespace.h>
27 #include <stxxl/bits/config.h>
28 
30 
31 #if STXXL_MSVC
32  #define STXXL_PRETTY_FUNCTION_NAME __FUNCTION__
33 #else
34  #define STXXL_PRETTY_FUNCTION_NAME __PRETTY_FUNCTION__
35 #endif
36 
37 ////////////////////////////////////////////////////////////////////////////
38 
39 //! Throws exception_type with "Error in [location] : [error_message]"
40 #define STXXL_THROW2(exception_type, location, error_message) \
41  do { \
42  std::ostringstream msg; \
43  msg << "Error in " << location << " : " << error_message; \
44  throw exception_type(msg.str()); \
45  } while (false)
46 
47 //! Throws exception_type with "Error in [function] : [error_message]"
48 #define STXXL_THROW(exception_type, error_message) \
49  STXXL_THROW2(exception_type, \
50  STXXL_PRETTY_FUNCTION_NAME, \
51  error_message)
52 
53 //! Throws exception_type with "Error in [function] : [error_message] : [errno_value message]"
54 #define STXXL_THROW_ERRNO2(exception_type, error_message, errno_value) \
55  STXXL_THROW2(exception_type, \
56  STXXL_PRETTY_FUNCTION_NAME, \
57  error_message << " : " << strerror(errno_value))
58 
59 //! Throws exception_type with "Error in [function] : [error_message] : [errno message]"
60 #define STXXL_THROW_ERRNO(exception_type, error_message) \
61  STXXL_THROW_ERRNO2(exception_type, error_message, errno)
62 
63 //! Throws std::invalid_argument with "Error in [function] : [error_message]"
64 #define STXXL_THROW_INVALID_ARGUMENT(error_message) \
65  STXXL_THROW2(std::invalid_argument, \
66  STXXL_PRETTY_FUNCTION_NAME, \
67  error_message)
68 
69 //! Throws stxxl::unreachable with "Error in file [file], line [line] : this code should never be reachable"
70 #define STXXL_THROW_UNREACHABLE() \
71  STXXL_THROW2(stxxl::unreachable, \
72  "file " << __FILE__ << ", line " << __LINE__, \
73  "this code should never be reachable")
74 
75 ////////////////////////////////////////////////////////////////////////////
76 
77 //! Throws exception_type if (expr) with "Error in [function] : [error_message]"
78 #define STXXL_THROW_IF(expr, exception_type, error_message) \
79  do { \
80  if (expr) { \
81  STXXL_THROW(exception_type, error_message); \
82  } \
83  } while (false)
84 
85 //! Throws exception_type if (expr != 0) with "Error in [function] : [error_message]"
86 #define STXXL_THROW_NE_0(expr, exception_type, error_message) \
87  STXXL_THROW_IF((expr) != 0, exception_type, error_message)
88 
89 //! Throws exception_type if (expr == 0) with "Error in [function] : [error_message]"
90 #define STXXL_THROW_EQ_0(expr, exception_type, error_message) \
91  STXXL_THROW_IF((expr) == 0, exception_type, error_message)
92 
93 //! Throws exception_type if (expr < 0) with "Error in [function] : [error_message]"
94 #define STXXL_THROW_LT_0(expr, exception_type, error_message) \
95  STXXL_THROW_IF((expr) < 0, exception_type, error_message)
96 
97 ////////////////////////////////////////////////////////////////////////////
98 
99 //! Throws exception_type if (expr) with "Error in [function] : [error_message] : [errno message]"
100 #define STXXL_THROW_ERRNO_IF(expr, exception_type, error_message) \
101  do { \
102  if (expr) { \
103  STXXL_THROW_ERRNO(exception_type, error_message); \
104  } \
105  } while (false)
106 
107 //! Throws exception_type if (expr != 0) with "Error in [function] : [error_message] : [errno message]"
108 #define STXXL_THROW_ERRNO_NE_0(expr, exception_type, error_message) \
109  STXXL_THROW_ERRNO_IF((expr) != 0, exception_type, error_message)
110 
111 //! Throws exception_type if (expr == 0) with "Error in [function] : [error_message] : [errno message]"
112 #define STXXL_THROW_ERRNO_EQ_0(expr, exception_type, error_message) \
113  STXXL_THROW_ERRNO_IF((expr) == 0, exception_type, error_message)
114 
115 //! Throws exception_type if (expr < 0) with "Error in [function] : [error_message] : [errno message]"
116 #define STXXL_THROW_ERRNO_LT_0(expr, exception_type, error_message) \
117  STXXL_THROW_ERRNO_IF((expr) < 0, exception_type, error_message)
118 
119 ////////////////////////////////////////////////////////////////////////////
120 
121 //! Checks pthread call, if return != 0, throws stxxl::resource_error with "Error in [function] : [pthread_expr] : [errno message]
122 #define STXXL_CHECK_PTHREAD_CALL(expr) \
123  do { \
124  int res = (expr); \
125  if (res != 0) { \
126  STXXL_THROW_ERRNO2(stxxl::resource_error, #expr, res); \
127  } \
128  } while (false)
129 
130 ////////////////////////////////////////////////////////////////////////////
131 
132 #if STXXL_WINDOWS || defined(__MINGW32__)
133 
134 //! Throws exception_type with "Error in [function] : [error_message] : [formatted GetLastError()]"
135 #define STXXL_THROW_WIN_LASTERROR(exception_type, error_message) \
136  do { \
137  LPVOID lpMsgBuf; \
138  DWORD dw = GetLastError(); \
139  FormatMessage( \
140  FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, \
141  NULL, dw, \
142  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \
143  (LPTSTR)&lpMsgBuf, \
144  0, NULL); \
145  std::ostringstream msg; \
146  msg << "Error in " << STXXL_PRETTY_FUNCTION_NAME \
147  << " : " << error_message \
148  << " : error code " << dw << " : " << ((char*)lpMsgBuf); \
149  LocalFree(lpMsgBuf); \
150  throw exception_type(msg.str()); \
151  } while (false)
152 
153 #endif
154 
155 ////////////////////////////////////////////////////////////////////////////
156 
158 
159 #endif // !STXXL_COMMON_ERROR_HANDLING_HEADER
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
#define STXXL_END_NAMESPACE
Definition: namespace.h:17