STXXL  1.4.0
 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 
29 
31 
32 #if STXXL_MSVC
33  #define STXXL_PRETTY_FUNCTION_NAME __FUNCTION__
34 #else
35  #define STXXL_PRETTY_FUNCTION_NAME __PRETTY_FUNCTION__
36 #endif
37 
38 ////////////////////////////////////////////////////////////////////////////
39 
40 //! Throws exception_type with "Error in [location] : [error_message]"
41 #define STXXL_THROW2(exception_type, location, error_message) \
42  do { \
43  std::ostringstream msg; \
44  msg << "Error in " << location << " : " << error_message; \
45  throw exception_type(msg.str()); \
46  } while (false)
47 
48 //! Throws exception_type with "Error in [function] : [error_message]"
49 #define STXXL_THROW(exception_type, error_message) \
50  STXXL_THROW2(exception_type, \
51  STXXL_PRETTY_FUNCTION_NAME, \
52  error_message)
53 
54 //! Throws exception_type with "Error in [function] : [error_message] : [errno_value message]"
55 #define STXXL_THROW_ERRNO2(exception_type, error_message, errno_value) \
56  STXXL_THROW2(exception_type, \
57  STXXL_PRETTY_FUNCTION_NAME, \
58  error_message << " : " << strerror(errno_value))
59 
60 //! Throws exception_type with "Error in [function] : [error_message] : [errno message]"
61 #define STXXL_THROW_ERRNO(exception_type, error_message) \
62  STXXL_THROW_ERRNO2(exception_type, error_message, errno)
63 
64 //! Throws std::invalid_argument with "Error in [function] : [error_message]"
65 #define STXXL_THROW_INVALID_ARGUMENT(error_message) \
66  STXXL_THROW2(std::invalid_argument, \
67  STXXL_PRETTY_FUNCTION_NAME, \
68  error_message)
69 
70 //! Throws stxxl::unreachable with "Error in file [file], line [line] : this code should never be reachable"
71 #define STXXL_THROW_UNREACHABLE() \
72  STXXL_THROW2(stxxl::unreachable, \
73  "file " << __FILE__ << ", line " << __LINE__, \
74  "this code should never be reachable")
75 
76 ////////////////////////////////////////////////////////////////////////////
77 
78 //! Throws exception_type if (expr) with "Error in [function] : [error_message]"
79 #define STXXL_THROW_IF(expr, exception_type, error_message) \
80  do { \
81  if (expr) { \
82  STXXL_THROW(exception_type, error_message); \
83  } \
84  } while (false)
85 
86 //! Throws exception_type if (expr != 0) with "Error in [function] : [error_message]"
87 #define STXXL_THROW_NE_0(expr, exception_type, error_message) \
88  STXXL_THROW_IF((expr) != 0, exception_type, error_message)
89 
90 //! Throws exception_type if (expr == 0) with "Error in [function] : [error_message]"
91 #define STXXL_THROW_EQ_0(expr, exception_type, error_message) \
92  STXXL_THROW_IF((expr) == 0, exception_type, error_message)
93 
94 //! Throws exception_type if (expr < 0) with "Error in [function] : [error_message]"
95 #define STXXL_THROW_LT_0(expr, exception_type, error_message) \
96  STXXL_THROW_IF((expr) < 0, exception_type, error_message)
97 
98 ////////////////////////////////////////////////////////////////////////////
99 
100 //! Throws exception_type if (expr) with "Error in [function] : [error_message] : [errno message]"
101 #define STXXL_THROW_ERRNO_IF(expr, exception_type, error_message) \
102  do { \
103  if (expr) { \
104  STXXL_THROW_ERRNO(exception_type, error_message); \
105  } \
106  } while (false)
107 
108 //! Throws exception_type if (expr != 0) with "Error in [function] : [error_message] : [errno message]"
109 #define STXXL_THROW_ERRNO_NE_0(expr, exception_type, error_message) \
110  STXXL_THROW_ERRNO_IF((expr) != 0, exception_type, error_message)
111 
112 //! Throws exception_type if (expr == 0) with "Error in [function] : [error_message] : [errno message]"
113 #define STXXL_THROW_ERRNO_EQ_0(expr, exception_type, error_message) \
114  STXXL_THROW_ERRNO_IF((expr) == 0, exception_type, error_message)
115 
116 //! Throws exception_type if (expr < 0) with "Error in [function] : [error_message] : [errno message]"
117 #define STXXL_THROW_ERRNO_LT_0(expr, exception_type, error_message) \
118  STXXL_THROW_ERRNO_IF((expr) < 0, exception_type, error_message)
119 
120 ////////////////////////////////////////////////////////////////////////////
121 
122 //! Checks pthread call, if return != 0, throws stxxl::resource_error with "Error in [function] : [pthread_expr] : [errno message]
123 #define STXXL_CHECK_PTHREAD_CALL(expr) \
124  do { \
125  int res = (expr); \
126  if (res != 0) { \
127  STXXL_THROW_ERRNO2(stxxl::resource_error, #expr, res); \
128  } \
129  } while (false)
130 
131 ////////////////////////////////////////////////////////////////////////////
132 
133 #if STXXL_WINDOWS || defined(__MINGW32__)
134 
135 //! Throws exception_type with "Error in [function] : [error_message] : [formatted GetLastError()]"
136 #define STXXL_THROW_WIN_LASTERROR(exception_type, error_message) \
137  do { \
138  LPVOID lpMsgBuf; \
139  DWORD dw = GetLastError(); \
140  FormatMessage( \
141  FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, \
142  NULL, dw, \
143  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), \
144  (LPTSTR)&lpMsgBuf, \
145  0, NULL); \
146  std::ostringstream msg; \
147  msg << "Error in " << STXXL_PRETTY_FUNCTION_NAME \
148  << " : " << error_message \
149  << " : error code " << dw << " : " << ((char*)lpMsgBuf); \
150  LocalFree(lpMsgBuf); \
151  throw exception_type(msg.str()); \
152  } while (false)
153 
154 #endif
155 
156 ////////////////////////////////////////////////////////////////////////////
157 
159 
160 #endif // !STXXL_COMMON_ERROR_HANDLING_HEADER
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
#define STXXL_END_NAMESPACE
Definition: namespace.h:17