PageRenderTime 56ms CodeModel.GetById 15ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/exception/diagnostic_information.hpp

http://hadesmem.googlecode.com/
C++ Header | 200 lines | 183 code | 14 blank | 3 comment | 27 complexity | 235d50cd0fe96cbac19cdb1798552cf7 MD5 | raw file
  1//Copyright (c) 2006-2010 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/exception/info.hpp>
 18#include <boost/utility/enable_if.hpp>
 19#ifndef BOOST_NO_RTTI
 20#include <boost/units/detail/utility.hpp>
 21#endif
 22#include <exception>
 23#include <sstream>
 24#include <string>
 25
 26#ifndef BOOST_NO_EXCEPTIONS
 27#include <boost/exception/current_exception_cast.hpp>
 28namespace
 29boost
 30    {
 31    namespace
 32    exception_detail
 33        {
 34        std::string diagnostic_information_impl( boost::exception const *, std::exception const *, bool );
 35        }
 36
 37    inline
 38    std::string
 39    current_exception_diagnostic_information()
 40        {
 41        boost::exception const * be=current_exception_cast<boost::exception const>();
 42        std::exception const * se=current_exception_cast<std::exception const>();
 43        if( be || se )
 44            return exception_detail::diagnostic_information_impl(be,se,true);
 45        else
 46            return "No diagnostic information available.";
 47        }
 48    }
 49#endif
 50
 51namespace
 52boost
 53    {
 54    namespace
 55    exception_detail
 56        {
 57        inline
 58        exception const *
 59        get_boost_exception( exception const * e )
 60            {
 61            return e;
 62            }
 63
 64        inline
 65        exception const *
 66        get_boost_exception( ... )
 67            {
 68            return 0;
 69            }
 70
 71        inline
 72        std::exception const *
 73        get_std_exception( std::exception const * e )
 74            {
 75            return e;
 76            }
 77
 78        inline
 79        std::exception const *
 80        get_std_exception( ... )
 81            {
 82            return 0;
 83            }
 84
 85        inline
 86        char const *
 87        get_diagnostic_information( exception const & x, char const * header )
 88            {
 89#ifndef BOOST_NO_EXCEPTIONS
 90            try
 91                {
 92#endif
 93                error_info_container * c=x.data_.get();
 94                if( !c )
 95                    x.data_.adopt(c=new exception_detail::error_info_container_impl);
 96                char const * di=c->diagnostic_information(header);
 97                BOOST_ASSERT(di!=0);
 98                return di;
 99#ifndef BOOST_NO_EXCEPTIONS
100                }
101            catch(...)
102                {
103                return 0;
104                }
105#endif
106            }
107
108        inline
109        std::string
110        diagnostic_information_impl( boost::exception const * be, std::exception const * se, bool with_what )
111            {
112            if( !be && !se )
113                return "Unknown exception.";
114#ifndef BOOST_NO_RTTI
115            if( !be )
116                be=dynamic_cast<boost::exception const *>(se);
117            if( !se )
118                se=dynamic_cast<std::exception const *>(be);
119#endif
120            char const * wh=0;
121            if( with_what && se )
122                {
123                wh=se->what();
124                if( be && exception_detail::get_diagnostic_information(*be,0)==wh )
125                    return wh;
126                }
127            std::ostringstream tmp;
128            if( be )
129                {
130                char const * const * f=get_error_info<throw_file>(*be);
131                int const * l=get_error_info<throw_line>(*be);
132                char const * const * fn=get_error_info<throw_function>(*be);
133                if( !f && !l && !fn )
134                    tmp << "Throw location unknown (consider using BOOST_THROW_EXCEPTION)\n";
135                else
136                    {
137                    if( f )
138                        {
139                        tmp << *f;
140                        if( int const * l=get_error_info<throw_line>(*be) )
141                            tmp << '(' << *l << "): ";
142                        }
143                    tmp << "Throw in function ";
144                    if( char const * const * fn=get_error_info<throw_function>(*be) )
145                        tmp << *fn;
146                    else
147                        tmp << "(unknown)";
148                    tmp << '\n';
149                    }
150                }
151#ifndef BOOST_NO_RTTI
152            tmp << std::string("Dynamic exception type: ") <<
153                units::detail::demangle((be?(BOOST_EXCEPTION_DYNAMIC_TYPEID(*be)):(BOOST_EXCEPTION_DYNAMIC_TYPEID(*se))).type_->name()) << '\n';
154#endif
155            if( with_what && se )
156                tmp << "std::exception::what: " << wh << '\n';
157            if( be )
158                if( char const * s=exception_detail::get_diagnostic_information(*be,tmp.str().c_str()) )
159                    if( *s )
160                        return s;
161            return tmp.str();
162            }
163        }
164
165    template <class T>
166    std::string
167    diagnostic_information( T const & e )
168        {
169        return exception_detail::diagnostic_information_impl(exception_detail::get_boost_exception(&e),exception_detail::get_std_exception(&e),true);
170        }
171
172    inline
173    char const *
174    diagnostic_information_what( exception const & e ) throw()
175        {
176        char const * w=0;
177#ifndef BOOST_NO_EXCEPTIONS
178        try
179            {
180#endif
181            (void) exception_detail::diagnostic_information_impl(&e,0,false);
182            if( char const * di=exception_detail::get_diagnostic_information(e,0) )
183                return di;
184            else
185                return "Failed to produce boost::diagnostic_information_what()";
186#ifndef BOOST_NO_EXCEPTIONS
187            }
188        catch(
189        ... )
190            {
191            }
192#endif
193        return w;
194        }
195    }
196
197#if defined(_MSC_VER) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
198#pragma warning(pop)
199#endif
200#endif