2013-11-13 12:07:21 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// Inline and template implementations for
|
|
|
|
// the Utility Collection of Miscellanious Handy Functions
|
2013-11-28 16:06:01 +01:00
|
|
|
//
|
|
|
|
// Created 2013 by Dan Andersson
|
|
|
|
// Edited 2013 by
|
|
|
|
// * Dan Andersson
|
|
|
|
// * Dennis Andersen
|
2013-11-13 12:07:21 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef UTILITIES_INLINE_IMPL_H
|
|
|
|
#define UTILITIES_INLINE_IMPL_H
|
|
|
|
|
|
|
|
#include "Utilities.h"
|
|
|
|
|
|
|
|
namespace Utility
|
|
|
|
{
|
|
|
|
namespace DynamicMemory
|
|
|
|
{
|
|
|
|
template<typename Type>
|
|
|
|
inline void SafeDeleteInstance( Type *dynamicInstance )
|
|
|
|
{
|
|
|
|
if( dynamicInstance )
|
|
|
|
{
|
|
|
|
delete dynamicInstance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
void SafeDeleteArray( Type dynamicArray[] )
|
|
|
|
{
|
|
|
|
if( dynamicArray )
|
|
|
|
{
|
|
|
|
delete [] dynamicArray;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-29 09:23:08 +01:00
|
|
|
#pragma region UnuiqePointer
|
2013-11-13 12:07:21 +01:00
|
|
|
template<typename Type>
|
|
|
|
UniquePointer<Type>::UniquePointer( Type *assignedInstance )
|
|
|
|
{
|
|
|
|
this->ownedInstance = assignedInstance;
|
|
|
|
}
|
|
|
|
|
2013-11-26 09:16:51 +01:00
|
|
|
template<typename Type>
|
|
|
|
UniquePointer<Type>::UniquePointer( const UniquePointer<Type> &donor )
|
|
|
|
{
|
|
|
|
this->ownedInstance = donor.ownedInstance;
|
|
|
|
donor.ownedInstance = NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-13 12:07:21 +01:00
|
|
|
template<typename Type>
|
|
|
|
UniquePointer<Type>::~UniquePointer()
|
|
|
|
{
|
|
|
|
SafeDeleteInstance( this->ownedInstance );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
UniquePointer<Type> & UniquePointer<Type>::operator = ( const UniquePointer<Type> &donor )
|
|
|
|
{
|
|
|
|
SafeDeleteInstance( this->ownedInstance );
|
|
|
|
this->ownedInstance = donor.ownedInstance;
|
|
|
|
donor.ownedInstance = NULL;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
UniquePointer<Type>::operator Type* ()
|
|
|
|
{
|
|
|
|
return this->ownedInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
UniquePointer<Type>::operator const Type* () const
|
|
|
|
{
|
|
|
|
return this->ownedInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
Type * UniquePointer<Type>::operator -> ()
|
|
|
|
{
|
|
|
|
return this->ownedInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
const Type * UniquePointer<Type>::operator -> () const
|
|
|
|
{
|
|
|
|
return this->ownedInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
UniquePointer<Type>::operator bool() const
|
|
|
|
{
|
|
|
|
return this->ownedInstance != NULL;
|
|
|
|
}
|
|
|
|
|
2013-11-28 11:15:25 +01:00
|
|
|
template<typename Type>
|
|
|
|
bool UniquePointer<Type>::operator == ( Type *stray ) const
|
|
|
|
{
|
|
|
|
return this->ownedInstance == stray;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
bool UniquePointer<Type>::operator != ( Type *stray ) const
|
|
|
|
{
|
|
|
|
return this->ownedInstance != stray;
|
|
|
|
}
|
|
|
|
|
2013-11-13 12:07:21 +01:00
|
|
|
template<typename Type>
|
|
|
|
Type* UniquePointer<Type>::Release()
|
|
|
|
{
|
|
|
|
Type *copy = this->ownedInstance;
|
|
|
|
this->ownedInstance = NULL;
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
inline bool UniquePointer<Type>::HaveOwnership() const
|
|
|
|
{
|
|
|
|
return this->operator bool();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
UniqueArray<Type>::UniqueArray( Type assignedArray[] )
|
|
|
|
{
|
|
|
|
this->ownedArray = assignedArray;
|
|
|
|
}
|
|
|
|
|
2013-11-26 09:16:51 +01:00
|
|
|
template<typename Type>
|
|
|
|
UniqueArray<Type>::UniqueArray( const UniqueArray<Type> &donor )
|
|
|
|
{
|
|
|
|
this->ownedArray = donor.ownedArray;
|
|
|
|
donor.ownedArray = NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-13 12:07:21 +01:00
|
|
|
template<typename Type>
|
|
|
|
UniqueArray<Type>::~UniqueArray()
|
|
|
|
{
|
|
|
|
SafeDeleteArray( this->ownedArray );
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
2013-11-13 14:48:24 +01:00
|
|
|
UniqueArray<Type> & UniqueArray<Type>::operator = ( const UniqueArray<Type> &donor )
|
2013-11-13 12:07:21 +01:00
|
|
|
{
|
|
|
|
SafeDeleteArray( this->ownedArray );
|
|
|
|
this->ownedArray = donor.ownedInstance;
|
|
|
|
donor.owned = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type> template<typename Index>
|
2013-11-13 14:48:24 +01:00
|
|
|
Type & UniqueArray<Type>::operator [] ( Index i )
|
2013-11-13 12:07:21 +01:00
|
|
|
{
|
|
|
|
return this->ownedArray[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type> template<typename Index>
|
2013-11-13 14:48:24 +01:00
|
|
|
const Type & UniqueArray<Type>::operator [] ( Index i ) const
|
2013-11-13 12:07:21 +01:00
|
|
|
{
|
|
|
|
return this->ownedArray[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
UniqueArray<Type>::operator bool () const
|
|
|
|
{
|
|
|
|
return this->ownedArray != NULL;
|
|
|
|
}
|
|
|
|
|
2013-11-28 11:15:25 +01:00
|
|
|
template<typename Type>
|
|
|
|
bool UniqueArray<Type>::operator == ( Type *stray ) const
|
|
|
|
{
|
|
|
|
return this->ownedArray == stray;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
bool UniqueArray<Type>::operator != ( Type *stray ) const
|
|
|
|
{
|
|
|
|
return this->ownedArray != stray;
|
|
|
|
}
|
|
|
|
|
2013-11-13 12:07:21 +01:00
|
|
|
template<typename Type>
|
|
|
|
Type* UniqueArray<Type>::Release()
|
|
|
|
{
|
|
|
|
Type *copy = this->ownedArray;
|
|
|
|
this->ownedArray = NULL;
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
inline bool UniqueArray<Type>::HaveOwnership() const
|
|
|
|
{
|
|
|
|
return this->operator bool();
|
|
|
|
}
|
2013-11-29 09:23:08 +01:00
|
|
|
#pragma endregion
|
|
|
|
|
|
|
|
#pragma region SmartPointer
|
|
|
|
template<typename T> void SmartPointer<T>::Destroy()
|
2013-11-26 21:08:34 +01:00
|
|
|
{
|
2013-11-29 09:23:08 +01:00
|
|
|
delete this->_rc;
|
|
|
|
this->_rc = NULL;
|
|
|
|
|
|
|
|
//Use default function for memory deallocation.
|
|
|
|
SafeDeleteInstance<T>(this->_ptr);
|
|
|
|
|
|
|
|
this->_ptr = NULL;
|
|
|
|
}
|
|
|
|
template<typename T> SmartPointer<T>::SmartPointer()
|
|
|
|
:_rc(0), _ptr(0)
|
|
|
|
{ }
|
2013-12-06 11:46:47 +01:00
|
|
|
template<typename T> SmartPointer<T>::SmartPointer(UniquePointer<T>& p)
|
|
|
|
:_ptr(p.Release())
|
|
|
|
{
|
|
|
|
this->_rc = new ReferenceCount();
|
|
|
|
this->_rc->Incref();
|
|
|
|
}
|
2013-11-29 09:23:08 +01:00
|
|
|
template<typename T> SmartPointer<T>::SmartPointer(T* p)
|
|
|
|
:_ptr(p)
|
|
|
|
{
|
|
|
|
this->_rc = new ReferenceCount();
|
|
|
|
this->_rc->Incref();
|
|
|
|
}
|
|
|
|
template<typename T> SmartPointer<T>::SmartPointer(const SmartPointer& d)
|
|
|
|
:_ptr(d._ptr), _rc(d._rc)
|
|
|
|
{
|
|
|
|
if(this->_rc)
|
2013-11-26 21:08:34 +01:00
|
|
|
this->_rc->Incref();
|
2013-11-29 09:23:08 +01:00
|
|
|
}
|
|
|
|
template<typename T> SmartPointer<T>::~SmartPointer()
|
|
|
|
{
|
2013-12-09 22:55:22 +01:00
|
|
|
this->Release();
|
2013-11-29 09:23:08 +01:00
|
|
|
}
|
|
|
|
template<typename T> SmartPointer<T>& SmartPointer<T>::operator= (const SmartPointer<T>& p)
|
|
|
|
{
|
|
|
|
if (this != &p)
|
2013-11-26 21:08:34 +01:00
|
|
|
{
|
2013-11-29 09:23:08 +01:00
|
|
|
//Last to go?
|
|
|
|
if(this->_rc && this->_rc->Decref() == 0)
|
2013-11-26 21:08:34 +01:00
|
|
|
{
|
2013-11-29 09:23:08 +01:00
|
|
|
//Call child specific
|
2013-11-26 21:08:34 +01:00
|
|
|
Destroy();
|
|
|
|
}
|
2013-11-29 09:23:08 +01:00
|
|
|
|
|
|
|
this->_ptr = p._ptr;
|
|
|
|
this->_rc = p._rc;
|
2014-01-31 08:41:08 +01:00
|
|
|
if(this->_rc) this->_rc->Incref();
|
2013-11-26 21:08:34 +01:00
|
|
|
}
|
2013-11-29 09:23:08 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2013-12-06 11:46:47 +01:00
|
|
|
template<typename T> SmartPointer<T>& SmartPointer<T>::operator= (UniquePointer<T>& p)
|
|
|
|
{
|
|
|
|
//Last to go?
|
|
|
|
if(this->_rc)
|
|
|
|
{
|
|
|
|
if(this->_rc->Decref() == 0)
|
|
|
|
{
|
|
|
|
//Call child specific
|
|
|
|
Destroy();
|
|
|
|
this->_rc = new ReferenceCount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(p) this->_rc = new ReferenceCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this->_rc)
|
|
|
|
this->_rc->Incref();
|
|
|
|
|
|
|
|
this->_ptr = p.Release();
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
2013-11-29 09:23:08 +01:00
|
|
|
template<typename T> SmartPointer<T>& SmartPointer<T>::operator= (T* p)
|
|
|
|
{
|
|
|
|
if (this->_ptr != p)
|
2013-11-26 21:08:34 +01:00
|
|
|
{
|
2013-11-29 09:23:08 +01:00
|
|
|
//Last to go?
|
|
|
|
if(this->_rc)
|
2013-11-26 21:08:34 +01:00
|
|
|
{
|
2013-11-29 09:23:08 +01:00
|
|
|
if(this->_rc->Decref() == 0)
|
2013-11-26 21:08:34 +01:00
|
|
|
{
|
|
|
|
//Call child specific
|
|
|
|
Destroy();
|
2013-12-19 12:32:23 +01:00
|
|
|
if(p) this->_rc = new ReferenceCount();
|
2013-11-26 21:08:34 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-19 12:32:23 +01:00
|
|
|
else if(p)
|
|
|
|
{
|
2013-11-29 09:23:08 +01:00
|
|
|
this->_rc = new ReferenceCount();
|
2013-12-19 12:32:23 +01:00
|
|
|
}
|
|
|
|
|
2013-11-29 09:23:08 +01:00
|
|
|
this->_ptr = p;
|
2013-12-19 12:32:23 +01:00
|
|
|
|
2014-01-13 12:44:33 +01:00
|
|
|
if(p) this->_rc->Incref();
|
|
|
|
else this->_rc = 0;
|
2013-11-26 21:08:34 +01:00
|
|
|
}
|
2013-11-29 09:23:08 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2013-12-06 11:46:47 +01:00
|
|
|
template<typename T> inline bool SmartPointer<T>::operator== (const SmartPointer<T>& d) const
|
2013-11-29 09:23:08 +01:00
|
|
|
{
|
|
|
|
return d._ptr == this->_ptr;
|
|
|
|
}
|
2013-12-06 11:46:47 +01:00
|
|
|
template<typename T> inline bool SmartPointer<T>::operator== (const T& p) const
|
2013-11-29 09:23:08 +01:00
|
|
|
{
|
|
|
|
return &p == this->_ptr;
|
|
|
|
}
|
2013-12-06 11:46:47 +01:00
|
|
|
template<typename T> inline bool SmartPointer<T>::operator!= (const SmartPointer<T>& d) const
|
2013-12-02 14:43:57 +01:00
|
|
|
{
|
|
|
|
return d._ptr != this->_ptr;
|
|
|
|
}
|
2013-12-06 11:46:47 +01:00
|
|
|
template<typename T> inline bool SmartPointer<T>::operator!= (const T& p) const
|
2013-12-02 14:43:57 +01:00
|
|
|
{
|
|
|
|
return &p != this->_ptr;
|
|
|
|
}
|
2013-11-29 09:23:08 +01:00
|
|
|
template<typename T> inline T& SmartPointer<T>::operator* ()
|
|
|
|
{
|
|
|
|
return *this->_ptr;
|
|
|
|
}
|
2013-12-06 11:46:47 +01:00
|
|
|
template<typename T> inline const T& SmartPointer<T>::operator* () const
|
|
|
|
{
|
|
|
|
return *this->_ptr;
|
|
|
|
}
|
2013-11-29 09:23:08 +01:00
|
|
|
template<typename T> inline T* SmartPointer<T>::operator-> ()
|
|
|
|
{
|
|
|
|
return this->_ptr;
|
|
|
|
}
|
2013-12-06 11:46:47 +01:00
|
|
|
template<typename T> inline const T* SmartPointer<T>::operator-> () const
|
2013-11-29 09:23:08 +01:00
|
|
|
{
|
|
|
|
return this->_ptr;
|
|
|
|
}
|
2013-12-06 11:46:47 +01:00
|
|
|
template<typename T> inline SmartPointer<T>::operator T* () const
|
|
|
|
{
|
|
|
|
return this->_ptr;
|
|
|
|
}
|
|
|
|
template<typename T> inline SmartPointer<T>::operator const T* () const
|
|
|
|
{
|
|
|
|
return this->_ptr;
|
|
|
|
}
|
|
|
|
template<typename T> inline SmartPointer<T>::operator T& () const
|
|
|
|
{
|
|
|
|
return *this->_ptr;
|
|
|
|
}
|
|
|
|
template<typename T> inline SmartPointer<T>::operator bool() const
|
2013-11-29 09:23:08 +01:00
|
|
|
{
|
|
|
|
return (this->_ptr != 0);
|
|
|
|
}
|
|
|
|
template<typename T> inline T* SmartPointer<T>::Get()
|
|
|
|
{
|
|
|
|
return this->_ptr;
|
|
|
|
}
|
2013-12-09 22:55:22 +01:00
|
|
|
template<typename T> inline T* SmartPointer<T>::Get() const
|
|
|
|
{
|
|
|
|
return this->_ptr;
|
|
|
|
}
|
|
|
|
template<typename T> int SmartPointer<T>::Release()
|
|
|
|
{
|
|
|
|
int returnVal = 0;
|
|
|
|
|
|
|
|
if(this->_rc && ((returnVal = this->_rc->Decref()) == 0))
|
|
|
|
{
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
return returnVal;
|
|
|
|
}
|
2013-12-19 12:32:23 +01:00
|
|
|
template<typename T> int SmartPointer<T>::ReleaseDummy()
|
|
|
|
{
|
|
|
|
int val = this->_rc->Decref();
|
|
|
|
this->_rc->Incref();
|
|
|
|
return val;
|
|
|
|
}
|
2013-12-06 11:46:47 +01:00
|
|
|
template<typename T> inline bool SmartPointer<T>::IsValid() const
|
2013-11-29 09:23:08 +01:00
|
|
|
{
|
|
|
|
return (this->_ptr != NULL) ? true : false;
|
2013-11-26 21:08:34 +01:00
|
|
|
}
|
2013-11-29 09:23:08 +01:00
|
|
|
#pragma endregion
|
|
|
|
|
2013-11-13 12:07:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|