get_error_info.hpp 3.6 KB

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