src/blfwk/smart_ptr.h
/*
* Copyright (c) 2013-14, Freescale Semiconductor, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#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]; }
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: src/blfwk/smart_ptr.h:86
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: src/blfwk/smart_ptr.h:155
smart_array_ptr::~smart_array_ptr
virtual ~smart_array_ptr()
Definition: src/blfwk/smart_ptr.h:168
smart_array_ptr::safe_delete
virtual void safe_delete()
Definition: src/blfwk/smart_ptr.h:193
smart_ptr::smart_ptr
smart_ptr()
Default constuctor. Initialises with no pointer set.
Definition: src/blfwk/smart_ptr.h:52
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: src/blfwk/smart_ptr.h:216
smart_ptr::smart_ptr
smart_ptr(ptr_type p)
This constructor takes a pointer to the object to be deleted.
Definition: src/blfwk/smart_ptr.h:58
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: src/blfwk/smart_ptr.h:84
smart_array_ptr::reset
void reset()
Dissociates any previously set pointer value without deleting it.
Definition: src/blfwk/smart_ptr.h:187
smart_array_ptr::clear
void clear()
Dissociates a previously set pointer value, deleting it at the same time.
Definition: src/blfwk/smart_ptr.h:189
smart_ptr::_p
ptr_type _p
The wrapped pointer.
Definition: apps/elftosb/common/smart_ptr.h:108
elftosb::Keyblob
Keyblob specification.
Definition: Keyblob.h:27
smart_array_ptr::operator->
const_ptr_type operator->() const
Another operator to allow you to treat the object just like a pointer.
Definition: src/blfwk/smart_ptr.h:225
smart_array_ptr::smart_array_ptr
smart_array_ptr(ptr_type p)
This constructor takes a pointer to the object to be deleted.
Definition: src/blfwk/smart_ptr.h:161
smart_ptr::operator->
const_ptr_type operator->() const
Another operator to allow you to treat the object just like a pointer.
Definition: src/blfwk/smart_ptr.h:122
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: src/blfwk/smart_ptr.h:120
smart_ptr::operator=
smart_ptr< T > & operator=(const_ptr_type p)
To allow setting the pointer directly. Equivalent to a call to set().
Definition: src/blfwk/smart_ptr.h:113
smart_array_ptr::operator->
ptr_type operator->()
Another operator to allow you to treat the object just like a pointer.
Definition: src/blfwk/smart_ptr.h:223
smart_array_ptr::operator[]
ref_type operator[](unsigned index)
Indexing operator.
Definition: src/blfwk/smart_ptr.h:227
smart_array_ptr::operator[]
const_ref_type operator[](unsigned index) const
Indexing operator.
Definition: src/blfwk/smart_ptr.h:229
smart_ptr::~smart_ptr
virtual ~smart_ptr()
Definition: src/blfwk/smart_ptr.h:65