///////////////////////////////////////////////////////////////////// // Inline and template implementations for // the Utility Collection of Miscellanious Handy Functions // © Dan Andersson 2013 // © Dennis Andersen 2013 TODO: Is this correct? ///////////////////////////////////////////////////////////////////// #ifndef UTILITIES_INLINE_IMPL_H #define UTILITIES_INLINE_IMPL_H #include "Utilities.h" namespace Utility { namespace DynamicMemory { template inline void SafeDeleteInstance( Type *dynamicInstance ) { if( dynamicInstance ) { delete dynamicInstance; } } template void SafeDeleteArray( Type dynamicArray[] ) { if( dynamicArray ) { delete [] dynamicArray; } } template UniquePointer::UniquePointer( Type *assignedInstance ) { this->ownedInstance = assignedInstance; } template UniquePointer::~UniquePointer() { SafeDeleteInstance( this->ownedInstance ); } template UniquePointer & UniquePointer::operator = ( Type *assignedInstance ) { SafeDeleteInstance( this->ownedInstance ); this->ownedInstance = assignedInstance; return *this; } template UniquePointer & UniquePointer::operator = ( const UniquePointer &donor ) { SafeDeleteInstance( this->ownedInstance ); this->ownedInstance = donor.ownedInstance; donor.ownedInstance = NULL; return *this; } template UniquePointer::operator Type* () { return this->ownedInstance; } template UniquePointer::operator const Type* () const { return this->ownedInstance; } template Type * UniquePointer::operator -> () { return this->ownedInstance; } template const Type * UniquePointer::operator -> () const { return this->ownedInstance; } template UniquePointer::operator bool() const { return this->ownedInstance != NULL; } template Type* UniquePointer::Release() { Type *copy = this->ownedInstance; this->ownedInstance = NULL; return copy; } template inline bool UniquePointer::HaveOwnership() const { return this->operator bool(); } template UniqueArray::UniqueArray( Type assignedArray[] ) { this->ownedArray = assignedArray; } template UniqueArray::~UniqueArray() { SafeDeleteArray( this->ownedArray ); } template UniqueArray & UniqueArray::operator = ( Type assignedArray[] ) { SafeDeleteArray( this->ownedArray ); this->ownedArray = assignedArray; } template UniqueArray & UniqueArray::operator = ( const UniqueArray &donor ) { SafeDeleteArray( this->ownedArray ); this->ownedArray = donor.ownedInstance; donor.owned = NULL; } template template Type & UniqueArray::operator [] ( Index i ) { return this->ownedArray[i]; } template template const Type & UniqueArray::operator [] ( Index i ) const { return this->ownedArray[i]; } template UniqueArray::operator bool () const { return this->ownedArray != NULL; } templateType* UniqueArray::Release() { Type *copy = this->ownedArray; this->ownedArray = NULL; return copy; } template inline bool UniqueArray::HaveOwnership() const { return this->operator bool(); } namespace SmartPointer { template void StdSmartPointer::Destroy() { delete this->_rc; this->_rc = NULL; delete this->_ptr; this->_ptr = NULL; } template StdSmartPointer::StdSmartPointer() :_rc(0), _ptr(0) { } template StdSmartPointer::StdSmartPointer(T* p) :_ptr(p) { this->_rc = new ReferenceCount(); this->_rc->Incref(); } template StdSmartPointer::StdSmartPointer(const StdSmartPointer& d) :_ptr(d._ptr), _rc(d._rc) { if(this->_rc) this->_rc->Incref(); } template StdSmartPointer::~StdSmartPointer() { if (this->_rc && this->_rc->Decref() == 0) { Destroy(); } } template StdSmartPointer& StdSmartPointer::operator= (const StdSmartPointer& p) { if (this != &p) { //Last to go? if(this->_rc && this->_rc->Release() == 0) { //Call child specific Destroy(); } this->_ptr = p._ptr; this->_rc = p._rc; this->_rc->Add(); } return *this; } template StdSmartPointer& StdSmartPointer::operator= (T* p) { if (this->_ptr != p) { //Last to go? if(this->_rc) { if(this->_rc->Decref() == 0) { //Call child specific Destroy(); this->_rc = new ReferenceCount(); } } else this->_rc = new ReferenceCount(); this->_ptr = p; this->_rc->Incref(); } return *this; } template inline bool StdSmartPointer::operator== (const StdSmartPointer& d) { return d._ptr == this->_ptr; } template inline bool StdSmartPointer::operator== (const T& p) { return &p == this->_ptr; } template inline T& StdSmartPointer::operator* () { return *this->_ptr; } template inline T* StdSmartPointer::operator-> () { return this->_ptr; } template inline StdSmartPointer::operator T* () { return this->_ptr; } /** * Returns the connected pointer */ template inline T* StdSmartPointer::Get() { return this->_ptr; } /** Checks if the pointer is valid (not NULL) Returns true for valid, else false. */ template inline bool StdSmartPointer::IsValid() { return (this->_ptr != NULL) ? true : false; } } } } #endif