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