///////////////////////////////////////////////////////////////////// // Inline and template implementations for // the Utility Collection of Miscellanious Handy Functions // © Dan Andersson 2013 ///////////////////////////////////////////////////////////////////// #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( const UniquePointer &donor ) { this->ownedInstance = donor.ownedInstance; donor.ownedInstance = NULL; } 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 bool UniquePointer::operator == ( Type *stray ) const { return this->ownedInstance == stray; } template bool UniquePointer::operator != ( Type *stray ) const { return this->ownedInstance != stray; } 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( const UniqueArray &donor ) { this->ownedArray = donor.ownedArray; donor.ownedArray = NULL; } 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; } template bool UniqueArray::operator == ( Type *stray ) const { return this->ownedArray == stray; } template bool UniqueArray::operator != ( Type *stray ) const { return this->ownedArray != stray; } template Type* UniqueArray::Release() { Type *copy = this->ownedArray; this->ownedArray = NULL; return copy; } template inline bool UniqueArray::HaveOwnership() const { return this->operator bool(); } } } #endif