BOOST_NO_EXCEPTIONS support added.

[SVN r14835]
This commit is contained in:
Peter Dimov
2002-08-14 12:27:22 +00:00
parent e650c7ff16
commit a09c2e556f
4 changed files with 73 additions and 23 deletions

View File

@ -17,11 +17,13 @@
#include <boost/assert.hpp> #include <boost/assert.hpp>
#include <boost/checked_delete.hpp> #include <boost/checked_delete.hpp>
#include <boost/throw_exception.hpp>
#include <boost/detail/atomic_count.hpp> #include <boost/detail/atomic_count.hpp>
#include <cstddef> // for std::ptrdiff_t #include <cstddef> // for std::ptrdiff_t
#include <algorithm> // for std::swap #include <algorithm> // for std::swap
#include <functional> // for std::less #include <functional> // for std::less
#include <new> // for std::bad_alloc
namespace boost namespace boost
{ {
@ -38,22 +40,36 @@ public:
explicit shared_array(T * p = 0): px(p) explicit shared_array(T * p = 0): px(p)
{ {
#ifndef BOOST_NO_EXCEPTIONS
try // prevent leak if new throws try // prevent leak if new throws
{ {
pn = new count_type(1); pn = new count_type(1);
} }
catch(...) catch(...)
{ {
checked_array_delete(p); boost::checked_array_delete(p);
throw; throw;
} }
#else
pn = new count_type(1);
if(pn == 0)
{
boost::checked_array_delete(p);
boost::throw_exception(std::bad_alloc());
}
#endif
} }
~shared_array() ~shared_array()
{ {
if(--*pn == 0) if(--*pn == 0)
{ {
checked_array_delete(px); boost::checked_array_delete(px);
delete pn; delete pn;
} }
} }
@ -63,19 +79,19 @@ public:
pn = r.pn; pn = r.pn;
++*pn; ++*pn;
} }
shared_array & operator=(shared_array const & r) shared_array & operator=(shared_array const & r)
{ {
shared_array(r).swap(*this); shared_array(r).swap(*this);
return *this; return *this;
} }
void reset(T * p = 0) void reset(T * p = 0)
{ {
BOOST_ASSERT(p == 0 || p != px); BOOST_ASSERT(p == 0 || p != px);
shared_array(p).swap(*this); shared_array(p).swap(*this);
} }
T * get() const // never throws T * get() const // never throws
{ {
return px; return px;
@ -87,7 +103,7 @@ public:
BOOST_ASSERT(i >= 0); BOOST_ASSERT(i >= 0);
return px[i]; return px[i];
} }
long use_count() const // never throws long use_count() const // never throws
{ {
return *pn; return *pn;
@ -97,15 +113,15 @@ public:
{ {
return *pn == 1; return *pn == 1;
} }
void swap(shared_array<T> & other) // never throws void swap(shared_array<T> & other) // never throws
{ {
std::swap(px, other.px); std::swap(px, other.px);
std::swap(pn, other.pn); std::swap(pn, other.pn);
} }
private: private:
T * px; // contained pointer T * px; // contained pointer
count_type * pn; // ptr to reference counter count_type * pn; // ptr to reference counter

View File

@ -23,10 +23,12 @@
#endif #endif
#include <boost/checked_delete.hpp> #include <boost/checked_delete.hpp>
#include <boost/throw_exception.hpp>
#include <boost/detail/lightweight_mutex.hpp> #include <boost/detail/lightweight_mutex.hpp>
#include <functional> // for std::less #include <functional> // for std::less
#include <exception> // for std::exception #include <exception> // for std::exception
#include <new> // for std::bad_alloc
#ifdef __BORLANDC__ #ifdef __BORLANDC__
# pragma warn -8026 // Functions with excep. spec. are not expanded inline # pragma warn -8026 // Functions with excep. spec. are not expanded inline
@ -95,7 +97,7 @@ public:
#ifdef BOOST_HAS_THREADS #ifdef BOOST_HAS_THREADS
mutex_type::scoped_lock lock(mtx_); mutex_type::scoped_lock lock(mtx_);
#endif #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_; ++use_count_;
++weak_count_; ++weak_count_;
} }
@ -235,6 +237,8 @@ public:
template<class P, class D> shared_count(P p, D d, void const * = 0): pi_(0) template<class P, class D> shared_count(P p, D d, void const * = 0): pi_(0)
{ {
#ifndef BOOST_NO_EXCEPTIONS
try try
{ {
pi_ = new counted_base_impl<P, D>(p, d, 1, 1); pi_ = new counted_base_impl<P, D>(p, d, 1, 1);
@ -244,6 +248,18 @@ public:
d(p); // delete p d(p); // delete p
throw; throw;
} }
#else
pi_ = new counted_base_impl<P, D>(p, d, 1, 1);
if(pi_ == 0)
{
d(p); // delete p
boost::throw_exception(std::bad_alloc());
}
#endif
} }
template<class P, class D> shared_count(P, D, counted_base * pi): pi_(pi) template<class P, class D> shared_count(P, D, counted_base * pi): pi_(pi)

View File

@ -17,14 +17,16 @@
#include <boost/assert.hpp> #include <boost/assert.hpp>
#include <boost/checked_delete.hpp> #include <boost/checked_delete.hpp>
#include <boost/throw_exception.hpp>
#include <boost/detail/atomic_count.hpp> #include <boost/detail/atomic_count.hpp>
#ifndef BOOST_NO_AUTO_PTR #ifndef BOOST_NO_AUTO_PTR
#include <memory> // for std::auto_ptr # include <memory> // for std::auto_ptr
#endif #endif
#include <algorithm> // for std::swap #include <algorithm> // for std::swap
#include <functional> // for std::less #include <functional> // for std::less
#include <new> // for std::bad_alloc
namespace boost namespace boost
{ {
@ -38,25 +40,40 @@ private:
public: public:
typedef T element_type; typedef T element_type;
typedef T value_type;
explicit shared_ptr(T * p = 0): px(p) explicit shared_ptr(T * p = 0): px(p)
{ {
#ifndef BOOST_NO_EXCEPTIONS
try // prevent leak if new throws try // prevent leak if new throws
{ {
pn = new count_type(1); pn = new count_type(1);
} }
catch(...) catch(...)
{ {
checked_delete(p); boost::checked_delete(p);
throw; throw;
} }
#else
pn = new count_type(1);
if(pn == 0)
{
boost::checked_delete(p);
boost::throw_exception(std::bad_alloc());
}
#endif
} }
~shared_ptr() ~shared_ptr()
{ {
if(--*pn == 0) if(--*pn == 0)
{ {
checked_delete(px); boost::checked_delete(px);
delete pn; delete pn;
} }
} }

View File

@ -23,7 +23,7 @@
#include <boost/assert.hpp> #include <boost/assert.hpp>
#include <boost/checked_delete.hpp> #include <boost/checked_delete.hpp>
#include <boost/throw_exception.hpp>
#include <boost/detail/shared_count.hpp> #include <boost/detail/shared_count.hpp>
#include <memory> // for std::auto_ptr #include <memory> // for std::auto_ptr
@ -90,6 +90,7 @@ private:
public: public:
typedef T element_type; typedef T element_type;
typedef T value_type;
shared_ptr(): px(0), pn() shared_ptr(): px(0), pn()
{ {
@ -146,7 +147,7 @@ public:
{ {
if (px == 0) if (px == 0)
{ {
throw std::bad_cast(); boost::throw_exception(std::bad_cast());
} }
} }