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