UniquePointer and UniqueArray
UniquePointer - removed hazardous members. UniqueArray - added All Documented
This commit is contained in:
parent
55879138c4
commit
ac1fb8bab9
|
@ -142,6 +142,7 @@
|
||||||
<ClCompile Include="WinTimer.cpp" />
|
<ClCompile Include="WinTimer.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="Utilities-InlineImpl.h" />
|
||||||
<ClInclude Include="Utilities.h" />
|
<ClInclude Include="Utilities.h" />
|
||||||
<ClInclude Include="WinTimer.h" />
|
<ClInclude Include="WinTimer.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
|
@ -29,5 +29,8 @@
|
||||||
<ClInclude Include="WinTimer.h">
|
<ClInclude Include="WinTimer.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Utilities-InlineImpl.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
|
@ -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<typename Type>
|
||||||
|
inline void SafeDeleteInstance( Type *dynamicInstance )
|
||||||
|
{
|
||||||
|
if( dynamicInstance )
|
||||||
|
{
|
||||||
|
delete dynamicInstance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
void SafeDeleteArray( Type dynamicArray[] )
|
||||||
|
{
|
||||||
|
if( dynamicArray )
|
||||||
|
{
|
||||||
|
delete [] dynamicArray;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
UniquePointer<Type>::UniquePointer( Type *assignedInstance )
|
||||||
|
{
|
||||||
|
this->ownedInstance = assignedInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
UniquePointer<Type>::~UniquePointer()
|
||||||
|
{
|
||||||
|
SafeDeleteInstance( this->ownedInstance );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
UniquePointer<Type> & UniquePointer<Type>::operator = ( Type *assignedInstance )
|
||||||
|
{
|
||||||
|
SafeDeleteInstance( this->ownedInstance );
|
||||||
|
this->ownedInstance = assignedInstance;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
UniqueArray<Type>::~UniqueArray()
|
||||||
|
{
|
||||||
|
SafeDeleteArray( this->ownedArray );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
UniqueArray<Type> & UniqueArray<Type>::operator = ( Type assignedArray[] )
|
||||||
|
{
|
||||||
|
SafeDeleteArray( this->ownedArray );
|
||||||
|
this->ownedArray = assignedArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
UniqueArray<Type> & UniqueArray<Type>::operator = ( const UniquePointer<Type> &donor )
|
||||||
|
{
|
||||||
|
SafeDeleteArray( this->ownedArray );
|
||||||
|
this->ownedArray = donor.ownedInstance;
|
||||||
|
donor.owned = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Type> template<typename Index>
|
||||||
|
template<typename Index> Type & UniqueArray<Type>::operator [] ( Index i )
|
||||||
|
{
|
||||||
|
return this->ownedArray[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Type> template<typename Index>
|
||||||
|
template<typename Index> const Type & UniqueArray<Type>::operator [] ( Index i ) const
|
||||||
|
{
|
||||||
|
return this->ownedArray[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
UniqueArray<Type>::operator bool () const
|
||||||
|
{
|
||||||
|
return this->ownedArray != NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
132
Misc/Utilities.h
132
Misc/Utilities.h
|
@ -3,7 +3,6 @@
|
||||||
// © Dan Andersson 2013
|
// © Dan Andersson 2013
|
||||||
/////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#ifndef UTILITIES_H
|
#ifndef UTILITIES_H
|
||||||
#define UTILITIES_H
|
#define UTILITIES_H
|
||||||
|
|
||||||
|
@ -15,100 +14,97 @@
|
||||||
|
|
||||||
namespace Utility
|
namespace Utility
|
||||||
{
|
{
|
||||||
namespace Memory
|
namespace DynamicMemory
|
||||||
{
|
{
|
||||||
|
/// If dynamicInstance is not NULL, then delete
|
||||||
|
template<typename Type> void SafeDeleteInstance( Type *dynamicInstance );
|
||||||
|
|
||||||
|
/// If dynamicArray is not NULL, then delete []
|
||||||
|
template<typename Type> void SafeDeleteArray( Type dynamicArray[] );
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
struct UniquePointer
|
struct UniquePointer
|
||||||
{
|
{ /// Wrapper to safely transfer dynamic ownership/responsibility
|
||||||
public:
|
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();
|
||||||
|
|
||||||
UniquePointer<Type> & 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<Type> & 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<Type> & operator = ( const UniquePointer<Type> &donor );
|
UniquePointer<Type> & operator = ( const UniquePointer<Type> &donor );
|
||||||
|
|
||||||
|
/// Access the assigned dynamic instance. Will crash if nothing there
|
||||||
operator Type* ();
|
operator Type* ();
|
||||||
operator const Type* () const;
|
|
||||||
Type * operator -> ();
|
|
||||||
const Type * operator -> () const;
|
|
||||||
template<typename Index> Type & operator [] ( Index i );
|
|
||||||
template<typename Index> 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;
|
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();
|
Type* Release();
|
||||||
bool haveOwnership() const;
|
|
||||||
|
/// (inline) If true, this UniquePointer have a current ownership/responsibility of a dynamic instance.
|
||||||
|
bool HaveOwnership() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable Type *ownedMemory;
|
mutable Type *ownedInstance;
|
||||||
};
|
};
|
||||||
|
|
||||||
// IMPLEMENTATIONS ////////////////////////////////////////////////
|
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
UniquePointer<Type>::UniquePointer( Type *assignedMemory )
|
struct UniqueArray
|
||||||
{ this->ownedMemory = assignedMemory; }
|
{ /// 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 );
|
||||||
|
|
||||||
template<typename Type>
|
/// Will auto delete assigned dynamic array.
|
||||||
UniquePointer<Type>::~UniquePointer()
|
~UniqueArray();
|
||||||
{ if( this->ownedMemory ) delete this->ownedMemory; }
|
|
||||||
|
|
||||||
template<typename Type>
|
/// Assigns assignedInstance ownership to this UniquePonter, old owned array will be deleted.
|
||||||
UniquePointer<Type> & UniquePointer<Type>::operator = ( Type *assignedMemory )
|
/// If NULL is assigned is equavalent with clearing all responsibilities from this UniqueArray.
|
||||||
{
|
UniqueArray<Type> & operator = ( Type assignedArray[] );
|
||||||
if( this->ownedPointer ) delete this->ownedMemory;
|
|
||||||
this->ownedMemory = assignedMemory;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Type>
|
/// Transfers assignedInstance ownership from donor to this UniquePonter, old owned array will be deleted.
|
||||||
UniquePointer<Type> & UniquePointer<Type>::operator = ( const UniquePointer<Type> &donor )
|
/// If donor had nothing, is equavalent with clearing all responsibilities from this UniqueArray.
|
||||||
{
|
UniqueArray<Type> & operator = ( const UniqueArray<Type> &donor );
|
||||||
if( this->ownedMemory ) delete this->ownedMemory;
|
|
||||||
this->ownedMemory = donor.ownedMemory;
|
|
||||||
donor.ownedMemory = NULL;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Type>
|
/// Accesses the instance at index i of this UniqeArray's owned dynamic array.
|
||||||
UniquePointer<Type>::operator Type* ()
|
/// Will crash if out-of-bound or there is no assigned array.
|
||||||
{ return this->ownedMemory; }
|
template<typename Index> Type & operator [] ( Index i );
|
||||||
|
|
||||||
template<typename Type>
|
/// Accesses the instance at index i of this UniqeArray's owned dynamic array.
|
||||||
UniquePointer<Type>::operator const Type* () const
|
/// Will crash if out-of-bound or there is no assigned array.
|
||||||
{ return this->ownedMemory; }
|
template<typename Index> const Type & operator [] ( Index i ) const;
|
||||||
|
|
||||||
template<typename Type>
|
/// If true, this UniqueArray have a current ownership/responsibility of a dynamic instance.
|
||||||
Type * UniquePointer<Type>::operator -> ()
|
operator bool () const;
|
||||||
{ return this->ownedMemory; }
|
|
||||||
|
|
||||||
template<typename Type>
|
/// This UniqueArray drops all claims of ownership/responsibility and returns the dynamic array. Now it is your responsibility to delete.
|
||||||
const Type * UniquePointer<Type>::operator -> () const
|
Type* Release();
|
||||||
{ return this->ownedMemory; }
|
|
||||||
|
|
||||||
template<typename Type> template<typename Index>
|
/// (inline) If true, this UniqueArray have a current ownership/responsibility of a dynamic array.
|
||||||
Type & UniquePointer<Type>::operator [] ( Index i )
|
bool HaveOwnership() const;
|
||||||
{ return this->ownedMemory[i]; }
|
|
||||||
|
|
||||||
template<typename Type> template<typename Index>
|
private:
|
||||||
const Type & UniquePointer<Type>::operator [] ( Index i ) const
|
mutable Type *ownedArray;
|
||||||
{ return this->ownedMemory[i]; }
|
};
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
UniquePointer<Type>::operator bool() const
|
|
||||||
{ return this->ownedMemory != NULL; }
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
Type* UniquePointer<Type>::Release()
|
|
||||||
{
|
|
||||||
Type *copy = this->ownedMemory;
|
|
||||||
this->ownedMemory = NULL;
|
|
||||||
return copy;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
inline bool UniquePointer<Type>::haveOwnership() const
|
|
||||||
{ return this->operator bool(); }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace String
|
namespace String
|
||||||
|
|
Loading…
Reference in New Issue