apps/elftosb/common/smart_ptr.h
/*
* smart_ptr.h
* elftosb
*
* Created by Chris Reed on 4/18/06.
* Copyright 2006 __MyCompanyName__. All rights reserved.
*
*/
#if !defined(_smart_ptr_h_)
#define _smart_ptr_h_
template <typename T>
class smart_ptr
{
public:
typedef T data_type;
typedef T *ptr_type;
typedef const T *const_ptr_type;
typedef T &ref_type;
typedef const T &const_ref_type;
smart_ptr()
: _p(0)
{
}
smart_ptr(ptr_type p)
: _p(p)
{
}
virtual ~smart_ptr() { safe_delete(); }
ptr_type get() { return _p; }
const_ptr_type get() const { return _p; }
void set(ptr_type p)
{
if (_p && p != _p)
{
safe_delete();
}
_p = p;
}
void reset() { _p = 0; }
void clear() { safe_delete(); }
virtual void safe_delete()
{
if (_p)
{
delete _p;
_p = 0;
}
}
operator ptr_type() { return _p; }
operator const_ptr_type() const { return _p; }
operator ref_type() { return *_p; }
operator const_ref_type() const { return *_p; }
operator bool() const { return _p != 0; }
smart_ptr<T> &operator=(const_ptr_type p)
{
set(const_cast<ptr_type>(p));
return *this;
}
ptr_type operator->() { return _p; }
const_ptr_type operator->() const { return _p; }
// //! Pointer dereferencing operator.
// ref_type operator * () const { return *_p; }
//
// //! Const version of the pointer dereference operator.
// const_ref_type operator * () const { return *_p; }
protected:
ptr_type _p;
};
template <typename T>
class smart_array_ptr
{
public:
typedef T data_type;
typedef T *ptr_type;
typedef const T *const_ptr_type;
typedef T &ref_type;
typedef const T &const_ref_type;
smart_array_ptr()
: _p(0)
{
}
smart_array_ptr(ptr_type p)
: _p(p)
{
}
virtual ~smart_array_ptr() { safe_delete(); }
ptr_type get() { return _p; }
const_ptr_type get() const { return _p; }
void set(ptr_type p)
{
if (_p && p != _p)
{
safe_delete();
}
_p = p;
}
void reset() { _p = 0; }
void clear() { safe_delete(); }
virtual void safe_delete()
{
if (_p)
{
delete[] _p;
_p = 0;
}
}
operator ptr_type() { return _p; }
operator const_ptr_type() const { return _p; }
operator ref_type() { return *_p; }
operator const_ref_type() const { return *_p; }
operator bool() const { return _p != 0; }
smart_array_ptr<T> &operator=(const_ptr_type p)
{
set(const_cast<ptr_type>(p));
return *this;
}
ptr_type operator->() { return _p; }
const_ptr_type operator->() const { return _p; }
ref_type operator[](unsigned index) { return _p[index]; }
const_ref_type operator[](unsigned index) const { return _p[index]; }
// //! Pointer dereferencing operator.
// ref_type operator * () const { return *_p; }
//
// //! Const version of the pointer dereference operator.
// const_ref_type operator * () const { return *_p; }
protected:
ptr_type _p;
};
#endif // _smart_ptr_h_
smart_ptr::clear
void clear()
Dissociates a previously set pointer value, deleting it at the same time.
Definition: apps/elftosb/common/smart_ptr.h:62
smart_ptr::safe_delete
virtual void safe_delete()
Definition: apps/elftosb/common/smart_ptr.h:66
smart_array_ptr::smart_array_ptr
smart_array_ptr()
Default constuctor. Initialises with no pointer set.
Definition: apps/elftosb/common/smart_ptr.h:131
smart_array_ptr::_p
ptr_type _p
The wrapped pointer.
Definition: apps/elftosb/common/smart_ptr.h:215
smart_array_ptr::~smart_array_ptr
virtual ~smart_array_ptr()
Definition: apps/elftosb/common/smart_ptr.h:144
smart_array_ptr::safe_delete
virtual void safe_delete()
Definition: apps/elftosb/common/smart_ptr.h:169
smart_ptr::smart_ptr
smart_ptr()
Default constuctor. Initialises with no pointer set.
Definition: apps/elftosb/common/smart_ptr.h:28
smart_array_ptr::operator=
smart_array_ptr< T > & operator=(const_ptr_type p)
To allow setting the pointer directly. Equivalent to a call to set().
Definition: apps/elftosb/common/smart_ptr.h:192
smart_ptr::smart_ptr
smart_ptr(ptr_type p)
This constructor takes a pointer to the object to be deleted.
Definition: apps/elftosb/common/smart_ptr.h:34
smart_ptr
Simple, standard smart pointer class.
Definition: apps/elftosb/common/smart_ptr.h:18
smart_ptr::reset
void reset()
Dissociates any previously set pointer value without deleting it.
Definition: apps/elftosb/common/smart_ptr.h:60
smart_array_ptr::reset
void reset()
Dissociates any previously set pointer value without deleting it.
Definition: apps/elftosb/common/smart_ptr.h:163
smart_array_ptr::clear
void clear()
Dissociates a previously set pointer value, deleting it at the same time.
Definition: apps/elftosb/common/smart_ptr.h:165
smart_ptr::_p
ptr_type _p
The wrapped pointer.
Definition: apps/elftosb/common/smart_ptr.h:108
smart_array_ptr::operator->
const_ptr_type operator->() const
Another operator to allow you to treat the object just like a pointer.
Definition: apps/elftosb/common/smart_ptr.h:201
smart_array_ptr::smart_array_ptr
smart_array_ptr(ptr_type p)
This constructor takes a pointer to the object to be deleted.
Definition: apps/elftosb/common/smart_ptr.h:137
smart_ptr::operator->
const_ptr_type operator->() const
Another operator to allow you to treat the object just like a pointer.
Definition: apps/elftosb/common/smart_ptr.h:98
smart_array_ptr
Simple, standard smart pointer class that uses the array delete operator.
Definition: apps/elftosb/common/smart_ptr.h:121
smart_ptr::operator->
ptr_type operator->()
Another operator to allow you to treat the object just like a pointer.
Definition: apps/elftosb/common/smart_ptr.h:96
smart_ptr::operator=
smart_ptr< T > & operator=(const_ptr_type p)
To allow setting the pointer directly. Equivalent to a call to set().
Definition: apps/elftosb/common/smart_ptr.h:89
smart_array_ptr::operator->
ptr_type operator->()
Another operator to allow you to treat the object just like a pointer.
Definition: apps/elftosb/common/smart_ptr.h:199
smart_array_ptr::operator[]
ref_type operator[](unsigned index)
Indexing operator.
Definition: apps/elftosb/common/smart_ptr.h:203
smart_array_ptr::operator[]
const_ref_type operator[](unsigned index) const
Indexing operator.
Definition: apps/elftosb/common/smart_ptr.h:205
smart_ptr::~smart_ptr
virtual ~smart_ptr()
Definition: apps/elftosb/common/smart_ptr.h:41