From ac1fb8bab92c75199c5abc248aaedf4e6fcddb3e Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Wed, 13 Nov 2013 12:07:21 +0100 Subject: [PATCH] UniquePointer and UniqueArray UniquePointer - removed hazardous members. UniqueArray - added All Documented --- Misc/Misc.vcxproj | 1 + Misc/Misc.vcxproj.filters | 3 + Misc/Utilities-InlineImpl.h | 168 ++++++++++++++++++++++++++++++++++++ Misc/Utilities.h | 140 +++++++++++++++--------------- 4 files changed, 240 insertions(+), 72 deletions(-) create mode 100644 Misc/Utilities-InlineImpl.h diff --git a/Misc/Misc.vcxproj b/Misc/Misc.vcxproj index 76714140..58c71f07 100644 --- a/Misc/Misc.vcxproj +++ b/Misc/Misc.vcxproj @@ -142,6 +142,7 @@ + diff --git a/Misc/Misc.vcxproj.filters b/Misc/Misc.vcxproj.filters index 9f5237ac..6f633e1d 100644 --- a/Misc/Misc.vcxproj.filters +++ b/Misc/Misc.vcxproj.filters @@ -29,5 +29,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/Misc/Utilities-InlineImpl.h b/Misc/Utilities-InlineImpl.h new file mode 100644 index 00000000..93b780ec --- /dev/null +++ b/Misc/Utilities-InlineImpl.h @@ -0,0 +1,168 @@ +///////////////////////////////////////////////////////////////////// +// 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() + { + 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 UniquePointer &donor ) + { + SafeDeleteArray( this->ownedArray ); + this->ownedArray = donor.ownedInstance; + donor.owned = NULL; + } + + template template + template Type & UniqueArray::operator [] ( Index i ) + { + return this->ownedArray[i]; + } + + template template + template const Type & UniqueArray::operator [] ( Index i ) const + { + return this->ownedArray[i]; + } + + template + UniqueArray::operator bool () const + { + return this->ownedArray != NULL; + } + + template + Type* UniqueArray::Release() + { + Type *copy = this->ownedArray; + this->ownedArray = NULL; + return copy; + } + + template + inline bool UniqueArray::HaveOwnership() const + { + return this->operator bool(); + } + } +} + +#endif \ No newline at end of file diff --git a/Misc/Utilities.h b/Misc/Utilities.h index ba6e0c3a..91285cde 100644 --- a/Misc/Utilities.h +++ b/Misc/Utilities.h @@ -3,7 +3,6 @@ // © Dan Andersson 2013 ///////////////////////////////////////////////////////////////////// -#pragma once #ifndef UTILITIES_H #define UTILITIES_H @@ -15,100 +14,97 @@ namespace Utility { - namespace Memory + namespace DynamicMemory { + /// If dynamicInstance is not NULL, then delete + template void SafeDeleteInstance( Type *dynamicInstance ); + + /// If dynamicArray is not NULL, then delete [] + template void SafeDeleteArray( Type dynamicArray[] ); + template struct UniquePointer - { + { /// Wrapper to safely transfer dynamic ownership/responsibility public: - UniquePointer( Type *assignedMemory = NULL ); + /// Assigns assignedInstance ownership to this UniquePonter, old owned instance will be deleted. + /// If NULL is assigned is equavalent with clearing all responsibilities from this UniquePointer. + UniquePointer( Type *assignedInstance = NULL ); + + /// Will auto delete assigned dynamic instance. ~UniquePointer(); - UniquePointer & operator = ( Type *assignedMemory ); + /// Assigns assignedInstance ownership to this UniquePonter, old owned instance will be deleted. + /// If NULL is assigned is equavalent with clearing all responsibilities from this UniquePointer. + UniquePointer & operator = ( Type *assignedInstance ); + + /// Transfers assignedInstance ownership from donor to this UniquePonter, old owned instance will be deleted. + /// If donor had nothing, is equavalent with clearing all responsibilities from this UniquePointer. UniquePointer & operator = ( const UniquePointer &donor ); + /// Access the assigned dynamic instance. Will crash if nothing there operator Type* (); - operator const Type* () const; - Type * operator -> (); - const Type * operator -> () const; - template Type & operator [] ( Index i ); - template const Type & operator [] ( Index i ) const; + /// Access the assigned dynamic instance. Will crash if nothing there + operator const Type* () const; + + /// Access members of the assigned dynamic instance. Will crash if nothing there + Type * operator -> (); + + /// Access members of the assigned dynamic instance. Will crash if nothing there + const Type * operator -> () const; + + /// If true, this UniquePointer have a current ownership/responsibility of a dynamic instance. operator bool () const; + /// This UniquePointer drops all claims of ownership/responsibility and returns the dynamic instance. Now it is your responsibility to delete. Type* Release(); - bool haveOwnership() const; - + + /// (inline) If true, this UniquePointer have a current ownership/responsibility of a dynamic instance. + bool HaveOwnership() const; + private: - mutable Type *ownedMemory; + mutable Type *ownedInstance; }; - // IMPLEMENTATIONS //////////////////////////////////////////////// - template - UniquePointer::UniquePointer( Type *assignedMemory ) - { this->ownedMemory = assignedMemory; } + struct UniqueArray + { /// Wrapper to safely transfer dynamic ownership/responsibility + public: + /// Assigns assignedInstance ownership to this UniquePonter, old owned array will be deleted. + /// If NULL is assigned is equavalent with clearing all responsibilities from this UniqueArray. + UniqueArray( Type assignedArray[] = NULL ); + + /// Will auto delete assigned dynamic array. + ~UniqueArray(); - template - UniquePointer::~UniquePointer() - { if( this->ownedMemory ) delete this->ownedMemory; } + /// Assigns assignedInstance ownership to this UniquePonter, old owned array will be deleted. + /// If NULL is assigned is equavalent with clearing all responsibilities from this UniqueArray. + UniqueArray & operator = ( Type assignedArray[] ); + + /// Transfers assignedInstance ownership from donor to this UniquePonter, old owned array will be deleted. + /// If donor had nothing, is equavalent with clearing all responsibilities from this UniqueArray. + UniqueArray & operator = ( const UniqueArray &donor ); - template - UniquePointer & UniquePointer::operator = ( Type *assignedMemory ) - { - if( this->ownedPointer ) delete this->ownedMemory; - this->ownedMemory = assignedMemory; - return *this; - } + /// Accesses the instance at index i of this UniqeArray's owned dynamic array. + /// Will crash if out-of-bound or there is no assigned array. + template Type & operator [] ( Index i ); + + /// Accesses the instance at index i of this UniqeArray's owned dynamic array. + /// Will crash if out-of-bound or there is no assigned array. + template const Type & operator [] ( Index i ) const; - template - UniquePointer & UniquePointer::operator = ( const UniquePointer &donor ) - { - if( this->ownedMemory ) delete this->ownedMemory; - this->ownedMemory = donor.ownedMemory; - donor.ownedMemory = NULL; - return *this; - } + /// If true, this UniqueArray have a current ownership/responsibility of a dynamic instance. + operator bool () const; - template - UniquePointer::operator Type* () - { return this->ownedMemory; } + /// This UniqueArray drops all claims of ownership/responsibility and returns the dynamic array. Now it is your responsibility to delete. + Type* Release(); - template - UniquePointer::operator const Type* () const - { return this->ownedMemory; } + /// (inline) If true, this UniqueArray have a current ownership/responsibility of a dynamic array. + bool HaveOwnership() const; - template - Type * UniquePointer::operator -> () - { return this->ownedMemory; } - - template - const Type * UniquePointer::operator -> () const - { return this->ownedMemory; } - - template template - Type & UniquePointer::operator [] ( Index i ) - { return this->ownedMemory[i]; } - - template template - const Type & UniquePointer::operator [] ( Index i ) const - { return this->ownedMemory[i]; } - - template - UniquePointer::operator bool() const - { return this->ownedMemory != NULL; } - - template - Type* UniquePointer::Release() - { - Type *copy = this->ownedMemory; - this->ownedMemory = NULL; - return copy; - } - - template - inline bool UniquePointer::haveOwnership() const - { return this->operator bool(); } + private: + mutable Type *ownedArray; + }; } namespace String