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/checked_delete.hpp>
#include <boost/throw_exception.hpp>
#include <boost/detail/atomic_count.hpp>
#include <cstddef> // for std::ptrdiff_t
#include <algorithm> // for std::swap
#include <functional> // for std::less
#include <cstddef> // for std::ptrdiff_t
#include <algorithm> // for std::swap
#include <functional> // for std::less
#include <new> // 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<T> & 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

View File

@ -23,10 +23,12 @@
#endif
#include <boost/checked_delete.hpp>
#include <boost/throw_exception.hpp>
#include <boost/detail/lightweight_mutex.hpp>
#include <functional> // for std::less
#include <exception> // for std::exception
#include <new> // 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<class P, class D> shared_count(P p, D d, void const * = 0): pi_(0)
{
#ifndef BOOST_NO_EXCEPTIONS
try
{
pi_ = new counted_base_impl<P, D>(p, d, 1, 1);
@ -244,6 +248,18 @@ public:
d(p); // delete p
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)

View File

@ -17,14 +17,16 @@
#include <boost/assert.hpp>
#include <boost/checked_delete.hpp>
#include <boost/throw_exception.hpp>
#include <boost/detail/atomic_count.hpp>
#ifndef BOOST_NO_AUTO_PTR
#include <memory> // for std::auto_ptr
# include <memory> // for std::auto_ptr
#endif
#include <algorithm> // for std::swap
#include <functional> // for std::less
#include <algorithm> // for std::swap
#include <functional> // for std::less
#include <new> // 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;
}
}

View File

@ -23,7 +23,7 @@
#include <boost/assert.hpp>
#include <boost/checked_delete.hpp>
#include <boost/throw_exception.hpp>
#include <boost/detail/shared_count.hpp>
#include <memory> // 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());
}
}