throw_exception.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2012-2013. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_THROW_EXCEPTION_HPP
  11. #define BOOST_CONTAINER_THROW_EXCEPTION_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #if defined(BOOST_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. #include <boost/container/detail/config_begin.hpp>
  19. #include <boost/container/detail/workaround.hpp>
  20. #ifndef BOOST_NO_EXCEPTIONS
  21. #include <stdexcept> //for std exception types
  22. #include <string> //for implicit std::string conversion
  23. #include <new> //for std::bad_alloc
  24. #else
  25. #include <boost/assert.hpp>
  26. #include <cstdlib> //for std::abort
  27. #endif
  28. namespace boost {
  29. namespace container {
  30. #if defined(BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS)
  31. //The user must provide definitions for the following functions
  32. void throw_bad_alloc();
  33. void throw_out_of_range(const char* str);
  34. void throw_length_error(const char* str);
  35. void throw_logic_error(const char* str);
  36. void throw_runtime_error(const char* str);
  37. #elif defined(BOOST_NO_EXCEPTIONS)
  38. BOOST_NORETURN inline void throw_bad_alloc()
  39. {
  40. const char msg[] = "boost::container bad_alloc thrown";
  41. (void)msg;
  42. BOOST_ASSERT(!msg);
  43. std::abort();
  44. }
  45. BOOST_NORETURN inline void throw_out_of_range(const char* str)
  46. {
  47. const char msg[] = "boost::container out_of_range thrown";
  48. (void)msg; (void)str;
  49. BOOST_ASSERT_MSG(!msg, str);
  50. std::abort();
  51. }
  52. BOOST_NORETURN inline void throw_length_error(const char* str)
  53. {
  54. const char msg[] = "boost::container length_error thrown";
  55. (void)msg; (void)str;
  56. BOOST_ASSERT_MSG(!msg, str);
  57. std::abort();
  58. }
  59. BOOST_NORETURN inline void throw_logic_error(const char* str)
  60. {
  61. const char msg[] = "boost::container logic_error thrown";
  62. (void)msg; (void)str;
  63. BOOST_ASSERT_MSG(!msg, str);
  64. std::abort();
  65. }
  66. BOOST_NORETURN inline void throw_runtime_error(const char* str)
  67. {
  68. const char msg[] = "boost::container runtime_error thrown";
  69. (void)msg; (void)str;
  70. BOOST_ASSERT_MSG(!msg, str);
  71. std::abort();
  72. }
  73. #else //defined(BOOST_NO_EXCEPTIONS)
  74. //! Exception callback called by Boost.Container when fails to allocate the requested storage space.
  75. //! <ul>
  76. //! <li>If BOOST_NO_EXCEPTIONS is NOT defined <code>std::bad_alloc()</code> is thrown.</li>
  77. //!
  78. //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
  79. //! is NOT defined <code>BOOST_ASSERT(!"boost::container bad_alloc thrown")</code> is called
  80. //! and <code>std::abort()</code> if the former returns.</li>
  81. //!
  82. //! <li>If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined
  83. //! the user must provide an implementation and the function should not return.</li>
  84. //! </ul>
  85. BOOST_NORETURN inline void throw_bad_alloc()
  86. {
  87. throw std::bad_alloc();
  88. }
  89. //! Exception callback called by Boost.Container to signal arguments out of range.
  90. //! <ul>
  91. //! <li>If BOOST_NO_EXCEPTIONS is NOT defined <code>std::out_of_range(str)</code> is thrown.</li>
  92. //!
  93. //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
  94. //! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container out_of_range thrown", str)</code> is called
  95. //! and <code>std::abort()</code> if the former returns.</li>
  96. //!
  97. //! <li>If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined
  98. //! the user must provide an implementation and the function should not return.</li>
  99. //! </ul>
  100. BOOST_NORETURN inline void throw_out_of_range(const char* str)
  101. {
  102. throw std::out_of_range(str);
  103. }
  104. //! Exception callback called by Boost.Container to signal errors resizing.
  105. //! <ul>
  106. //! <li>If BOOST_NO_EXCEPTIONS is NOT defined <code>std::length_error(str)</code> is thrown.</li>
  107. //!
  108. //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
  109. //! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container length_error thrown", str)</code> is called
  110. //! and <code>std::abort()</code> if the former returns.</li>
  111. //!
  112. //! <li>If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined
  113. //! the user must provide an implementation and the function should not return.</li>
  114. //! </ul>
  115. BOOST_NORETURN inline void throw_length_error(const char* str)
  116. {
  117. throw std::length_error(str);
  118. }
  119. //! Exception callback called by Boost.Container to report errors in the internal logical
  120. //! of the program, such as violation of logical preconditions or class invariants.
  121. //! <ul>
  122. //! <li>If BOOST_NO_EXCEPTIONS is NOT defined <code>std::logic_error(str)</code> is thrown.</li>
  123. //!
  124. //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
  125. //! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container logic_error thrown", str)</code> is called
  126. //! and <code>std::abort()</code> if the former returns.</li>
  127. //!
  128. //! <li>If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined
  129. //! the user must provide an implementation and the function should not return.</li>
  130. //! </ul>
  131. BOOST_NORETURN inline void throw_logic_error(const char* str)
  132. {
  133. throw std::logic_error(str);
  134. }
  135. //! Exception callback called by Boost.Container to report errors that can only be detected during runtime.
  136. //! <ul>
  137. //! <li>If BOOST_NO_EXCEPTIONS is NOT defined <code>std::runtime_error(str)</code> is thrown.</li>
  138. //!
  139. //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
  140. //! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container runtime_error thrown", str)</code> is called
  141. //! and <code>std::abort()</code> if the former returns.</li>
  142. //!
  143. //! <li>If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined
  144. //! the user must provide an implementation and the function should not return.</li>
  145. //! </ul>
  146. BOOST_NORETURN inline void throw_runtime_error(const char* str)
  147. {
  148. throw std::runtime_error(str);
  149. }
  150. #endif
  151. }} //namespace boost { namespace container {
  152. #include <boost/container/detail/config_end.hpp>
  153. #endif //#ifndef BOOST_CONTAINER_THROW_EXCEPTION_HPP