From a09c2e556f9421da9f0e7e74b98c89a5599b17f8 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Wed, 14 Aug 2002 12:27:22 +0000 Subject: [PATCH] BOOST_NO_EXCEPTIONS support added. [SVN r14835] --- include/boost/detail/shared_array_nmt.hpp | 44 +++++++++++++++-------- include/boost/detail/shared_count.hpp | 18 +++++++++- include/boost/detail/shared_ptr_nmt.hpp | 29 +++++++++++---- include/boost/shared_ptr.hpp | 5 +-- 4 files changed, 73 insertions(+), 23 deletions(-) diff --git a/include/boost/detail/shared_array_nmt.hpp b/include/boost/detail/shared_array_nmt.hpp index 3ff1954..8e9b727 100644 --- a/include/boost/detail/shared_array_nmt.hpp +++ b/include/boost/detail/shared_array_nmt.hpp @@ -17,11 +17,13 @@ #include #include +#include #include -#include // for std::ptrdiff_t -#include // for std::swap -#include // for std::less +#include // for std::ptrdiff_t +#include // for std::swap +#include // for std::less +#include // for std::bad_alloc namespace boost { @@ -38,22 +40,36 @@ public: explicit shared_array(T * p = 0): px(p) { +#ifndef BOOST_NO_EXCEPTIONS + try // prevent leak if new throws { pn = new count_type(1); } catch(...) { - checked_array_delete(p); + boost::checked_array_delete(p); throw; - } + } + +#else + + pn = new count_type(1); + + if(pn == 0) + { + boost::checked_array_delete(p); + boost::throw_exception(std::bad_alloc()); + } + +#endif } - + ~shared_array() { if(--*pn == 0) { - checked_array_delete(px); + boost::checked_array_delete(px); delete pn; } } @@ -63,19 +79,19 @@ public: pn = r.pn; ++*pn; } - + shared_array & operator=(shared_array const & r) { shared_array(r).swap(*this); return *this; } - + void reset(T * p = 0) { BOOST_ASSERT(p == 0 || p != px); shared_array(p).swap(*this); } - + T * get() const // never throws { return px; @@ -87,7 +103,7 @@ public: BOOST_ASSERT(i >= 0); return px[i]; } - + long use_count() const // never throws { return *pn; @@ -97,15 +113,15 @@ public: { return *pn == 1; } - + void swap(shared_array & other) // never throws { std::swap(px, other.px); std::swap(pn, other.pn); } - + private: - + T * px; // contained pointer count_type * pn; // ptr to reference counter diff --git a/include/boost/detail/shared_count.hpp b/include/boost/detail/shared_count.hpp index 673a632..17aeb9d 100644 --- a/include/boost/detail/shared_count.hpp +++ b/include/boost/detail/shared_count.hpp @@ -23,10 +23,12 @@ #endif #include +#include #include #include // for std::less #include // for std::exception +#include // for std::bad_alloc #ifdef __BORLANDC__ # pragma warn -8026 // Functions with excep. spec. are not expanded inline @@ -95,7 +97,7 @@ public: #ifdef BOOST_HAS_THREADS mutex_type::scoped_lock lock(mtx_); #endif - if(use_count_ == 0 && weak_count_ != 0) throw use_count_is_zero(); + if(use_count_ == 0 && weak_count_ != 0) boost::throw_exception(boost::use_count_is_zero()); ++use_count_; ++weak_count_; } @@ -235,6 +237,8 @@ public: template shared_count(P p, D d, void const * = 0): pi_(0) { +#ifndef BOOST_NO_EXCEPTIONS + try { pi_ = new counted_base_impl(p, d, 1, 1); @@ -244,6 +248,18 @@ public: d(p); // delete p throw; } + +#else + + pi_ = new counted_base_impl(p, d, 1, 1); + + if(pi_ == 0) + { + d(p); // delete p + boost::throw_exception(std::bad_alloc()); + } + +#endif } template shared_count(P, D, counted_base * pi): pi_(pi) diff --git a/include/boost/detail/shared_ptr_nmt.hpp b/include/boost/detail/shared_ptr_nmt.hpp index 79d5b5d..14fbe65 100644 --- a/include/boost/detail/shared_ptr_nmt.hpp +++ b/include/boost/detail/shared_ptr_nmt.hpp @@ -17,14 +17,16 @@ #include #include +#include #include #ifndef BOOST_NO_AUTO_PTR -#include // for std::auto_ptr +# include // for std::auto_ptr #endif -#include // for std::swap -#include // for std::less +#include // for std::swap +#include // for std::less +#include // for std::bad_alloc namespace boost { @@ -38,25 +40,40 @@ private: public: typedef T element_type; + typedef T value_type; explicit shared_ptr(T * p = 0): px(p) { +#ifndef BOOST_NO_EXCEPTIONS + try // prevent leak if new throws { pn = new count_type(1); } catch(...) { - checked_delete(p); + boost::checked_delete(p); throw; - } + } + +#else + + pn = new count_type(1); + + if(pn == 0) + { + boost::checked_delete(p); + boost::throw_exception(std::bad_alloc()); + } + +#endif } ~shared_ptr() { if(--*pn == 0) { - checked_delete(px); + boost::checked_delete(px); delete pn; } } diff --git a/include/boost/shared_ptr.hpp b/include/boost/shared_ptr.hpp index 2634929..8f55948 100644 --- a/include/boost/shared_ptr.hpp +++ b/include/boost/shared_ptr.hpp @@ -23,7 +23,7 @@ #include #include - +#include #include #include // for std::auto_ptr @@ -90,6 +90,7 @@ private: public: typedef T element_type; + typedef T value_type; shared_ptr(): px(0), pn() { @@ -146,7 +147,7 @@ public: { if (px == 0) { - throw std::bad_cast(); + boost::throw_exception(std::bad_cast()); } }