]> git.lyx.org Git - lyx.git/blob - boost/boost/exception/diagnostic_information.hpp
boost: update to version 1.40
[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
9 #include <boost/config.hpp>
10 #include <boost/exception/get_error_info.hpp>
11 #include <boost/exception/detail/exception_ptr_base.hpp>
12 #include <boost/utility/enable_if.hpp>
13 #include <exception>
14 #include <sstream>
15 #include <string>
16
17 namespace
18 boost
19     {
20     namespace
21     exception_detail
22         {
23         template <class T>
24         struct
25         enable_boost_exception_overload
26             {
27             struct yes { char q[100]; };
28             typedef char no;
29             static yes check(exception const *);
30             static no check(...);
31             enum e { value=sizeof(check((T*)0))==sizeof(yes) };
32             };
33
34         template <class T>
35         struct
36         enable_std_exception_overload
37             {
38             struct yes { char q[100]; };
39             typedef char no;
40             static yes check(std::exception const *);
41             static no check(...);
42             enum e { value = !enable_boost_exception_overload<T>::value && sizeof(check((T*)0))==sizeof(yes) };
43             };
44
45         inline
46         char const *
47         get_diagnostic_information( exception const & x )
48             {
49             if( error_info_container * c=x.data_.get() )
50 #ifndef BOOST_NO_EXCEPTIONS
51                 try
52                     {
53 #endif
54                     return c->diagnostic_information();
55 #ifndef BOOST_NO_EXCEPTIONS
56                     }
57                 catch(...)
58                     {
59                     }
60 #endif
61             return 0;
62             }
63
64         inline
65         std::string
66         diagnostic_information_impl( boost::exception const * be, std::exception const * se )
67             {
68             BOOST_ASSERT(be||se);
69 #ifndef BOOST_NO_RTTI
70             if( !se )
71                 se = dynamic_cast<std::exception const *>(be);
72             if( !be )
73                 be = dynamic_cast<boost::exception const *>(se);
74 #endif
75             std::ostringstream tmp;
76             if( be )
77                 {
78                 if( char const * const * f=get_error_info<throw_file>(*be) )
79                     {
80                     tmp << *f;
81                     if( int const * l=get_error_info<throw_line>(*be) )
82                         tmp << '(' << *l << "): ";
83                     }
84                 tmp << "Throw in function ";
85                 if( char const * const * fn=get_error_info<throw_function>(*be) )
86                     tmp << *fn;
87                 else
88                     tmp << "(unknown)";
89                 tmp << '\n';
90                 }
91 #ifndef BOOST_NO_RTTI
92             tmp << std::string("Dynamic exception type: ") <<
93                 (be?BOOST_EXCEPTION_DYNAMIC_TYPEID(*be):BOOST_EXCEPTION_DYNAMIC_TYPEID(*se)).name() << '\n';
94 #endif
95             if( se )
96                 tmp << "std::exception::what: " << se->what() << '\n';
97             if( be )
98                 if( char const * s=exception_detail::get_diagnostic_information(*be) )
99                     if( *s )
100                         tmp << s;
101             return tmp.str();
102             }
103         }
104
105     template <class T>
106     inline
107     typename enable_if<exception_detail::enable_boost_exception_overload<T>,std::string>::type
108     diagnostic_information( T const & e )
109         {
110         return exception_detail::diagnostic_information_impl(&e,0);
111         }
112
113     template <class T>
114     inline
115     typename enable_if<exception_detail::enable_std_exception_overload<T>,std::string>::type
116     diagnostic_information( T const & e )
117         {
118         return exception_detail::diagnostic_information_impl(0,&e);
119         }
120     }
121
122 #ifndef BOOST_NO_EXCEPTIONS
123 #include <boost/exception/current_exception_cast.hpp>
124 namespace
125 boost
126     {
127     inline
128     std::string
129     current_exception_diagnostic_information()
130         {
131         boost::exception const * be=current_exception_cast<boost::exception const>();
132         std::exception const * se=current_exception_cast<std::exception const>();
133         if( be || se )
134             return exception_detail::diagnostic_information_impl(be,se);
135         else
136             return "No diagnostic information available.";
137         }
138
139     inline
140     std::string
141     diagnostic_information( exception_detail::exception_ptr_base const & p )
142         {
143         if( !p._empty() )
144             try
145                 {
146                 p._rethrow();
147                 }
148             catch(
149             ... )
150                 {
151                 return current_exception_diagnostic_information();
152                 }
153         return "<empty>";
154         }
155     }
156 #endif
157
158 #endif