1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_CONTAINER_THROW_EXCEPTION_HPP
12 #define BOOST_CONTAINER_THROW_EXCEPTION_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/container/detail/config_begin.hpp>
23 #include <boost/container/detail/workaround.hpp>
24 #include <boost/core/ignore_unused.hpp>
26 #ifndef BOOST_NO_EXCEPTIONS
27 #include <exception> //for std exception base
29 # if defined(BOOST_CONTAINER_USE_STD_EXCEPTIONS)
30 #include <stdexcept> //for std::out_of_range, std::length_error, std::logic_error, std::runtime_error
31 #include <string> //for implicit std::string conversion
32 #include <new> //for std::bad_alloc
37 typedef std::bad_alloc bad_alloc_t;
38 typedef std::out_of_range out_of_range_t;
39 typedef std::length_error length_error_t;
40 typedef std::logic_error logic_error_t;
41 typedef std::runtime_error runtime_error_t;
43 }} //namespace boost::container
45 # else //!BOOST_CONTAINER_USE_STD_EXCEPTIONS
50 class BOOST_SYMBOL_VISIBLE exception
51 : public ::std::exception
53 typedef ::std::exception std_exception_t;
57 //msg must be a static string (guaranteed by callers)
58 explicit exception(const char *msg)
59 : std_exception_t(), m_msg(msg)
62 virtual const char *what() const BOOST_NOEXCEPT_OR_NOTHROW BOOST_OVERRIDE
63 { return m_msg ? m_msg : "unknown boost::container exception"; }
69 class BOOST_SYMBOL_VISIBLE bad_alloc
74 : exception("boost::container::bad_alloc thrown")
78 typedef bad_alloc bad_alloc_t;
80 class BOOST_SYMBOL_VISIBLE out_of_range
84 explicit out_of_range(const char *msg)
89 typedef out_of_range out_of_range_t;
91 class BOOST_SYMBOL_VISIBLE length_error
95 explicit length_error(const char *msg)
100 typedef out_of_range length_error_t;
102 class BOOST_SYMBOL_VISIBLE logic_error
106 explicit logic_error(const char *msg)
111 typedef logic_error logic_error_t;
113 class BOOST_SYMBOL_VISIBLE runtime_error
117 explicit runtime_error(const char *msg)
122 typedef runtime_error runtime_error_t;
124 } // namespace boost {
125 } // namespace container {
129 #include <boost/assert.hpp>
130 #include <cstdlib> //for std::abort
134 namespace container {
136 #if defined(BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS)
137 //The user must provide definitions for the following functions
139 BOOST_NORETURN void throw_bad_alloc();
141 BOOST_NORETURN void throw_out_of_range(const char* str);
143 BOOST_NORETURN void throw_length_error(const char* str);
145 BOOST_NORETURN void throw_logic_error(const char* str);
147 BOOST_NORETURN void throw_runtime_error(const char* str);
149 #elif defined(BOOST_NO_EXCEPTIONS)
151 BOOST_NORETURN inline void throw_bad_alloc()
153 BOOST_ASSERT(!"boost::container bad_alloc thrown");
157 BOOST_NORETURN inline void throw_out_of_range(const char* str)
159 boost::ignore_unused(str);
160 BOOST_ASSERT_MSG(!"boost::container out_of_range thrown", str);
164 BOOST_NORETURN inline void throw_length_error(const char* str)
166 boost::ignore_unused(str);
167 BOOST_ASSERT_MSG(!"boost::container length_error thrown", str);
171 BOOST_NORETURN inline void throw_logic_error(const char* str)
173 boost::ignore_unused(str);
174 BOOST_ASSERT_MSG(!"boost::container logic_error thrown", str);
178 BOOST_NORETURN inline void throw_runtime_error(const char* str)
180 boost::ignore_unused(str);
181 BOOST_ASSERT_MSG(!"boost::container runtime_error thrown", str);
185 #else //defined(BOOST_NO_EXCEPTIONS)
187 //! Exception callback called by Boost.Container when fails to allocate the requested storage space.
189 //! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is NOT defined
190 //! <code>boost::container::bad_alloc(str)</code> is thrown.</li>
192 //! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is defined
193 //! <code>std::bad_alloc(str)</code> is thrown.</li>
195 //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
196 //! is NOT defined <code>BOOST_ASSERT(!"boost::container bad_alloc thrown")</code> is called
197 //! and <code>std::abort()</code> if the former returns.</li>
199 //! <li>If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined
200 //! the user must provide an implementation and the function should not return.</li>
202 BOOST_NORETURN inline void throw_bad_alloc()
207 //! Exception callback called by Boost.Container to signal arguments out of range.
209 //! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is NOT defined
210 //! <code>boost::container::out_of_range(str)</code> is thrown.</li>
212 //! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is defined
213 //! <code>std::out_of_range(str)</code> is thrown.</li>
215 //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
216 //! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container out_of_range thrown", str)</code> is called
217 //! and <code>std::abort()</code> if the former returns.</li>
219 //! <li>If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined
220 //! the user must provide an implementation and the function should not return.</li>
222 BOOST_NORETURN inline void throw_out_of_range(const char* str)
224 throw out_of_range_t(str);
227 //! Exception callback called by Boost.Container to signal errors resizing.
230 //! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is NOT defined
231 //! <code>boost::container::length_error(str)</code> is thrown.</li>
233 //! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is defined
234 //! <code>std::length_error(str)</code> is thrown.</li>
236 //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
237 //! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container length_error thrown", str)</code> is called
238 //! and <code>std::abort()</code> if the former returns.</li>
240 //! <li>If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined
241 //! the user must provide an implementation and the function should not return.</li>
243 BOOST_NORETURN inline void throw_length_error(const char* str)
245 throw length_error_t(str);
248 //! Exception callback called by Boost.Container to report errors in the internal logical
249 //! of the program, such as violation of logical preconditions or class invariants.
252 //! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is NOT defined
253 //! <code>boost::container::logic_error(str)</code> is thrown.</li>
255 //! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is defined
256 //! <code>std::logic_error(str)</code> is thrown.</li>
258 //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
259 //! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container logic_error thrown", str)</code> is called
260 //! and <code>std::abort()</code> if the former returns.</li>
262 //! <li>If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined
263 //! the user must provide an implementation and the function should not return.</li>
265 BOOST_NORETURN inline void throw_logic_error(const char* str)
267 throw logic_error_t(str);
270 //! Exception callback called by Boost.Container to report errors that can only be detected during runtime.
272 //! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is NOT defined
273 //! <code>boost::container::runtime_error(str)</code> is thrown.</li>
275 //! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is defined
276 //! <code>std::runtime_error(str)</code> is thrown.</li>
278 //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
279 //! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container runtime_error thrown", str)</code> is called
280 //! and <code>std::abort()</code> if the former returns.</li>
282 //! <li>If BOOST_NO_EXCEPTIONS and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS are defined
283 //! the user must provide an implementation and the function should not return.</li>
285 BOOST_NORETURN inline void throw_runtime_error(const char* str)
287 throw runtime_error_t(str);
292 }} //namespace boost { namespace container {
294 #include <boost/container/detail/config_end.hpp>
296 #endif //#ifndef BOOST_CONTAINER_THROW_EXCEPTION_HPP