summaryrefslogtreecommitdiffstats
path: root/ext/boost/boost/exception/diagnostic_information.hpp
blob: 01f7b35823d3b0f2879b2f8222cc26ad6307f938 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc.

//Distributed under the Boost Software License, Version 1.0. (See accompanying
//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef UUID_0552D49838DD11DD90146B8956D89593
#define UUID_0552D49838DD11DD90146B8956D89593

#include <boost/config.hpp>
#include <boost/exception/get_error_info.hpp>
#include <boost/exception/detail/exception_ptr_base.hpp>
#include <boost/utility/enable_if.hpp>
#include <exception>
#include <sstream>
#include <string>

namespace
boost
    {
    namespace
    exception_detail
        {
        template <class T>
        struct
        enable_boost_exception_overload
            {
            struct yes { char q[100]; };
            typedef char no;
            static yes check(exception const *);
            static no check(...);
            enum e { value=sizeof(check((T*)0))==sizeof(yes) };
            };

        template <class T>
        struct
        enable_std_exception_overload
            {
            struct yes { char q[100]; };
            typedef char no;
            static yes check(std::exception const *);
            static no check(...);
            enum e { value = !enable_boost_exception_overload<T>::value && sizeof(check((T*)0))==sizeof(yes) };
            };

        inline
        char const *
        get_diagnostic_information( exception const & x )
            {
            if( error_info_container * c=x.data_.get() )
#ifndef BOOST_NO_EXCEPTIONS
                try
                    {
#endif
                    return c->diagnostic_information();
#ifndef BOOST_NO_EXCEPTIONS
                    }
                catch(...)
                    {
                    }
#endif
            return 0;
            }

        inline
        std::string
        diagnostic_information_impl( boost::exception const * be, std::exception const * se )
            {
            BOOST_ASSERT(be||se);
#ifndef BOOST_NO_RTTI
            if( !se )
                se = dynamic_cast<std::exception const *>(be);
            if( !be )
                be = dynamic_cast<boost::exception const *>(se);
#endif
            std::ostringstream tmp;
            if( be )
                {
                if( char const * const * f=get_error_info<throw_file>(*be) )
                    {
                    tmp << *f;
                    if( int const * l=get_error_info<throw_line>(*be) )
                        tmp << '(' << *l << "): ";
                    }
                tmp << "Throw in function ";
                if( char const * const * fn=get_error_info<throw_function>(*be) )
                    tmp << *fn;
                else
                    tmp << "(unknown)";
                tmp << '\n';
                }
#ifndef BOOST_NO_RTTI
            tmp << std::string("Dynamic exception type: ") <<
                (be?BOOST_EXCEPTION_DYNAMIC_TYPEID(*be):BOOST_EXCEPTION_DYNAMIC_TYPEID(*se)).name() << '\n';
#endif
            if( se )
                tmp << "std::exception::what: " << se->what() << '\n';
            if( be )
                if( char const * s=exception_detail::get_diagnostic_information(*be) )
                    if( *s )
                        tmp << s;
            return tmp.str();
            }
        }

    template <class T>
    inline
    typename enable_if<exception_detail::enable_boost_exception_overload<T>,std::string>::type
    diagnostic_information( T const & e )
        {
        return exception_detail::diagnostic_information_impl(&e,0);
        }

    template <class T>
    inline
    typename enable_if<exception_detail::enable_std_exception_overload<T>,std::string>::type
    diagnostic_information( T const & e )
        {
        return exception_detail::diagnostic_information_impl(0,&e);
        }
    }

#ifndef BOOST_NO_EXCEPTIONS
#include <boost/exception/current_exception_cast.hpp>
namespace
boost
    {
    inline
    std::string
    current_exception_diagnostic_information()
        {
        boost::exception const * be=current_exception_cast<boost::exception const>();
        std::exception const * se=current_exception_cast<std::exception const>();
        if( be || se )
            return exception_detail::diagnostic_information_impl(be,se);
        else
            return "No diagnostic information available.";
        }

    inline
    std::string
    diagnostic_information( exception_detail::exception_ptr_base const & p )
        {
        if( !p._empty() )
            try
                {
                p._rethrow();
                }
            catch(
            ... )
                {
                return current_exception_diagnostic_information();
                }
        return "<empty>";
        }
    }
#endif

#endif