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