get_error_info.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 BOOST_EXCEPTION_1A590226753311DD9E4CCF6156D89593
  5. #define BOOST_EXCEPTION_1A590226753311DD9E4CCF6156D89593
  6. #include <boost/config.hpp>
  7. #include <boost/exception/exception.hpp>
  8. #include <boost/exception/detail/error_info_impl.hpp>
  9. #include <boost/exception/detail/type_info.hpp>
  10. #include <boost/exception/detail/shared_ptr.hpp>
  11. #include <boost/assert.hpp>
  12. #ifndef BOOST_EXCEPTION_ENABLE_WARNINGS
  13. #if __GNUC__*100+__GNUC_MINOR__>301
  14. #pragma GCC system_header
  15. #endif
  16. #ifdef __clang__
  17. #pragma clang system_header
  18. #endif
  19. #ifdef _MSC_VER
  20. #pragma warning(push,1)
  21. #endif
  22. #endif
  23. namespace
  24. boost
  25. {
  26. namespace
  27. exception_detail
  28. {
  29. template <class ErrorInfo>
  30. struct
  31. get_info
  32. {
  33. static
  34. typename ErrorInfo::value_type *
  35. get( exception const & x )
  36. {
  37. if( exception_detail::error_info_container * c=x.data_.get() )
  38. if( shared_ptr<exception_detail::error_info_base> eib = c->get(BOOST_EXCEPTION_STATIC_TYPEID(ErrorInfo)) )
  39. {
  40. #ifndef BOOST_NO_RTTI
  41. BOOST_ASSERT( 0!=dynamic_cast<ErrorInfo *>(eib.get()) );
  42. #endif
  43. ErrorInfo * w = static_cast<ErrorInfo *>(eib.get());
  44. return &w->value();
  45. }
  46. return 0;
  47. }
  48. };
  49. template <>
  50. struct
  51. get_info<throw_function>
  52. {
  53. static
  54. char const * *
  55. get( exception const & x )
  56. {
  57. return x.throw_function_ ? &x.throw_function_ : 0;
  58. }
  59. };
  60. template <>
  61. struct
  62. get_info<throw_file>
  63. {
  64. static
  65. char const * *
  66. get( exception const & x )
  67. {
  68. return x.throw_file_ ? &x.throw_file_ : 0;
  69. }
  70. };
  71. template <>
  72. struct
  73. get_info<throw_line>
  74. {
  75. static
  76. int *
  77. get( exception const & x )
  78. {
  79. return x.throw_line_!=-1 ? &x.throw_line_ : 0;
  80. }
  81. };
  82. template <class T,class R>
  83. struct
  84. get_error_info_return_type
  85. {
  86. typedef R * type;
  87. };
  88. template <class T,class R>
  89. struct
  90. get_error_info_return_type<T const,R>
  91. {
  92. typedef R const * type;
  93. };
  94. }
  95. #ifdef BOOST_NO_RTTI
  96. template <class ErrorInfo>
  97. inline
  98. typename ErrorInfo::value_type const *
  99. get_error_info( boost::exception const & x )
  100. {
  101. return exception_detail::get_info<ErrorInfo>::get(x);
  102. }
  103. template <class ErrorInfo>
  104. inline
  105. typename ErrorInfo::value_type *
  106. get_error_info( boost::exception & x )
  107. {
  108. return exception_detail::get_info<ErrorInfo>::get(x);
  109. }
  110. #else
  111. template <class ErrorInfo,class E>
  112. inline
  113. typename exception_detail::get_error_info_return_type<E,typename ErrorInfo::value_type>::type
  114. get_error_info( E & some_exception )
  115. {
  116. if( exception const * x = dynamic_cast<exception const *>(&some_exception) )
  117. return exception_detail::get_info<ErrorInfo>::get(*x);
  118. else
  119. return 0;
  120. }
  121. #endif
  122. }
  123. #if defined(_MSC_VER) && !defined(BOOST_EXCEPTION_ENABLE_WARNINGS)
  124. #pragma warning(pop)
  125. #endif
  126. #endif