#pragma once
#ifndef __HWINGUID_H__
#define __HWINGUID_H__
#include "hwinDef.h"
#include "hwinString.h"
#ifdef _MANAGED
#pragma managed(push,off)
#endif
#pragma pack(push,8)
namespace harlinn
{
namespace windows
{
class Guid : public GUID
{
public:
static Guid Empty( )
{
GUID result = { 0, };
return result;
}
Guid( )
{
memset( static_cast<GUID*>( this ), 0, sizeof( GUID ) );
}
explicit Guid( const WideString& theGuidAsAString )
{
*this = Parse( theGuidAsAString );
}
explicit Guid( const std::array<Byte, 16>& theBytes )
{
memcpy( static_cast<GUID*>( this ), theBytes.data( ), sizeof( GUID ) );
}
Guid( const GUID& theOther )
{
*( ( GUID* )this ) = theOther;
}
Guid& operator = ( const GUID& theOther )
{
*( ( GUID* )this ) = theOther;
return *this;
}
int CompareTo( const GUID& theOther ) const
{
auto result = memcmp( static_cast<const GUID*>( this ), static_cast<const GUID*>( &theOther ), sizeof( GUID ) );
return result;
}
bool operator == ( const GUID& theOther ) const
{
return CompareTo( theOther ) == 0;
}
bool operator != ( const GUID& theOther ) const
{
return CompareTo( theOther ) != 0;
}
bool operator <= ( const GUID& theOther ) const
{
return CompareTo( theOther ) <= 0;
}
bool operator < ( const GUID& theOther ) const
{
return CompareTo( theOther ) < 0;
}
bool operator >= ( const GUID& theOther ) const
{
return CompareTo( theOther ) >= 0;
}
bool operator > ( const GUID& theOther ) const
{
return CompareTo( theOther ) > 0;
}
bool IsEmpty( ) const
{
GUID empty = { 0, };
auto emptyCompareResult = memcmp( static_cast<const GUID*>( this ), static_cast<const GUID*>( &empty ), sizeof( GUID ) );
return emptyCompareResult == 0;
}
static HWIN_EXPORT Guid NewGuid( );
static Guid New( )
{
return NewGuid( );
}
static HWIN_EXPORT bool TryParse( const WideString& theGuidAsAString, Guid& result );
static HWIN_EXPORT Guid Parse( const WideString& theGuidAsAString );
HWIN_EXPORT String ToString( ) const;
HWIN_EXPORT WideString ToWideString( ) const;
HWIN_EXPORT AnsiString ToAnsiString( ) const;
std::array<Byte, 16> ToBytes( ) const
{
std::array<Byte, 16> result;
result[0] = ( byte )( Data1 );
result[1] = ( byte )( Data1 >> 8 );
result[2] = ( byte )( Data1 >> 16 );
result[3] = ( byte )( Data1 >> 24 );
result[4] = ( byte )( Data2 );
result[5] = ( byte )( Data2 >> 8 );
result[6] = ( byte )( Data3 );
result[7] = ( byte )( Data3 >> 8 );
result[8] = Data4[0];
result[9] = Data4[1];
result[10] = Data4[2];
result[11] = Data4[3];
result[12] = Data4[4];
result[13] = Data4[5];
result[14] = Data4[6];
result[15] = Data4[7];
return result;
}
int GetHashCode( ) const
{
const char* ptr = reinterpret_cast<const char*>( static_cast<const GUID*>( this ) );
return ptr[0] ^ ( ( *( ( int* )&ptr[4] ) << 16 ) | ( int )*( ( unsigned short* )&ptr[6] ) ) ^ ( ( ( int )ptr[10] << 24 ) | ptr[15] );
}
};
}
}
namespace std
{
template<>
struct hash<harlinn::windows::Guid> : public std::unary_function<harlinn::windows::Guid, size_t>
{
size_t operator()( const harlinn::windows::Guid& theGuid ) const
{
return static_cast<size_t>( theGuid.GetHashCode( ) );
}
};
}
#pragma pack(pop)
#ifdef _MANAGED
#pragma managed(pop)
#endif
#endif //__COMMONGUID_H__