assert.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // boost/assert.hpp - BOOST_ASSERT(expr)
  3. // BOOST_ASSERT_MSG(expr, msg)
  4. // BOOST_VERIFY(expr)
  5. //
  6. // Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
  7. // Copyright (c) 2007 Peter Dimov
  8. // Copyright (c) Beman Dawes 2011
  9. //
  10. // Distributed under the Boost Software License, Version 1.0. (See
  11. // accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. //
  14. // Note: There are no include guards. This is intentional.
  15. //
  16. // See http://www.boost.org/libs/utility/assert.html for documentation.
  17. //
  18. //
  19. // Stop inspect complaining about use of 'assert':
  20. //
  21. // boostinspect:naassert_macro
  22. //
  23. //--------------------------------------------------------------------------------------//
  24. // BOOST_ASSERT //
  25. //--------------------------------------------------------------------------------------//
  26. #undef BOOST_ASSERT
  27. #if defined(BOOST_DISABLE_ASSERTS)
  28. # define BOOST_ASSERT(expr) ((void)0)
  29. #elif defined(BOOST_ENABLE_ASSERT_HANDLER)
  30. #include <boost/config.hpp>
  31. #include <boost/current_function.hpp>
  32. namespace boost
  33. {
  34. void assertion_failed(char const * expr,
  35. char const * function, char const * file, long line); // user defined
  36. } // namespace boost
  37. #define BOOST_ASSERT(expr) (BOOST_LIKELY(!!(expr)) \
  38. ? ((void)0) \
  39. : ::boost::assertion_failed(#expr, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__))
  40. #else
  41. # include <assert.h> // .h to support old libraries w/o <cassert> - effect is the same
  42. # define BOOST_ASSERT(expr) assert(expr)
  43. #endif
  44. //--------------------------------------------------------------------------------------//
  45. // BOOST_ASSERT_MSG //
  46. //--------------------------------------------------------------------------------------//
  47. # undef BOOST_ASSERT_MSG
  48. #if defined(BOOST_DISABLE_ASSERTS) || defined(NDEBUG)
  49. #define BOOST_ASSERT_MSG(expr, msg) ((void)0)
  50. #elif defined(BOOST_ENABLE_ASSERT_HANDLER)
  51. #include <boost/config.hpp>
  52. #include <boost/current_function.hpp>
  53. namespace boost
  54. {
  55. void assertion_failed_msg(char const * expr, char const * msg,
  56. char const * function, char const * file, long line); // user defined
  57. } // namespace boost
  58. #define BOOST_ASSERT_MSG(expr, msg) (BOOST_LIKELY(!!(expr)) \
  59. ? ((void)0) \
  60. : ::boost::assertion_failed_msg(#expr, msg, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__))
  61. #else
  62. #ifndef BOOST_ASSERT_HPP
  63. #define BOOST_ASSERT_HPP
  64. #include <cstdlib>
  65. #include <iostream>
  66. #include <boost/config.hpp>
  67. #include <boost/current_function.hpp>
  68. // IDE's like Visual Studio perform better if output goes to std::cout or
  69. // some other stream, so allow user to configure output stream:
  70. #ifndef BOOST_ASSERT_MSG_OSTREAM
  71. # define BOOST_ASSERT_MSG_OSTREAM std::cerr
  72. #endif
  73. namespace boost
  74. {
  75. namespace assertion
  76. {
  77. namespace detail
  78. {
  79. // Note: The template is needed to make the function non-inline and avoid linking errors
  80. template< typename CharT >
  81. BOOST_NOINLINE void assertion_failed_msg(CharT const * expr, char const * msg, char const * function,
  82. char const * file, long line)
  83. {
  84. BOOST_ASSERT_MSG_OSTREAM
  85. << "***** Internal Program Error - assertion (" << expr << ") failed in "
  86. << function << ":\n"
  87. << file << '(' << line << "): " << msg << std::endl;
  88. #ifdef UNDER_CE
  89. // The Windows CE CRT library does not have abort() so use exit(-1) instead.
  90. std::exit(-1);
  91. #else
  92. std::abort();
  93. #endif
  94. }
  95. } // detail
  96. } // assertion
  97. } // detail
  98. #endif
  99. #define BOOST_ASSERT_MSG(expr, msg) (BOOST_LIKELY(!!(expr)) \
  100. ? ((void)0) \
  101. : ::boost::assertion::detail::assertion_failed_msg(#expr, msg, \
  102. BOOST_CURRENT_FUNCTION, __FILE__, __LINE__))
  103. #endif
  104. //--------------------------------------------------------------------------------------//
  105. // BOOST_VERIFY //
  106. //--------------------------------------------------------------------------------------//
  107. #undef BOOST_VERIFY
  108. #if defined(BOOST_DISABLE_ASSERTS) || ( !defined(BOOST_ENABLE_ASSERT_HANDLER) && defined(NDEBUG) )
  109. # define BOOST_VERIFY(expr) ((void)(expr))
  110. #else
  111. # define BOOST_VERIFY(expr) BOOST_ASSERT(expr)
  112. #endif