Stxxl  1.4.0
include/stxxl/bits/common/error_handling.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  include/stxxl/bits/common/error_handling.h
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2007-2010 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
00007  *
00008  *  Distributed under the Boost Software License, Version 1.0.
00009  *  (See accompanying file LICENSE_1_0.txt or copy at
00010  *  http://www.boost.org/LICENSE_1_0.txt)
00011  **************************************************************************/
00012 
00013 #ifndef STXXL_ERROR_HANDLING_HEADER
00014 #define STXXL_ERROR_HANDLING_HEADER
00015 
00016 #include <sstream>
00017 #include <cerrno>
00018 #include <cstring>
00019 
00020 #include <stxxl/bits/namespace.h>
00021 #include <stxxl/bits/common/exceptions.h>
00022 
00023 
00024 __STXXL_BEGIN_NAMESPACE
00025 
00026 #define _STXXL_STRING(x) # x
00027 
00028 #ifdef BOOST_MSVC
00029  #define STXXL_PRETTY_FUNCTION_NAME __FUNCTION__
00030 #else
00031  #define STXXL_PRETTY_FUNCTION_NAME __PRETTY_FUNCTION__
00032 #endif
00033 
00034 ////////////////////////////////////////////////////////////////////////////
00035 
00036 #define STXXL_THROW(exception_type, location, error_message) \
00037     do { \
00038         std::ostringstream msg_; \
00039         msg_ << "Error in " << location << ": " << error_message; \
00040         throw exception_type(msg_.str()); \
00041     } while (false)
00042 
00043 #define STXXL_THROW2(exception_type, error_message) \
00044     STXXL_THROW(exception_type, "function " << STXXL_PRETTY_FUNCTION_NAME, \
00045                 "Info: " << error_message << " " << strerror(errno))
00046 
00047 #define STXXL_THROW_INVALID_ARGUMENT(error_message) \
00048     STXXL_THROW(std::invalid_argument, \
00049                 "function " << STXXL_PRETTY_FUNCTION_NAME, \
00050                 error_message)
00051 
00052 #define STXXL_THROW_UNREACHABLE() \
00053     STXXL_THROW(stxxl::unreachable, \
00054                 "file: " << __FILE__ << ", line: " << __LINE__, \
00055                 "must be unreachable code")
00056 
00057 ////////////////////////////////////////////////////////////////////////////
00058 
00059 template <typename E>
00060 inline void stxxl_util_function_error(const char * func_name, const char * expr = 0, const char * error = 0)
00061 {
00062     std::ostringstream str_;
00063     str_ << "Error in function " << func_name << " " << (expr ? expr : strerror(errno));
00064     if (error)
00065         str_ << " " << error;
00066     throw E(str_.str());
00067 }
00068 
00069 #define stxxl_function_error(exception_type) \
00070     stxxl::stxxl_util_function_error<exception_type>(STXXL_PRETTY_FUNCTION_NAME)
00071 
00072 template <typename E>
00073 inline bool helper_check_success(bool success, const char * func_name, const char * expr = 0, const char * error = 0)
00074 {
00075     if (!success)
00076         stxxl_util_function_error<E>(func_name, expr, error);
00077     return success;
00078 }
00079 
00080 template <typename E, typename INT>
00081 inline bool helper_check_eq_0(INT res, const char * func_name, const char * expr, bool res_2_strerror = false)
00082 {
00083     return helper_check_success<E>(res == 0, func_name, expr, res_2_strerror ? strerror(res) : 0);
00084 }
00085 
00086 #define check_pthread_call(expr) \
00087     stxxl::helper_check_eq_0<stxxl::resource_error>(expr, STXXL_PRETTY_FUNCTION_NAME, _STXXL_STRING(expr), true)
00088 
00089 template <typename E, typename INT>
00090 inline bool helper_check_ge_0(INT res, const char * func_name)
00091 {
00092     return helper_check_success<E>(res >= 0, func_name);
00093 }
00094 
00095 #define stxxl_check_ge_0(expr, exception_type) \
00096     stxxl::helper_check_ge_0<exception_type>(expr, STXXL_PRETTY_FUNCTION_NAME)
00097 
00098 template <typename E, typename INT>
00099 inline bool helper_check_ne_0(INT res, const char * func_name)
00100 {
00101     return helper_check_success<E>(res != 0, func_name);
00102 }
00103 
00104 #define stxxl_check_ne_0(expr, exception_type) \
00105     stxxl::helper_check_ne_0<exception_type>(expr, STXXL_PRETTY_FUNCTION_NAME)
00106 
00107 __STXXL_END_NAMESPACE
00108 
00109 #endif // !STXXL_ERROR_HANDLING_HEADER
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines