Fixes #184 ("Issues with custom exceptions implementation")

This commit is contained in:
Ion Gaztañaga
2021-04-19 21:52:55 +02:00
parent 2073b125f4
commit 16cada57ea
10 changed files with 67 additions and 53 deletions

View File

@ -1338,6 +1338,13 @@ use [*Boost.Container]? There are several reasons for that:
[section:release_notes Release Notes] [section:release_notes Release Notes]
[section:release_notes_boost_1_77_00 Boost 1.77 Release]
* Fixed bugs/issues:
* [@https://github.com/boostorg/container/issues/184 GitHub #184: ['"Issues with custom exceptions implementation"]].
[endsect]
[section:release_notes_boost_1_76_00 Boost 1.76 Release] [section:release_notes_boost_1_76_00 Boost 1.76 Release]
* Added [[no-discard]] attribute in all containers to catch bugs related to unused return values. * Added [[no-discard]] attribute in all containers to catch bugs related to unused return values.

View File

@ -153,7 +153,7 @@ class adaptive_pool
{ return size_type(-1)/(2u*sizeof(T)); } { return size_type(-1)/(2u*sizeof(T)); }
//!Allocate memory for an array of count elements. //!Allocate memory for an array of count elements.
//!Throws std::bad_alloc if there is no enough memory //!Throws bad_alloc if there is no enough memory
pointer allocate(size_type count, const void * = 0) pointer allocate(size_type count, const void * = 0)
{ {
if(BOOST_UNLIKELY(count > size_type(-1)/(2u*sizeof(T)))) if(BOOST_UNLIKELY(count > size_type(-1)/(2u*sizeof(T))))
@ -467,7 +467,7 @@ class private_adaptive_pool
{ return size_type(-1)/(2u*sizeof(T)); } { return size_type(-1)/(2u*sizeof(T)); }
//!Allocate memory for an array of count elements. //!Allocate memory for an array of count elements.
//!Throws std::bad_alloc if there is no enough memory //!Throws bad_alloc if there is no enough memory
pointer allocate(size_type count, const void * = 0) pointer allocate(size_type count, const void * = 0)
{ {
if(BOOST_UNLIKELY(count > size_type(-1)/(2u*sizeof(T)))) if(BOOST_UNLIKELY(count > size_type(-1)/(2u*sizeof(T))))

View File

@ -174,7 +174,7 @@ class allocator
{} {}
//!Allocates memory for an array of count elements. //!Allocates memory for an array of count elements.
//!Throws std::bad_alloc if there is no enough memory //!Throws bad_alloc if there is no enough memory
//!If Version is 2, this allocated memory can only be deallocated //!If Version is 2, this allocated memory can only be deallocated
//!with deallocate() or (for Version == 2) deallocate_many() //!with deallocate() or (for Version == 2) deallocate_many()
BOOST_CONTAINER_ATTRIBUTE_NODISCARD pointer allocate(size_type count, const void * hint= 0) BOOST_CONTAINER_ATTRIBUTE_NODISCARD pointer allocate(size_type count, const void * hint= 0)

View File

@ -111,7 +111,7 @@ class shared_adaptive_node_pool
~shared_adaptive_node_pool() ~shared_adaptive_node_pool()
{} {}
//!Allocates array of count elements. Can throw std::bad_alloc //!Allocates array of count elements. Can throw bad_alloc
void *allocate_node() void *allocate_node()
{ {
//----------------------- //-----------------------
@ -130,7 +130,7 @@ class shared_adaptive_node_pool
} }
//!Allocates a singly linked list of n nodes ending in null pointer. //!Allocates a singly linked list of n nodes ending in null pointer.
//!can throw std::bad_alloc //!can throw bad_alloc
void allocate_nodes(const std::size_t n, multiallocation_chain &chain) void allocate_nodes(const std::size_t n, multiallocation_chain &chain)
{ {
//----------------------- //-----------------------

View File

@ -82,7 +82,7 @@ class shared_node_pool
~shared_node_pool() ~shared_node_pool()
{} {}
//!Allocates array of count elements. Can throw std::bad_alloc //!Allocates array of count elements. Can throw bad_alloc
void *allocate_node() void *allocate_node()
{ {
//----------------------- //-----------------------
@ -101,7 +101,7 @@ class shared_node_pool
} }
//!Allocates a singly linked list of n nodes ending in null pointer. //!Allocates a singly linked list of n nodes ending in null pointer.
//!can throw std::bad_alloc //!can throw bad_alloc
void allocate_nodes(const std::size_t n, multiallocation_chain &chain) void allocate_nodes(const std::size_t n, multiallocation_chain &chain)
{ {
//----------------------- //-----------------------

View File

@ -1269,7 +1269,7 @@ class devector
/** /**
* **Returns**: A reference to the `n`th element in the devector. * **Returns**: A reference to the `n`th element in the devector.
* *
* **Throws**: `std::out_of_range`, if `n >= size()`. * **Throws**: `out_of_range`, if `n >= size()`.
* *
* **Complexity**: Constant. * **Complexity**: Constant.
*/ */
@ -1284,7 +1284,7 @@ class devector
/** /**
* **Returns**: A constant reference to the `n`th element in the devector. * **Returns**: A constant reference to the `n`th element in the devector.
* *
* **Throws**: `std::out_of_range`, if `n >= size()`. * **Throws**: `out_of_range`, if `n >= size()`.
* *
* **Complexity**: Constant. * **Complexity**: Constant.
*/ */

View File

@ -151,7 +151,7 @@ class new_allocator
{} {}
//!Allocates memory for an array of count elements. //!Allocates memory for an array of count elements.
//!Throws std::bad_alloc if there is no enough memory //!Throws bad_alloc if there is no enough memory
pointer allocate(size_type count) pointer allocate(size_type count)
{ {
const std::size_t max_count = std::size_t(-1)/(2*sizeof(T)); const std::size_t max_count = std::size_t(-1)/(2*sizeof(T));

View File

@ -141,7 +141,7 @@ class node_allocator
{ return size_type(-1)/sizeof(T); } { return size_type(-1)/sizeof(T); }
//!Allocate memory for an array of count elements. //!Allocate memory for an array of count elements.
//!Throws std::bad_alloc if there is no enough memory //!Throws bad_alloc if there is no enough memory
pointer allocate(size_type count, const void * = 0) pointer allocate(size_type count, const void * = 0)
{ {
if(BOOST_UNLIKELY(count > this->max_size())) if(BOOST_UNLIKELY(count > this->max_size()))

View File

@ -137,10 +137,10 @@ struct get_static_vector_allocator
//!possible. //!possible.
//! //!
//!@par Error Handling //!@par Error Handling
//! Insertion beyond the capacity result in throwing std::bad_alloc() if exceptions are enabled or //! Insertion beyond the capacity result in throwing bad_alloc() if exceptions are enabled or
//! calling throw_bad_alloc() if not enabled. //! calling throw_bad_alloc() if not enabled.
//! //!
//! std::out_of_range is thrown if out of bounds access is performed in <code>at()</code> if exceptions are //! out_of_range is thrown if out of bounds access is performed in <code>at()</code> if exceptions are
//! enabled, throw_out_of_range() if not enabled. //! enabled, throw_out_of_range() if not enabled.
//! //!
//!@tparam T The type of element that will be stored. //!@tparam T The type of element that will be stored.
@ -788,7 +788,7 @@ public:
//! from the beginning of the container. //! from the beginning of the container.
//! //!
//! @par Throws //! @par Throws
//! \c std::out_of_range exception by default. //! \c out_of_range exception by default.
//! //!
//! @par Complexity //! @par Complexity
//! Constant O(1). //! Constant O(1).
@ -804,7 +804,7 @@ public:
//! from the beginning of the container. //! from the beginning of the container.
//! //!
//! @par Throws //! @par Throws
//! \c std::out_of_range exception by default. //! \c out_of_range exception by default.
//! //!
//! @par Complexity //! @par Complexity
//! Constant O(1). //! Constant O(1).

View File

@ -27,22 +27,27 @@
#include <exception> //for std exception base #include <exception> //for std exception base
# if defined(BOOST_CONTAINER_USE_STD_EXCEPTIONS) # if defined(BOOST_CONTAINER_USE_STD_EXCEPTIONS)
#include <stdexcept> //for std exception types #include <stdexcept> //for std::out_of_range, std::length_error, std::logic_error, std::runtime_error
#include <string> //for implicit std::string conversion #include <string> //for implicit std::string conversion
#include <new> //for std::bad_alloc #include <new> //for std::bad_alloc
namespace boost {
namespace container {
typedef std::bad_alloc bad_alloc_t; typedef std::bad_alloc bad_alloc_t;
typedef std::out_of_range out_of_range_t; typedef std::out_of_range out_of_range_t;
typedef std::out_of_range length_error_t; typedef std::length_error length_error_t;
typedef std::logic_error logic_error_t; typedef std::logic_error logic_error_t;
typedef std::runtime_error runtime_error_t; typedef std::runtime_error runtime_error_t;
}} //namespace boost::container
# else //!BOOST_CONTAINER_USE_STD_EXCEPTIONS # else //!BOOST_CONTAINER_USE_STD_EXCEPTIONS
namespace boost { namespace boost {
namespace container { namespace container {
class exception class BOOST_SYMBOL_EXPORT exception
: public ::std::exception : public ::std::exception
{ {
typedef ::std::exception std_exception_t; typedef ::std::exception std_exception_t;
@ -61,7 +66,7 @@ class exception
const char *m_msg; const char *m_msg;
}; };
class bad_alloc class BOOST_SYMBOL_EXPORT bad_alloc
: public exception : public exception
{ {
public: public:
@ -72,7 +77,7 @@ class bad_alloc
typedef bad_alloc bad_alloc_t; typedef bad_alloc bad_alloc_t;
class out_of_range class BOOST_SYMBOL_EXPORT out_of_range
: public exception : public exception
{ {
public: public:
@ -83,7 +88,7 @@ class out_of_range
typedef out_of_range out_of_range_t; typedef out_of_range out_of_range_t;
class length_error class BOOST_SYMBOL_EXPORT length_error
: public exception : public exception
{ {
public: public:
@ -94,7 +99,7 @@ class length_error
typedef out_of_range length_error_t; typedef out_of_range length_error_t;
class logic_error class BOOST_SYMBOL_EXPORT logic_error
: public exception : public exception
{ {
public: public:
@ -105,7 +110,7 @@ class logic_error
typedef logic_error logic_error_t; typedef logic_error logic_error_t;
class runtime_error class BOOST_SYMBOL_EXPORT runtime_error
: public exception : public exception
{ {
public: public:
@ -181,7 +186,11 @@ namespace container {
//! Exception callback called by Boost.Container when fails to allocate the requested storage space. //! Exception callback called by Boost.Container when fails to allocate the requested storage space.
//! <ul> //! <ul>
//! <li>If BOOST_NO_EXCEPTIONS is NOT defined <code>std::bad_alloc()</code> is thrown.</li> //! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is NOT defined
//! <code>boost::container::bad_alloc(str)</code> is thrown.</li>
//!
//! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is defined
//! <code>std::bad_alloc(str)</code> is thrown.</li>
//! //!
//! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
//! is NOT defined <code>BOOST_ASSERT(!"boost::container bad_alloc thrown")</code> is called //! is NOT defined <code>BOOST_ASSERT(!"boost::container bad_alloc thrown")</code> is called
@ -192,16 +201,16 @@ namespace container {
//! </ul> //! </ul>
BOOST_NORETURN inline void throw_bad_alloc() BOOST_NORETURN inline void throw_bad_alloc()
{ {
#ifdef BOOST_CONTAINER_USE_STD_EXCEPTIONS throw bad_alloc_t();
throw std::bad_alloc();
#else
throw bad_alloc();
#endif
} }
//! Exception callback called by Boost.Container to signal arguments out of range. //! Exception callback called by Boost.Container to signal arguments out of range.
//! <ul> //! <ul>
//! <li>If BOOST_NO_EXCEPTIONS is NOT defined <code>std::out_of_range(str)</code> is thrown.</li> //! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is NOT defined
//! <code>boost::container::out_of_range(str)</code> is thrown.</li>
//!
//! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is defined
//! <code>std::out_of_range(str)</code> is thrown.</li>
//! //!
//! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
//! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container out_of_range thrown", str)</code> is called //! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container out_of_range thrown", str)</code> is called
@ -212,16 +221,17 @@ namespace container {
//! </ul> //! </ul>
BOOST_NORETURN inline void throw_out_of_range(const char* str) BOOST_NORETURN inline void throw_out_of_range(const char* str)
{ {
#ifdef BOOST_CONTAINER_USE_STD_EXCEPTIONS throw out_of_range_t(str);
throw std::out_of_range(str);
#else
throw out_of_range(str);
#endif
} }
//! Exception callback called by Boost.Container to signal errors resizing. //! Exception callback called by Boost.Container to signal errors resizing.
//! <ul> //! <ul>
//! <li>If BOOST_NO_EXCEPTIONS is NOT defined <code>std::length_error(str)</code> is thrown.</li> //!
//! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is NOT defined
//! <code>boost::container::length_error(str)</code> is thrown.</li>
//!
//! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is defined
//! <code>std::length_error(str)</code> is thrown.</li>
//! //!
//! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
//! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container length_error thrown", str)</code> is called //! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container length_error thrown", str)</code> is called
@ -232,17 +242,18 @@ namespace container {
//! </ul> //! </ul>
BOOST_NORETURN inline void throw_length_error(const char* str) BOOST_NORETURN inline void throw_length_error(const char* str)
{ {
#ifdef BOOST_CONTAINER_USE_STD_EXCEPTIONS throw length_error_t(str);
throw std::length_error(str);
#else
throw length_error(str);
#endif
} }
//! Exception callback called by Boost.Container to report errors in the internal logical //! Exception callback called by Boost.Container to report errors in the internal logical
//! of the program, such as violation of logical preconditions or class invariants. //! of the program, such as violation of logical preconditions or class invariants.
//! <ul> //! <ul>
//! <li>If BOOST_NO_EXCEPTIONS is NOT defined <code>std::logic_error(str)</code> is thrown.</li> //!
//! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is NOT defined
//! <code>boost::container::logic_error(str)</code> is thrown.</li>
//!
//! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is defined
//! <code>std::logic_error(str)</code> is thrown.</li>
//! //!
//! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
//! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container logic_error thrown", str)</code> is called //! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container logic_error thrown", str)</code> is called
@ -253,16 +264,16 @@ namespace container {
//! </ul> //! </ul>
BOOST_NORETURN inline void throw_logic_error(const char* str) BOOST_NORETURN inline void throw_logic_error(const char* str)
{ {
#ifdef BOOST_CONTAINER_USE_STD_EXCEPTIONS throw logic_error_t(str);
throw std::logic_error(str);
#else
throw logic_error(str);
#endif
} }
//! Exception callback called by Boost.Container to report errors that can only be detected during runtime. //! Exception callback called by Boost.Container to report errors that can only be detected during runtime.
//! <ul> //! <ul>
//! <li>If BOOST_NO_EXCEPTIONS is NOT defined <code>std::runtime_error(str)</code> is thrown.</li> //! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is NOT defined
//! <code>boost::container::runtime_error(str)</code> is thrown.</li>
//!
//! <li>If BOOST_NO_EXCEPTIONS is NOT defined and BOOST_CONTAINER_USE_STD_EXCEPTIONS is defined
//! <code>std::runtime_error(str)</code> is thrown.</li>
//! //!
//! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS //! <li>If BOOST_NO_EXCEPTIONS is defined and BOOST_CONTAINER_USER_DEFINED_THROW_CALLBACKS
//! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container runtime_error thrown", str)</code> is called //! is NOT defined <code>BOOST_ASSERT_MSG(!"boost::container runtime_error thrown", str)</code> is called
@ -273,11 +284,7 @@ namespace container {
//! </ul> //! </ul>
BOOST_NORETURN inline void throw_runtime_error(const char* str) BOOST_NORETURN inline void throw_runtime_error(const char* str)
{ {
#ifdef BOOST_CONTAINER_USE_STD_EXCEPTIONS throw runtime_error_t(str);
throw std::runtime_error(str);
#else
throw runtime_error(str);
#endif
} }
#endif #endif