]> git.lyx.org Git - lyx.git/blob - boost/boost/exception/diagnostic_information.hpp
boost: update to 1.42.0
[lyx.git] / boost / boost / exception / diagnostic_information.hpp
1 //Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.
2
3 //Distributed under the Boost Software License, Version 1.0. (See accompanying
4 //file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5
6 #ifndef UUID_0552D49838DD11DD90146B8956D89593
7 #define UUID_0552D49838DD11DD90146B8956D89593
8 #if defined(__GNUC__) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
9 #pragma GCC system_header
10 #endif
11 #if defined(_MSC_VER) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
12 #pragma warning(push,1)
13 #endif
14
15 #include <boost/config.hpp>
16 #include <boost/exception/get_error_info.hpp>
17 #include <boost/utility/enable_if.hpp>
18 #include <boost/config.hpp>
19 #include <exception>
20 #include <sstream>
21 #include <string>
22
23 #ifndef BOOST_NO_EXCEPTIONS
24 #include <boost/exception/current_exception_cast.hpp>
25 namespace
26 boost
27     {
28     namespace
29     exception_detail
30         {
31         std::string diagnostic_information_impl( boost::exception const *, std::exception const *, bool );
32         }
33
34     inline
35     std::string
36     current_exception_diagnostic_information()
37         {
38         boost::exception const * be=current_exception_cast<boost::exception const>();
39         std::exception const * se=current_exception_cast<std::exception const>();
40         if( be || se )
41             return exception_detail::diagnostic_information_impl(be,se,true);
42         else
43             return "No diagnostic information available.";
44         }
45     }
46 #endif
47
48 namespace
49 boost
50     {
51     namespace
52     exception_detail
53         {
54         inline
55         exception const *
56         get_boost_exception( exception const * e )
57             {
58             return e;
59             }
60
61         inline
62         exception const *
63         get_boost_exception( ... )
64             {
65             return 0;
66             }
67
68         inline
69         std::exception const *
70         get_std_exception( std::exception const * e )
71             {
72             return e;
73             }
74
75         inline
76         std::exception const *
77         get_std_exception( ... )
78             {
79             return 0;
80             }
81
82         inline
83         char const *
84         get_diagnostic_information( exception const & x, char const * header )
85             {
86             if( error_info_container * c=x.data_.get() )
87 #ifndef BOOST_NO_EXCEPTIONS
88                 try
89                     {
90 #endif
91                     return c->diagnostic_information(header);
92 #ifndef BOOST_NO_EXCEPTIONS
93                     }
94                 catch(...)
95                     {
96                     }
97 #endif
98             return 0;
99             }
100
101         inline
102         std::string
103         diagnostic_information_impl( boost::exception const * be, std::exception const * se, bool with_what )
104             {
105             if( !be && !se )
106                 return "Unknown exception.";
107 #ifndef BOOST_NO_RTTI
108             if( !be )
109                 be=dynamic_cast<boost::exception const *>(se);
110             if( !se )
111                 se=dynamic_cast<std::exception const *>(be);
112 #endif
113             char const * wh=0;
114             if( with_what && se )
115                 {
116                 wh=se->what();
117                 if( be && exception_detail::get_diagnostic_information(*be,0)==wh )
118                     return wh;
119                 }
120             std::ostringstream tmp;
121             if( be )
122                 {
123                 if( char const * const * f=get_error_info<throw_file>(*be) )
124                     {
125                     tmp << *f;
126                     if( int const * l=get_error_info<throw_line>(*be) )
127                         tmp << '(' << *l << "): ";
128                     }
129                 tmp << "Throw in function ";
130                 if( char const * const * fn=get_error_info<throw_function>(*be) )
131                     tmp << *fn;
132                 else
133                     tmp << "(unknown)";
134                 tmp << '\n';
135                 }
136 #ifndef BOOST_NO_RTTI
137             tmp << std::string("Dynamic exception type: ") <<
138                 (be?BOOST_EXCEPTION_DYNAMIC_TYPEID(*be):BOOST_EXCEPTION_DYNAMIC_TYPEID(*se)).type_.name() << '\n';
139 #endif
140             if( with_what && se )
141                 tmp << "std::exception::what: " << wh << '\n';
142             if( be )
143                 if( char const * s=exception_detail::get_diagnostic_information(*be,tmp.str().c_str()) )
144                     if( *s )
145                         return s;
146             return tmp.str();
147             }
148         }
149
150     template <class T>
151     std::string
152     diagnostic_information( T const & e )
153         {
154         return exception_detail::diagnostic_information_impl(exception_detail::get_boost_exception(&e),exception_detail::get_std_exception(&e),true);
155         }
156
157     inline
158     char const *
159     diagnostic_information_what( exception const & e ) throw()
160         {
161         char const * w=0;
162 #ifndef BOOST_NO_EXCEPTIONS
163         try
164             {
165 #endif
166             (void) exception_detail::diagnostic_information_impl(&e,0,false);
167             return exception_detail::get_diagnostic_information(e,0);
168 #ifndef BOOST_NO_EXCEPTIONS
169             }
170         catch(
171         ... )
172             {
173             }
174 #endif
175         return w;
176         }
177     }
178
179 #if defined(_MSC_VER) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
180 #pragma warning(pop)
181 #endif
182 #endif