2002-01-22 13:38:52 +00:00
|
|
|
#ifndef BOOST_SCOPED_PTR_HPP_INCLUDED
|
|
|
|
#define BOOST_SCOPED_PTR_HPP_INCLUDED
|
|
|
|
|
|
|
|
// (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
|
|
|
|
// Copyright (c) 2001, 2002 Peter Dimov
|
|
|
|
//
|
|
|
|
// Permission to copy, use, modify, sell and distribute this software
|
|
|
|
// is granted provided this copyright notice appears in all copies.
|
|
|
|
// This software is provided "as is" without express or implied
|
|
|
|
// warranty, and with no claim as to its suitability for any purpose.
|
|
|
|
//
|
2002-02-04 11:15:40 +00:00
|
|
|
// See http://www.boost.org/libs/smart_ptr/scoped_ptr.htm for documentation.
|
|
|
|
//
|
2002-01-22 13:38:52 +00:00
|
|
|
|
|
|
|
#include <boost/assert.hpp>
|
2002-02-16 13:23:01 +00:00
|
|
|
#include <boost/checked_delete.hpp>
|
2002-01-22 13:38:52 +00:00
|
|
|
|
|
|
|
namespace boost
|
|
|
|
{
|
|
|
|
|
|
|
|
// scoped_ptr mimics a built-in pointer except that it guarantees deletion
|
|
|
|
// of the object pointed to, either on destruction of the scoped_ptr or via
|
2002-02-02 18:36:12 +00:00
|
|
|
// an explicit reset(). scoped_ptr is a simple solution for simple needs;
|
2002-01-22 13:38:52 +00:00
|
|
|
// use shared_ptr or std::auto_ptr if your needs are more complex.
|
|
|
|
|
|
|
|
template<typename T> class scoped_ptr // noncopyable
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
T* ptr;
|
|
|
|
|
|
|
|
scoped_ptr(scoped_ptr const &);
|
|
|
|
scoped_ptr & operator=(scoped_ptr const &);
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
typedef T element_type;
|
|
|
|
|
2002-02-02 18:36:12 +00:00
|
|
|
explicit scoped_ptr(T * p = 0): ptr(p) // never throws
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-02-02 18:36:12 +00:00
|
|
|
~scoped_ptr() // never throws
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
2002-02-16 13:23:01 +00:00
|
|
|
checked_delete(ptr);
|
2002-01-22 13:38:52 +00:00
|
|
|
}
|
|
|
|
|
2002-02-02 18:36:12 +00:00
|
|
|
void reset(T * p = 0) // never throws
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
2002-02-02 18:36:12 +00:00
|
|
|
if (ptr != p)
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
2002-02-16 13:23:01 +00:00
|
|
|
checked_delete(ptr);
|
2002-01-22 13:38:52 +00:00
|
|
|
ptr = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-02 18:36:12 +00:00
|
|
|
T & operator*() const // never throws
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
|
|
|
BOOST_ASSERT(ptr != 0);
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
2002-02-02 18:36:12 +00:00
|
|
|
T * operator->() const // never throws
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
|
|
|
BOOST_ASSERT(ptr != 0);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2002-02-02 18:36:12 +00:00
|
|
|
T * get() const // never throws
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2002-02-02 18:36:12 +00:00
|
|
|
void swap(scoped_ptr & b) // never throws
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
2002-02-02 18:36:12 +00:00
|
|
|
T * tmp = b.ptr;
|
|
|
|
b.ptr = ptr;
|
2002-01-22 13:38:52 +00:00
|
|
|
ptr = tmp;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2002-02-02 18:36:12 +00:00
|
|
|
template<typename T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) // never throws
|
2002-01-22 13:38:52 +00:00
|
|
|
{
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace boost
|
|
|
|
|
2002-02-02 18:36:12 +00:00
|
|
|
#endif // #ifndef BOOST_SCOPED_PTR_HPP_INCLUDED
|