annotate DEPENDENCIES/generic/include/boost/detail/catch_exceptions.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
Chris@16 1 // boost/catch_exceptions.hpp -----------------------------------------------//
Chris@16 2
Chris@16 3 // Copyright Beman Dawes 1995-2001. Distributed under the Boost
Chris@16 4 // Software License, Version 1.0. (See accompanying file
Chris@16 5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 6
Chris@16 7 // See http://www.boost.org/libs/test for documentation.
Chris@16 8
Chris@16 9 // Revision History
Chris@16 10 // 13 Jun 01 report_exception() made inline. (John Maddock, Jesse Jones)
Chris@16 11 // 26 Feb 01 Numerous changes suggested during formal review. (Beman)
Chris@16 12 // 25 Jan 01 catch_exceptions.hpp code factored out of cpp_main.cpp.
Chris@16 13 // 22 Jan 01 Remove test_tools dependencies to reduce coupling.
Chris@16 14 // 5 Nov 00 Initial boost version (Beman Dawes)
Chris@16 15
Chris@16 16 #ifndef BOOST_CATCH_EXCEPTIONS_HPP
Chris@16 17 #define BOOST_CATCH_EXCEPTIONS_HPP
Chris@16 18
Chris@16 19 // header dependencies are deliberately restricted to the standard library
Chris@16 20 // to reduce coupling to other boost libraries.
Chris@16 21 #include <string> // for string
Chris@16 22 #include <new> // for bad_alloc
Chris@16 23 #include <typeinfo> // for bad_cast, bad_typeid
Chris@16 24 #include <exception> // for exception, bad_exception
Chris@16 25 #include <stdexcept> // for std exception hierarchy
Chris@16 26 #include <boost/cstdlib.hpp> // for exit codes
Chris@16 27 # if __GNUC__ != 2 || __GNUC_MINOR__ > 96
Chris@16 28 # include <ostream> // for ostream
Chris@16 29 # else
Chris@16 30 # include <iostream> // workaround GNU missing ostream header
Chris@16 31 # endif
Chris@16 32
Chris@16 33 # if defined(__BORLANDC__) && (__BORLANDC__ <= 0x0551)
Chris@16 34 # define BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT
Chris@16 35 # endif
Chris@16 36
Chris@16 37 #if defined(MPW_CPLUS) && (MPW_CPLUS <= 0x890)
Chris@16 38 # define BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT
Chris@16 39 namespace std { class bad_typeid { }; }
Chris@16 40 # endif
Chris@16 41
Chris@16 42 namespace boost
Chris@16 43 {
Chris@16 44
Chris@16 45 namespace detail
Chris@16 46 {
Chris@16 47 // A separate reporting function was requested during formal review.
Chris@16 48 inline void report_exception( std::ostream & os,
Chris@16 49 const char * name, const char * info )
Chris@16 50 { os << "\n** uncaught exception: " << name << " " << info << std::endl; }
Chris@16 51 }
Chris@16 52
Chris@16 53 // catch_exceptions ------------------------------------------------------//
Chris@16 54
Chris@16 55 template< class Generator > // Generator is function object returning int
Chris@16 56 int catch_exceptions( Generator function_object,
Chris@16 57 std::ostream & out, std::ostream & err )
Chris@16 58 {
Chris@16 59 int result = 0; // quiet compiler warnings
Chris@16 60 bool exception_thrown = true; // avoid setting result for each excptn type
Chris@16 61
Chris@16 62 #ifndef BOOST_NO_EXCEPTIONS
Chris@16 63 try
Chris@16 64 {
Chris@16 65 #endif
Chris@16 66 result = function_object();
Chris@16 67 exception_thrown = false;
Chris@16 68 #ifndef BOOST_NO_EXCEPTIONS
Chris@16 69 }
Chris@16 70
Chris@16 71 // As a result of hard experience with strangely interleaved output
Chris@16 72 // under some compilers, there is a lot of use of endl in the code below
Chris@16 73 // where a simple '\n' might appear to do.
Chris@16 74
Chris@16 75 // The rules for catch & arguments are a bit different from function
Chris@16 76 // arguments (ISO 15.3 paragraphs 18 & 19). Apparently const isn't
Chris@16 77 // required, but it doesn't hurt and some programmers ask for it.
Chris@16 78
Chris@16 79 catch ( const char * ex )
Chris@16 80 { detail::report_exception( out, "", ex ); }
Chris@16 81 catch ( const std::string & ex )
Chris@16 82 { detail::report_exception( out, "", ex.c_str() ); }
Chris@16 83
Chris@16 84 // std:: exceptions
Chris@16 85 catch ( const std::bad_alloc & ex )
Chris@16 86 { detail::report_exception( out, "std::bad_alloc:", ex.what() ); }
Chris@16 87
Chris@16 88 # ifndef BOOST_BUILT_IN_EXCEPTIONS_MISSING_WHAT
Chris@16 89 catch ( const std::bad_cast & ex )
Chris@16 90 { detail::report_exception( out, "std::bad_cast:", ex.what() ); }
Chris@16 91 catch ( const std::bad_typeid & ex )
Chris@16 92 { detail::report_exception( out, "std::bad_typeid:", ex.what() ); }
Chris@16 93 # else
Chris@16 94 catch ( const std::bad_cast & )
Chris@16 95 { detail::report_exception( out, "std::bad_cast", "" ); }
Chris@16 96 catch ( const std::bad_typeid & )
Chris@16 97 { detail::report_exception( out, "std::bad_typeid", "" ); }
Chris@16 98 # endif
Chris@16 99
Chris@16 100 catch ( const std::bad_exception & ex )
Chris@16 101 { detail::report_exception( out, "std::bad_exception:", ex.what() ); }
Chris@16 102 catch ( const std::domain_error & ex )
Chris@16 103 { detail::report_exception( out, "std::domain_error:", ex.what() ); }
Chris@16 104 catch ( const std::invalid_argument & ex )
Chris@16 105 { detail::report_exception( out, "std::invalid_argument:", ex.what() ); }
Chris@16 106 catch ( const std::length_error & ex )
Chris@16 107 { detail::report_exception( out, "std::length_error:", ex.what() ); }
Chris@16 108 catch ( const std::out_of_range & ex )
Chris@16 109 { detail::report_exception( out, "std::out_of_range:", ex.what() ); }
Chris@16 110 catch ( const std::range_error & ex )
Chris@16 111 { detail::report_exception( out, "std::range_error:", ex.what() ); }
Chris@16 112 catch ( const std::overflow_error & ex )
Chris@16 113 { detail::report_exception( out, "std::overflow_error:", ex.what() ); }
Chris@16 114 catch ( const std::underflow_error & ex )
Chris@16 115 { detail::report_exception( out, "std::underflow_error:", ex.what() ); }
Chris@16 116 catch ( const std::logic_error & ex )
Chris@16 117 { detail::report_exception( out, "std::logic_error:", ex.what() ); }
Chris@16 118 catch ( const std::runtime_error & ex )
Chris@16 119 { detail::report_exception( out, "std::runtime_error:", ex.what() ); }
Chris@16 120 catch ( const std::exception & ex )
Chris@16 121 { detail::report_exception( out, "std::exception:", ex.what() ); }
Chris@16 122
Chris@16 123 catch ( ... )
Chris@16 124 { detail::report_exception( out, "unknown exception", "" ); }
Chris@16 125 #endif // BOOST_NO_EXCEPTIONS
Chris@16 126
Chris@16 127 if ( exception_thrown ) result = boost::exit_exception_failure;
Chris@16 128
Chris@16 129 if ( result != 0 && result != exit_success )
Chris@16 130 {
Chris@16 131 out << std::endl << "**** returning with error code "
Chris@16 132 << result << std::endl;
Chris@16 133 err
Chris@16 134 << "********** errors detected; see stdout for details ***********"
Chris@16 135 << std::endl;
Chris@16 136 }
Chris@16 137 #if !defined(BOOST_NO_CPP_MAIN_SUCCESS_MESSAGE)
Chris@16 138 else { out << std::flush << "no errors detected" << std::endl; }
Chris@16 139 #endif
Chris@16 140 return result;
Chris@16 141 } // catch_exceptions
Chris@16 142
Chris@16 143 } // boost
Chris@16 144
Chris@16 145 #endif // BOOST_CATCH_EXCEPTIONS_HPP
Chris@16 146