cleanup this code, making it more "llvm-like".

Add comments to reset indicating that it deletes its pointer.
Add a new take() method, which can be used to get the pointer
without it being deleted.

llvm-svn: 45112
This commit is contained in:
Chris Lattner 2007-12-17 18:58:23 +00:00
parent 3a0d757bd5
commit ea78f7ae10

View File

@ -23,104 +23,99 @@
// //
// http://www.boost.org/libs/smart_ptr/scoped_ptr.htm // http://www.boost.org/libs/smart_ptr/scoped_ptr.htm
// //
#ifndef LLVM_SCOPED_PTR_H_INCLUDED
#ifndef LLVM_SCOPED_PTR_HPP_INCLUDED #define LLVM_SCOPED_PTR_H_INCLUDED
#define LLVM_SCOPED_PTR_HPP_INCLUDED
#include <cassert> #include <cassert>
namespace llvm { namespace llvm {
// verify that types are complete for increased safety // verify that types are complete for increased safety
template<class T> inline void checked_delete(T * x) { template<class T>
// intentionally complex - simplification causes regressions inline void checked_delete(T * x) {
typedef char type_must_be_complete[ sizeof(T)? 1: -1 ]; // intentionally complex - simplification causes warnings in some compilers.
(void) sizeof(type_must_be_complete); typedef char type_must_be_complete[sizeof(T) ? 1 : -1];
(void)sizeof(type_must_be_complete);
delete x; delete x;
} }
// scoped_ptr mimics a built-in pointer except that it guarantees deletion /// 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 /// of the object pointed to, either on destruction of the scoped_ptr or via
// an explicit reset(). scoped_ptr is a simple solution for simple needs; /// an explicit reset(). scoped_ptr is a simple solution for simple needs;
// use shared_ptr or std::auto_ptr if your needs are more complex. /// use shared_ptr or std::auto_ptr if your needs are more complex.
template<class T>
template<class T> class scoped_ptr // noncopyable class scoped_ptr {// noncopyable
{ T *ptr;
private: scoped_ptr(scoped_ptr const &); // DO NOT IMPLEMENT
scoped_ptr & operator=(scoped_ptr const &); // DO NOT IMPLEMENT
T * ptr;
scoped_ptr(scoped_ptr const &);
scoped_ptr & operator=(scoped_ptr const &);
typedef scoped_ptr<T> this_type; typedef scoped_ptr<T> this_type;
public: public:
typedef T element_type; typedef T element_type;
explicit scoped_ptr(T * p = 0): ptr(p) // never throws explicit scoped_ptr(T * p = 0): ptr(p) {} // never throws
{
}
~scoped_ptr() // never throws ~scoped_ptr() { // never throws
{
llvm::checked_delete(ptr); llvm::checked_delete(ptr);
} }
void reset(T * p = 0) // never throws /// reset - Change the current pointee to the specified pointer. Note that
{ /// calling this with any pointer (including a null pointer) deletes the
assert( (p == 0 || p != ptr) && "scoped_ptr: self-reset error"); // catch self-reset errors /// current pointer.
this_type(p).swap(*this); void reset(T *p = 0) {
// catch self-reset errors
assert((p == 0 || p != ptr) && "scoped_ptr: self-reset error");
T *tmp = ptr;
ptr = p;
delete tmp;
} }
T & operator*() const // never throws /// take - Reset the scoped pointer to null and return its pointer. This does
{ /// not delete the pointer before returning it.
assert(ptr != 0 && "scoped_ptr: Trying to dereference a null pointeur"); T *take() {
T *P = ptr;
ptr = 0;
return P;
}
T& operator*() const {
assert(ptr != 0 && "scoped_ptr: Trying to dereference a null pointer");
return *ptr; return *ptr;
} }
T * operator->() const // never throws T* operator->() const {
{ assert(ptr != 0 && "scoped_ptr: Trying to dereference a null pointer");
assert(ptr != 0 && "scoped_ptr: Trying to dereference a null pointeur");
return ptr; return ptr;
} }
T * get() const // never throws T* get() const {
{
return ptr; return ptr;
} }
// implicit conversion to "bool" // implicit conversion to "bool"
typedef T * this_type::*unspecified_bool_type; typedef T * this_type::*unspecified_bool_type;
operator unspecified_bool_type() const // never throws operator unspecified_bool_type() const {// never throws
{
return ptr == 0? 0: &this_type::ptr; return ptr == 0? 0: &this_type::ptr;
} }
bool operator! () const // never throws bool operator!() const { // never throws
{
return ptr == 0; return ptr == 0;
} }
void swap(scoped_ptr & b) // never throws void swap(scoped_ptr &b) {// never throws
{
T * tmp = b.ptr; T * tmp = b.ptr;
b.ptr = ptr; b.ptr = ptr;
ptr = tmp; ptr = tmp;
} }
}; };
template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) // never throws template<class T> inline void swap(scoped_ptr<T> &a, scoped_ptr<T> &b) {
{ // never throws
a.swap(b); a.swap(b);
} }
// get_pointer(p) is a generic way to say p.get() // get_pointer(p) is a generic way to say p.get()
template<class T> inline T * get_pointer(scoped_ptr<T> const &p) {
template<class T> inline T * get_pointer(scoped_ptr<T> const & p)
{
return p.get(); return p.get();
} }