get_error_info.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //Copyright (c) 2006-2009 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_1A590226753311DD9E4CCF6156D89593
  5. #define UUID_1A590226753311DD9E4CCF6156D89593
  6. #include <boost/exception/exception.hpp>
  7. #include <boost/exception/detail/error_info_impl.hpp>
  8. #include <boost/exception/detail/type_info.hpp>
  9. #include <boost/shared_ptr.hpp>
  10. namespace
  11. boost
  12. {
  13. namespace
  14. exception_detail
  15. {
  16. template <class ErrorInfo>
  17. struct
  18. get_info
  19. {
  20. static
  21. typename ErrorInfo::value_type const *
  22. get( exception const & x )
  23. {
  24. if( exception_detail::error_info_container * c=x.data_.get() )
  25. if( shared_ptr<exception_detail::error_info_base const> eib = c->get(BOOST_EXCEPTION_STATIC_TYPEID(ErrorInfo)) )
  26. {
  27. #ifndef BOOST_NO_RTTI
  28. BOOST_ASSERT( 0!=dynamic_cast<ErrorInfo const *>(eib.get()) );
  29. #endif
  30. ErrorInfo const * w = static_cast<ErrorInfo const *>(eib.get());
  31. return &w->value();
  32. }
  33. return 0;
  34. }
  35. };
  36. template <>
  37. struct
  38. get_info<throw_function>
  39. {
  40. static
  41. char const * const *
  42. get( exception const & x )
  43. {
  44. return x.throw_function_ ? &x.throw_function_ : 0;
  45. }
  46. };
  47. template <>
  48. struct
  49. get_info<throw_file>
  50. {
  51. static
  52. char const * const *
  53. get( exception const & x )
  54. {
  55. return x.throw_file_ ? &x.throw_file_ : 0;
  56. }
  57. };
  58. template <>
  59. struct
  60. get_info<throw_line>
  61. {
  62. static
  63. int const *
  64. get( exception const & x )
  65. {
  66. return x.throw_line_!=-1 ? &x.throw_line_ : 0;
  67. }
  68. };
  69. }
  70. #ifdef BOOST_NO_RTTI
  71. template <class ErrorInfo>
  72. inline
  73. typename ErrorInfo::value_type const *
  74. get_error_info( boost::exception const & x )
  75. {
  76. return exception_detail::get_info<ErrorInfo>::get(x);
  77. }
  78. #else
  79. template <class ErrorInfo,class E>
  80. inline
  81. typename ErrorInfo::value_type const *
  82. get_error_info( E const & some_exception )
  83. {
  84. if( exception const * x = dynamic_cast<exception const *>(&some_exception) )
  85. return exception_detail::get_info<ErrorInfo>::get(*x);
  86. else
  87. return 0;
  88. }
  89. #endif
  90. }
  91. #endif