Misc updated and fixed

Follows coding practice doc now.
missing documentation
Added feature: Utility::Memory::UniquePointer
This commit is contained in:
Dander7BD 2013-11-09 23:36:38 +01:00
parent 0ad4bca1b4
commit 44634d67b1
3 changed files with 301 additions and 45 deletions

View File

@ -24,13 +24,13 @@
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset> <PlatformToolset>v110</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet> <CharacterSet>MultiByte</CharacterSet>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>StaticLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset> <PlatformToolset>v110</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet> <CharacterSet>MultiByte</CharacterSet>

View File

@ -5,23 +5,25 @@
#include "Utilities.h" #include "Utilities.h"
#include <sstream> #include <sstream>
#include <locale>
using ::std::vector; using ::std::vector;
using ::std::string; using ::std::string;
using ::std::wstring;
namespace Utility namespace Utility
{ {
// PRIVATE STATIC //////////////////////////////////////////////////// // PRIVATE STATIC ////////////////////////////////////////////////////
namespace PrivateStatic namespace PrivateStatic
{ {
const ::std::locale systemDefaultLocale = ::std::locale(); const ::std::locale system_default_locale = ::std::locale();
} }
// STRING //////////////////////////////////////////////////////////// // STRING ////////////////////////////////////////////////////////////
namespace String namespace String
{ {
vector<string> & split( vector<string> &output, const string &str, char delim, string::size_type offset ) // string
vector<string> & Split( vector<string> &output, const string &str, char delim, string::size_type offset )
{ {
if( str.length() > 0 ) if( str.length() > 0 )
{ {
@ -42,13 +44,13 @@ namespace Utility
{ {
if( delimPos > offset ) if( delimPos > offset )
output.push_back( str.substr( offset, delimPos - offset ) ); output.push_back( str.substr( offset, delimPos - offset ) );
String::split( output, str, delim, delimPos + 1 ); String::Split( output, str, delim, delimPos + 1 );
} }
} }
return output; return output;
} }
vector<string> & split( vector<string> &output, const string &str, const string &delim, string::size_type offset ) vector<string> & Split( vector<string> &output, const string &str, const string &delim, string::size_type offset )
{ {
if( str.length() > 0 ) if( str.length() > 0 )
{ {
@ -62,13 +64,13 @@ namespace Utility
{ {
if( delimPos > offset ) if( delimPos > offset )
output.push_back( str.substr( offset, delimPos - offset ) ); output.push_back( str.substr( offset, delimPos - offset ) );
String::split( output, str, delim, delimPos + delim.length() ); String::Split( output, str, delim, delimPos + delim.length() );
} }
} }
return output; return output;
} }
vector<string> & split( vector<string> &output, const string &str, const vector<string> &delim, string::size_type offset ) vector<string> & Split( vector<string> &output, const string &str, const vector<string> &delim, string::size_type offset )
{ {
if( str.length() > 0 ) if( str.length() > 0 )
{ {
@ -94,13 +96,13 @@ namespace Utility
{ {
if( firstDelimPos > offset ) if( firstDelimPos > offset )
output.push_back( str.substr( offset, firstDelimPos - offset ) ); output.push_back( str.substr( offset, firstDelimPos - offset ) );
String::split( output, str, delim, firstDelimPos + delim[delimRef].length() ); String::Split( output, str, delim, firstDelimPos + delim[delimRef].length() );
} }
} }
return output; return output;
} }
string trim( const string &str ) string Trim( const string &str )
{ {
string::size_type first = 0, string::size_type first = 0,
last = str.length(); last = str.length();
@ -128,7 +130,7 @@ namespace Utility
return str.substr( first, (++last) - first ); return str.substr( first, (++last) - first );
} }
string & toLowerCase( string &output, const string &str ) string & ToLowerCase( string &output, const string &str )
{ {
int length = (int)str.length(); int length = (int)str.length();
output.resize( length ); output.resize( length );
@ -137,7 +139,7 @@ namespace Utility
return output; return output;
} }
string & toLowerCase( string &str ) string & ToLowerCase( string &str )
{ {
int length = (int)str.length(); int length = (int)str.length();
for( int i = 0; i < length; ++i ) for( int i = 0; i < length; ++i )
@ -145,7 +147,7 @@ namespace Utility
return str; return str;
} }
string & toUpperCase( string &output, const string &str ) string & ToUpperCase( string &output, const string &str )
{ {
int length = (int)str.length(); int length = (int)str.length();
output.resize( length ); output.resize( length );
@ -154,7 +156,7 @@ namespace Utility
return output; return output;
} }
string & toUpperCase( string &str ) string & ToUpperCase( string &str )
{ {
int length = (int)str.length(); int length = (int)str.length();
for( int i = 0; i < length; ++i ) for( int i = 0; i < length; ++i )
@ -162,14 +164,14 @@ namespace Utility
return str; return str;
} }
string & extractDirPath( string &output, const string &file, char dirDelimeter ) string & ExtractDirPath( string &output, const string &file, char dirDelimeter )
{ {
string d = " "; string d = " ";
d[0] = dirDelimeter; d[0] = dirDelimeter;
return String::extractDirPath( output, file, d ); return String::ExtractDirPath( output, file, d );
} }
string & extractDirPath( string &output, const string &file, const string &dirDelimeter ) string & ExtractDirPath( string &output, const string &file, const string &dirDelimeter )
{ {
string::size_type end = file.find_last_of( dirDelimeter ); string::size_type end = file.find_last_of( dirDelimeter );
if( end == string::npos ) if( end == string::npos )
@ -185,7 +187,7 @@ namespace Utility
return output; return output;
} }
string & replaceCharacters( string &str, char characterToReplace, char newCharacter, const string::size_type &offset, const string::size_type &end ) string & ReplaceCharacters( string &str, char characterToReplace, char newCharacter, const string::size_type &offset, const string::size_type &end )
{ {
string::size_type i = offset; string::size_type i = offset;
while( true ) while( true )
@ -197,12 +199,94 @@ namespace Utility
} }
return str; return str;
} }
// wstring
vector<wstring> & Split( vector<wstring> &output, const wstring &str, char delim, wstring::size_type offset )
{
if( str.length() > 0 )
{
while( offset < str.length() ) // trim
{
if( str[offset] == delim )
++offset;
else break;
}
wstring::size_type delimPos = str.find_first_of( delim, offset );
if( delimPos == wstring::npos )
{
if( str.length() > offset )
output.push_back( str.substr( offset, str.length() - offset ) );
}
else
{
if( delimPos > offset )
output.push_back( str.substr( offset, delimPos - offset ) );
String::Split( output, str, delim, delimPos + 1 );
}
}
return output;
}
vector<wstring> & Split( vector<wstring> &output, const wstring &str, const wstring &delim, wstring::size_type offset )
{
if( str.length() > 0 )
{
wstring::size_type delimPos = str.find_first_of( delim, offset );
if( delimPos == wstring::npos )
{
if( str.length() > offset )
output.push_back( str.substr( offset, str.length() - offset ) );
}
else
{
if( delimPos > offset )
output.push_back( str.substr( offset, delimPos - offset ) );
String::Split( output, str, delim, delimPos + delim.length() );
}
}
return output;
}
vector<wstring> & Split( vector<wstring> &output, const wstring &str, const vector<wstring> &delim, wstring::size_type offset )
{
if( str.length() > 0 )
{
wstring::size_type firstDelimPos = str.length(), delimPos;
vector<wstring>::size_type numDelims = delim.size(), delimRef = 0;
for( vector<wstring>::size_type i = 0; i < numDelims ; ++i )
{
delimPos = str.find_first_of( delim[i], offset );
if( delimPos != wstring::npos ) if( delimPos < firstDelimPos )
{
delimRef = i;
firstDelimPos = delimPos;
}
}
if( firstDelimPos == str.length() )
{
if( str.length() > offset )
output.push_back( str.substr( offset, str.length() - offset ) );
}
else
{
if( firstDelimPos > offset )
output.push_back( str.substr( offset, firstDelimPos - offset ) );
String::Split( output, str, delim, firstDelimPos + delim[delimRef].length() );
}
}
return output;
}
} }
// STREAM //////////////////////////////////////////////////////////// // STREAM ////////////////////////////////////////////////////////////
namespace Stream namespace Stream
{ {
float* readFloats( float *output, ::std::istream &input, unsigned int numFloats ) float* ReadFloats( float *output, ::std::istream &input, unsigned int numFloats )
{ {
string str; string str;
for( unsigned int i = 0; i < numFloats; ++i ) for( unsigned int i = 0; i < numFloats; ++i )

View File

@ -11,69 +11,241 @@
#include <istream> #include <istream>
#include <vector> #include <vector>
#include <locale> #include <locale>
#include <limits>
namespace Utility namespace Utility
{ {
namespace Memory
{
template<typename Type>
struct UniquePointer
{
public:
UniquePointer( Type *assignedMemory = NULL );
~UniquePointer();
UniquePointer<Type> & operator = ( Type *assignedMemory );
UniquePointer<Type> & operator = ( const UniquePointer<Type> &donor );
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;
operator bool () const;
Type* Release();
bool haveOwnership() const;
private:
mutable Type *ownedMemory;
};
// IMPLEMENTATIONS ////////////////////////////////////////////////
template<typename Type>
UniquePointer<Type>::UniquePointer( Type *assignedMemory )
{ this->ownedMemory = assignedMemory; }
template<typename Type>
UniquePointer<Type>::~UniquePointer()
{ if( this->ownedMemory ) delete this->ownedMemory; }
template<typename Type>
UniquePointer<Type> & UniquePointer<Type>::operator = ( Type *assignedMemory )
{
if( this->ownedPointer ) delete this->ownedMemory;
this->ownedMemory = assignedMemory;
return *this;
}
template<typename Type>
UniquePointer<Type> & UniquePointer<Type>::operator = ( const UniquePointer<Type> &donor )
{
if( this->ownedMemory ) delete this->ownedMemory;
this->ownedMemory = donor.ownedMemory;
donor.ownedMemory = NULL;
return *this;
}
template<typename Type>
UniquePointer<Type>::operator Type* ()
{ return this->ownedMemory; }
template<typename Type>
UniquePointer<Type>::operator const Type* () const
{ return this->ownedMemory; }
template<typename Type>
Type * UniquePointer<Type>::operator -> ()
{ return this->ownedMemory; }
template<typename Type>
const Type * UniquePointer<Type>::operator -> () const
{ return this->ownedMemory; }
template<typename Type> template<typename Index>
Type & UniquePointer<Type>::operator [] ( Index i )
{ return this->ownedMemory[i]; }
template<typename Type> template<typename Index>
const Type & UniquePointer<Type>::operator [] ( Index i ) const
{ 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
{ {
// note to self: add a whitespaceSplit method? // string
::std::vector<::std::string> & split( ::std::vector<::std::string> &output, const ::std::string &str, char delim, ::std::string::size_type offset = 0 );
::std::vector<::std::string> & split( ::std::vector<::std::string> &output, const ::std::string &str, const ::std::string &delim, ::std::string::size_type offset = 0 ); ::std::vector<::std::string> & Split( ::std::vector<::std::string> &output, const ::std::string &str, char delim, ::std::string::size_type offset = 0 );
::std::vector<::std::string> & split( ::std::vector<::std::string> &output, const ::std::string &str, const ::std::vector<::std::string> &delim, ::std::string::size_type offset = 0 ); ::std::vector<::std::string> & Split( ::std::vector<::std::string> &output, const ::std::string &str, const ::std::string &delim, ::std::string::size_type offset = 0 );
::std::string trim( const ::std::string &str ); ::std::vector<::std::string> & Split( ::std::vector<::std::string> &output, const ::std::string &str, const ::std::vector<::std::string> &delim, ::std::string::size_type offset = 0 );
::std::string & toLowerCase( ::std::string &output, const ::std::string &str ); ::std::string Trim( const ::std::string &str );
::std::string & toLowerCase( ::std::string &str ); ::std::string & ToLowerCase( ::std::string &output, const ::std::string &str );
::std::string & toUpperCase( ::std::string &output, const ::std::string &str ); ::std::string & ToLowerCase( ::std::string &str );
::std::string & toUpperCase( ::std::string &str ); ::std::string & ToUpperCase( ::std::string &output, const ::std::string &str );
::std::string & extractDirPath( ::std::string &output, const ::std::string &file, char dirDelimeter ); ::std::string & ToUpperCase( ::std::string &str );
::std::string & extractDirPath( ::std::string &output, const ::std::string &file, const ::std::string &dirDelimeter ); ::std::string & ExtractDirPath( ::std::string &output, const ::std::string &file, char dirDelimeter );
::std::string & replaceCharacters( ::std::string &str, char characterToReplace, char newCharacter, const ::std::string::size_type &offset = 0, const ::std::string::size_type &end = ::std::string::npos ); ::std::string & ExtractDirPath( ::std::string &output, const ::std::string &file, const ::std::string &dirDelimeter );
::std::string & ReplaceCharacters( ::std::string &str, char characterToReplace, char newCharacter, const ::std::string::size_type &offset = 0, const ::std::string::size_type &end = ::std::string::npos );
// wstring
::std::vector<::std::wstring> & Split( ::std::vector<::std::wstring> &output, const ::std::wstring &str, char delim, ::std::wstring::size_type offset = 0 );
::std::vector<::std::wstring> & Split( ::std::vector<::std::wstring> &output, const ::std::wstring &str, const ::std::wstring &delim, ::std::wstring::size_type offset = 0 );
::std::vector<::std::wstring> & Split( ::std::vector<::std::wstring> &output, const ::std::wstring &str, const ::std::vector<::std::wstring> &delim, ::std::wstring::size_type offset = 0 );
} }
namespace Stream namespace Stream
{ {
float* readFloats( float *output, ::std::istream &input, unsigned int numFloats ); float* ReadFloats( float *output, ::std::istream &input, unsigned int numFloats );
} }
namespace StaticArray namespace StaticArray
{ {
template<typename ElementType, unsigned int num> template<typename ScalarType, unsigned int num>
inline unsigned int numElementsOf( const ElementType(&)[num] ) inline unsigned int NumElementsOf( const ScalarType(&)[num] )
{ return num; } { return num; }
template<typename ScalarType, unsigned int num>
inline ScalarType & FirstElementOf( ScalarType (&arr)[num] )
{ return arr[0]; }
template<typename ScalarType, unsigned int num>
inline ScalarType & LastElementOf( ScalarType (&arr)[num] )
{ return arr[num-1]; }
} }
namespace Element namespace Element
{ {
template<typename ElementType> template<typename ScalarType>
inline void swap( ElementType &elementA, ElementType &elementB, ElementType &swapSpace ) inline void Swap( ScalarType &elementA, ScalarType &elementB, ScalarType &swapSpace )
{ swapSpace = elementA; elementA = elementB; elementB = swapSpace; } { swapSpace = elementA; elementA = elementB; elementB = swapSpace; }
template<typename ElementType> template<typename ScalarType>
inline void swap( ElementType &elementA, ElementType &elementB ) inline void Swap( ScalarType &elementA, ScalarType &elementB )
{ ElementType swapSpace; swap( elementA, elementB, swapSpace ); } { ScalarType swapSpace; Swap( elementA, elementB, swapSpace ); }
} }
namespace Value namespace Value
{ {
using ::std::numeric_limits;
template<typename ValueType> template<typename ValueType>
inline ValueType abs( const ValueType &value ) inline ValueType Abs( const ValueType &value )
{ return value < 0 ? value * -1 : value; } { return value < 0 ? value * -1 : value; }
template<typename ValueType> template<typename ValueType>
inline ValueType max( const ValueType &valueA, const ValueType &valueB ) inline ValueType Max( const ValueType &valueA, const ValueType &valueB )
{ return valueA > valueB ? valueA : valueB; } { return valueA > valueB ? valueA : valueB; }
template<typename ValueType> template<typename ValueType>
inline ValueType min( const ValueType &valueA, const ValueType &valueB ) inline ValueType Min( const ValueType &valueA, const ValueType &valueB )
{ return valueA < valueB ? valueA : valueB; } { return valueA < valueB ? valueA : valueB; }
template<typename ValueType> template<typename ValueType>
inline ValueType radian( const ValueType &degree ) inline ValueType Average( const ValueType &valueA, const ValueType &valueB )
{ return (valueA + valueB) * 0.5f; }
template<typename ValueType>
inline ValueType AverageWithDelta( const ValueType &origin, const ValueType &delta )
{ return origin + (delta * 0.5f); }
template<typename ValueType>
inline ValueType Radian( const ValueType &degree )
{ return degree * (3.1415926535897932384626433832795f / 180.0f); } { return degree * (3.1415926535897932384626433832795f / 180.0f); }
template<typename ValueType> template<typename ValueType>
inline ValueType degree( const ValueType &radian ) inline ValueType Degree( const ValueType &radian )
{ return radian * (180.0f / 3.1415926535897932384626433832795f); } { return radian * (180.0f / 3.1415926535897932384626433832795f); }
// SPECIALIZATIONS //////////////////////////////////////////
template<> inline char Average<char>( const char &valueA, const char &valueB )
{ return (valueA + valueB) >> 1; }
template<> inline unsigned char Average<unsigned char>( const unsigned char &valueA, const unsigned char &valueB )
{ return (valueA + valueB) >> 1; }
template<> inline int Average<int>( const int &valueA, const int &valueB )
{ return (valueA + valueB) >> 1; }
template<> inline unsigned int Average<unsigned int>( const unsigned int &valueA, const unsigned int &valueB )
{ return (valueA + valueB) >> 1; }
template<> inline long Average<long>( const long &valueA, const long &valueB )
{ return (valueA + valueB) >> 1; }
template<> inline unsigned long Average<unsigned long>( const unsigned long &valueA, const unsigned long &valueB )
{ return (valueA + valueB) >> 1; }
template<> inline long long Average<long long>( const long long &valueA, const long long &valueB )
{ return (valueA + valueB) >> 1; }
template<> inline unsigned long long Average<unsigned long long>( const unsigned long long &valueA, const unsigned long long &valueB )
{ return (valueA + valueB) >> 1; }
template<> inline char AverageWithDelta<char>( const char &origin, const char &delta )
{ return origin + (delta >> 1); }
template<> inline unsigned char AverageWithDelta<unsigned char>( const unsigned char &origin, const unsigned char &delta )
{ return origin + (delta >> 1); }
template<> inline int AverageWithDelta<int>( const int &origin, const int &delta )
{ return origin + (delta >> 1); }
template<> inline unsigned int AverageWithDelta<unsigned int>( const unsigned int &origin, const unsigned int &delta )
{ return origin + (delta >> 1); }
template<> inline long AverageWithDelta<long>( const long &origin, const long &delta )
{ return origin + (delta >> 1); }
template<> inline unsigned long AverageWithDelta<unsigned long>( const unsigned long &origin, const unsigned long &delta )
{ return origin + (delta >> 1); }
template<> inline long long AverageWithDelta<long long>( const long long &origin, const long long &delta )
{ return origin + (delta >> 1); }
template<> inline unsigned long long AverageWithDelta<unsigned long long>( const unsigned long long &origin, const unsigned long long &delta )
{ return origin + (delta >> 1); }
} }
} }