throw_exception.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #include <boost/core/ignore_unused.hpp>
  21. #ifndef BOOST_NO_EXCEPTIONS
  22. #include <exception> //for std exception base
  23. # if defined(BOOST_CONTAINER_USE_STD_EXCEPTIONS)
  24. #include <stdexcept> //for std exception types
  25. #include <string> //for implicit std::string conversion
  26. #include <new> //for std::bad_alloc
  27. typedef std::bad_alloc bad_alloc_t;
  28. typedef std::out_of_range out_of_range_t;
  29. typedef std::out_of_range length_error_t;
  30. typedef std::logic_error logic_error_t;
  31. typedef std::runtime_error runtime_error_t;
  32. # else //!BOOST_CONTAINER_USE_STD_EXCEPTIONS
  33. namespace boost {
  34. namespace container {
  35. class exception
  36. : public ::std::exception
  37. {
  38. typedef ::std::exception std_exception_t;
  39. public:
  40. //msg must be a static string (guaranteed by callers)
  41. explicit exception(const char *msg)
  42. : std_exception_t(), m_msg(msg)
  43. {}
  44. virtual const char *what() const BOOST_NOEXCEPT_OR_NOTHROW
  45. { return m_msg ? m_msg : "unknown boost::container exception"; }
  46. private:
  47. const char *m_msg;
  48. };
  49. class bad_alloc
  50. : public exception
  51. {
  52. public:
  53. bad_alloc()
  54. : exception("boost::container::bad_alloc thrown")
  55. {}
  56. };
  57. typedef bad_alloc bad_alloc_t;
  58. class out_of_range
  59. : public exception
  60. {
  61. public:
  62. explicit out_of_range(const char *msg)
  63. : exception(msg)
  64. {}
  65. };
  66. typedef out_of_range out_of_range_t;
  67. class length_error
  68. : public exception
  69. {
  70. public:
  71. explicit length_error(const char *msg)
  72. : exception(msg)
  73. {}
  74. };
  75. typedef out_of_range length_error_t;
  76. class logic_error
  77. : public exception
  78. {
  79. public:
  80. explicit logic_error(const char *msg)
  81. : exception(msg)
  82. {}
  83. };
  84. typedef logic_error logic_error_t;
  85. class runtime_error
  86. : public exception
  87. {
  88. public:
  89. explicit runtime_error(const char *msg)
  90. : exception(msg)
  91. {}
  92. };
  93. typedef runtime_error runtime_error_t;
  94. } // namespace boost {
  95. } // namespace container {
  96. # endif
  97. #else
  98. #include <boost/assert.hpp>
  99. #include <cstdlib> //for std::abort
  100. #endif
  101. namespace boost {
  102. namespace container {
  103. #if defined(BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS)
  104. //The user must provide definitions for the following functions
  105. BOOST_NORETURN void throw_bad_alloc();
  106. BOOST_NORETURN void throw_out_of_range(const char* str);
  107. BOOST_NORETURN void throw_length_error(const char* str);
  108. BOOST_NORETURN void throw_logic_error(const char* str);
  109. BOOST_NORETURN void throw_runtime_error(const char* str);
  110. #elif defined(BOOST_NO_EXCEPTIONS)
  111. BOOST_NORETURN inline void throw_bad_alloc()
  112. {
  113. BOOST_ASSERT(!"boost::container bad_alloc thrown");
  114. std::abort();
  115. }
  116. BOOST_NORETURN inline void throw_out_of_range(const char* str)
  117. {
  118. boost::ignore_unused(str);
  119. BOOST_ASSERT_MSG(!"boost::container out_of_range thrown", str);
  120. std::abort();
  121. }
  122. BOOST_NORETURN inline void throw_length_error(const char* str)
  123. {
  124. boost::ignore_unused(str);
  125. BOOST_ASSERT_MSG(!"boost::container length_error thrown", str);
  126. std::abort();
  127. }
  128. BOOST_NORETURN inline void throw_logic_error(const char* str)
  129. {
  130. boost::ignore_unused(str);
  131. BOOST_ASSERT_MSG(!"boost::container logic_error thrown", str);
  132. std::abort();
  133. }
  134. BOOST_NORETURN inline void throw_runtime_error(const char* str)
  135. {
  136. boost::ignore_unused(str);
  137. BOOST_ASSERT_MSG(!"boost::container runtime_error thrown", str);
  138. std::abort();
  139. }
  140. #else //defined(BOOST_NO_EXCEPTIONS)
  141. //! Exception callback called by Boost.Container when fails to allocate the requested storage space.
  142. //! <ul>
  143. //! <li>If BOOST_NO_EXCEPTIONS is NOT defined <code>std::bad_alloc()</code> is thrown.</li>
  144. //!
  145. //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
  146. //! is NOT defined <code>BOOST_ASSERT(!"boost::container bad_alloc thrown")</code> is called
  147. //! and <code>std::abort()</code> if the former returns.</li>
  148. //!
  149. //! <li>If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined
  150. //! the user must provide an implementation and the function should not return.</li>
  151. //! </ul>
  152. BOOST_NORETURN inline void throw_bad_alloc()
  153. {
  154. #ifdef BOOST_CONTAINER_USE_STD_EXCEPTIONS
  155. throw std::bad_alloc();
  156. #else
  157. throw bad_alloc();
  158. #endif
  159. }
  160. //! Exception callback called by Boost.Container to signal arguments out of range.
  161. //! <ul>
  162. //! <li>If BOOST_NO_EXCEPTIONS is NOT defined <code>std::out_of_range(str)</code> is thrown.</li>
  163. //!
  164. //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
  165. //! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container out_of_range thrown", str)</code> is called
  166. //! and <code>std::abort()</code> if the former returns.</li>
  167. //!
  168. //! <li>If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined
  169. //! the user must provide an implementation and the function should not return.</li>
  170. //! </ul>
  171. BOOST_NORETURN inline void throw_out_of_range(const char* str)
  172. {
  173. #ifdef BOOST_CONTAINER_USE_STD_EXCEPTIONS
  174. throw std::out_of_range(str);
  175. #else
  176. throw out_of_range(str);
  177. #endif
  178. }
  179. //! Exception callback called by Boost.Container to signal errors resizing.
  180. //! <ul>
  181. //! <li>If BOOST_NO_EXCEPTIONS is NOT defined <code>std::length_error(str)</code> is thrown.</li>
  182. //!
  183. //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
  184. //! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container length_error thrown", str)</code> is called
  185. //! and <code>std::abort()</code> if the former returns.</li>
  186. //!
  187. //! <li>If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined
  188. //! the user must provide an implementation and the function should not return.</li>
  189. //! </ul>
  190. BOOST_NORETURN inline void throw_length_error(const char* str)
  191. {
  192. #ifdef BOOST_CONTAINER_USE_STD_EXCEPTIONS
  193. throw std::length_error(str);
  194. #else
  195. throw length_error(str);
  196. #endif
  197. }
  198. //! Exception callback called by Boost.Container to report errors in the internal logical
  199. //! of the program, such as violation of logical preconditions or class invariants.
  200. //! <ul>
  201. //! <li>If BOOST_NO_EXCEPTIONS is NOT defined <code>std::logic_error(str)</code> is thrown.</li>
  202. //!
  203. //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
  204. //! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container logic_error thrown", str)</code> is called
  205. //! and <code>std::abort()</code> if the former returns.</li>
  206. //!
  207. //! <li>If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined
  208. //! the user must provide an implementation and the function should not return.</li>
  209. //! </ul>
  210. BOOST_NORETURN inline void throw_logic_error(const char* str)
  211. {
  212. #ifdef BOOST_CONTAINER_USE_STD_EXCEPTIONS
  213. throw std::logic_error(str);
  214. #else
  215. throw logic_error(str);
  216. #endif
  217. }
  218. //! Exception callback called by Boost.Container to report errors that can only be detected during runtime.
  219. //! <ul>
  220. //! <li>If BOOST_NO_EXCEPTIONS is NOT defined <code>std::runtime_error(str)</code> is thrown.</li>
  221. //!
  222. //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
  223. //! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container runtime_error thrown", str)</code> is called
  224. //! and <code>std::abort()</code> if the former returns.</li>
  225. //!
  226. //! <li>If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined
  227. //! the user must provide an implementation and the function should not return.</li>
  228. //! </ul>
  229. BOOST_NORETURN inline void throw_runtime_error(const char* str)
  230. {
  231. #ifdef BOOST_CONTAINER_USE_STD_EXCEPTIONS
  232. throw std::runtime_error(str);
  233. #else
  234. throw runtime_error(str);
  235. #endif
  236. }
  237. #endif
  238. }} //namespace boost { namespace container {
  239. #include <boost/container/detail/config_end.hpp>
  240. #endif //#ifndef BOOST_CONTAINER_THROW_EXCEPTION_HPP