]> git.lyx.org Git - lyx.git/blob - boost/boost/exception/diagnostic_information.hpp
boost: add eol property
[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, char const * header )
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(header);
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, bool with_what )
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             char const * wh=0;
76             if( with_what && se )
77                 {
78                 wh=se->what();
79                 if( be && exception_detail::get_diagnostic_information(*be,0)==wh )
80                     return wh;
81                 }
82             std::ostringstream tmp;
83             if( be )
84                 {
85                 if( char const * const * f=get_error_info<throw_file>(*be) )
86                     {
87                     tmp << *f;
88                     if( int const * l=get_error_info<throw_line>(*be) )
89                         tmp << '(' << *l << "): ";
90                     }
91                 tmp << "Throw in function ";
92                 if( char const * const * fn=get_error_info<throw_function>(*be) )
93                     tmp << *fn;
94                 else
95                     tmp << "(unknown)";
96                 tmp << '\n';
97                 }
98 #ifndef BOOST_NO_RTTI
99             tmp << std::string("Dynamic exception type: ") <<
100                 (be?BOOST_EXCEPTION_DYNAMIC_TYPEID(*be):BOOST_EXCEPTION_DYNAMIC_TYPEID(*se)).name() << '\n';
101 #endif
102             if( with_what && se )
103                 tmp << "std::exception::what: " << wh << '\n';
104             if( be )
105                 if( char const * s=exception_detail::get_diagnostic_information(*be,tmp.str().c_str()) )
106                     if( *s )
107                         return s;
108             return tmp.str();
109             }
110         }
111
112     template <class T>
113     inline
114     typename enable_if<exception_detail::enable_boost_exception_overload<T>,std::string>::type
115     diagnostic_information( T const & e )
116         {
117         return exception_detail::diagnostic_information_impl(&e,0,true);
118         }
119
120     template <class T>
121     inline
122     typename enable_if<exception_detail::enable_std_exception_overload<T>,std::string>::type
123     diagnostic_information( T const & e )
124         {
125         return exception_detail::diagnostic_information_impl(0,&e,true);
126         }
127
128     inline
129     char const *
130     diagnostic_information_what( exception const & e ) throw()
131         {
132         char const * w=0;
133 #ifndef BOOST_NO_EXCEPTIONS
134         try
135             {
136 #endif
137             (void) exception_detail::diagnostic_information_impl(&e,0,false);
138             return exception_detail::get_diagnostic_information(e,0);
139 #ifndef BOOST_NO_EXCEPTIONS
140             }
141         catch(
142         ... )
143             {
144             }
145 #endif
146         return w;
147         }
148     }
149
150 #ifndef BOOST_NO_EXCEPTIONS
151 #include <boost/exception/current_exception_cast.hpp>
152 namespace
153 boost
154     {
155     inline
156     std::string
157     current_exception_diagnostic_information()
158         {
159         boost::exception const * be=current_exception_cast<boost::exception const>();
160         std::exception const * se=current_exception_cast<std::exception const>();
161         if( be || se )
162             return exception_detail::diagnostic_information_impl(be,se,true);
163         else
164             return "No diagnostic information available.";
165         }
166
167     inline
168     std::string
169     diagnostic_information( exception_detail::exception_ptr_base const & p )
170         {
171         if( !p._empty() )
172             try
173                 {
174                 p._rethrow();
175                 }
176             catch(
177             ... )
178                 {
179                 return current_exception_diagnostic_information();
180                 }
181         return "<empty>";
182         }
183     }
184 #endif
185
186 #endif