//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;
using System.ComponentModel;

namespace CommonHelpers {

//**************************************************************************************************
/// <summary>
/// </summary>
public static partial class HUnsafeSZ {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static void VerifyCapacity(int capacity)
{
   HSZ.VerifyCapacity( capacity );
}

public static unsafe string ToString([HOptional][HIn] char* psz)
{
   return HSZ.ToString( (IntPtr)psz );
}

//==================================================================================================

public static unsafe int Length([HIn] char* psz)
{
   return HSZ.Length( (IntPtr)psz );
}

public static unsafe bool IsEmpty([HIn] char* psz)
{
   return HSZ.IsEmpty( (IntPtr)psz );
}

public static unsafe bool IsNullOrEmpty([HIn] char* psz)
{
   return HSZ.IsNullOrEmpty( (IntPtr)psz );
}

//--------------------------------------------------------------------------------------------------

/// <exception cref="ArgumentException" />
/// <exception cref="HOperationFailedException" />
public static unsafe char* Copy([HIn] char* pszSource,[HOut] char* pszDestination)
{
   return (char*)HSZ.Copy(
      (IntPtr)pszSource, (IntPtr)pszDestination );
}

/// <exception cref="ArgumentException" />
/// <exception cref="HOperationFailedException" />
public static unsafe char* Copy( [HIn] char* pszSource,
                                 [HOut] char* pszDestinationBuffer,
                                 int destBuffCapacity )
{
   return (char*)HSZ.Copy(
      (IntPtr)pszSource, (IntPtr)pszDestinationBuffer, destBuffCapacity );
}

//==================================================================================================

/// <exception cref="HOperationFailedException" />
public static unsafe char* Allocate(int capacity)
{
   return (char*)HSZ.Allocate( capacity );
}

//--------------------------------------------------------------------------------------------------

/// <exception cref="HOperationFailedException" />
public static unsafe char* ReAllocate([HIn] char* pAllocatedBuffer,int newCapacity)
{
   return (char*)HSZ.ReAllocate( (IntPtr)pAllocatedBuffer, newCapacity );
}

/// <exception cref="HOperationFailedException" />
public static unsafe char* ReAllocate([HIn] ref char* pAllocatedBuffer,int newCapacity)
{
   return pAllocatedBuffer = (char*)HSZ.ReAllocate(
      (IntPtr)pAllocatedBuffer, newCapacity );
}

//--------------------------------------------------------------------------------------------------

/// <exception cref="HOperationFailedException" />
public static unsafe char* ReAllocateByLength([HIn] char* pszAllocatedSzBuffer)
{
   return (char*)HSZ.ReAllocateByLength( (IntPtr)pszAllocatedSzBuffer );
}

/// <exception cref="HOperationFailedException" />
public static unsafe char* ReAllocateByLength([HIn] ref char* pszAllocatedSzBuffer)
{
   return pszAllocatedSzBuffer = (char*)HSZ.ReAllocateByLength( (IntPtr)pszAllocatedSzBuffer );
}

//--------------------------------------------------------------------------------------------------

/// <exception cref="HOperationFailedException" />
public static unsafe char* Duplicate([HIn] char* psz)
{
   return (char*)HSZ.Duplicate( (IntPtr)psz );
}

/// <exception cref="HOperationFailedException" />
public static unsafe char* Duplicate(string str)
{
   return (char*)HSZ.Duplicate( str );
}

/// <exception cref="HOperationFailedException" />
public static unsafe char* Duplicate([HIn] IntPtr ptrSZ)
{
   return (char*)HSZ.Duplicate( (char*)ptrSZ );
}

/// <exception cref="HOperationFailedException" />
public static unsafe char* DuplicateAllocatedBuffer([HIn] char* pAllocatedBuffer)
{
   return (char*)HSZ.DuplicateAllocatedBuffer( (IntPtr)pAllocatedBuffer );
}

//--------------------------------------------------------------------------------------------------

/// <exception cref="HOperationFailedException" />
public static unsafe int GetAllocatedCapacity(char* pAllocatedBuffer)
{
   return HSZ.GetAllocatedCapacity( (IntPtr)pAllocatedBuffer );
}

//--------------------------------------------------------------------------------------------------

/// <exception cref="Win32Exception" />
public static unsafe bool Free([HOptional] char* pAllocatedBuffer)
{
   return HSZ.Free( (IntPtr)pAllocatedBuffer );
}

/// <exception cref="Win32Exception" />
public static unsafe bool Free([HOptional][HIn][HOut] char** ppAllocatedBuffer)
{
   return Free( (char*)HUnsafePtr.InterlockedNullValueExchange(
      (void**)ppAllocatedBuffer ) );
}

//==================================================================================================

static HUnsafeSZ()
{
   CommonHelpers_PAccess.Access();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HUnsafeSZ
//**************************************************************************************************

} // CommonHelpers
