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