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

namespace CommonHelpers {

//**************************************************************************************************
static partial class SysUtilsHlp {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static Exception LAST_ERROR()
{
   return HNLibAccess.LAST_ERROR( ErrorToExceptonFlags );
}

public static string LAST_ERROR_MSG()
{
   return HNLibAccess.LAST_ERROR_MSG();
}

public static HNLibError LAST_ERROR_DATA()
{
   return HNLibAccess.LAST_ERROR_DATA();
}

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

/// <exception cref="ArgumentException" />
/// <exception cref="OutOfMemoryException" />
/// <exception cref="HOperationFailedException" />
[HReturnsTrue]
public static bool VERIFY(bool success)
{
   if ( !success )
      throw LAST_ERROR_DATA().CreateException();
   return true;
}
/// <exception cref="ArgumentException" />
/// <exception cref="OutOfMemoryException" />
/// <exception cref="HOperationFailedException" />
[HReturnsSource]
public static IntPtr VERIFY([HRequired] IntPtr pointer)
{
   VERIFY( pointer != IntPtr.Zero );
   return pointer;
}

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

/// <exception cref="ArgumentException" />
/// <exception cref="OutOfMemoryException" />
/// <exception cref="HOperationFailedException" />
[HReturnsTrue]
public static bool VERIFY_NO_ERROR()
{
   var error = LAST_ERROR_DATA();
   if ( error.HasError )
      throw error.CreateException();
   return true;
}

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

/// <exception cref="ArgumentException" />
/// <exception cref="OutOfMemoryException" />
/// <exception cref="HOperationFailedException" />
[HNotNull]
public static string STR([HRequired] IntPtr pointer)
{
   return STR( ref pointer );
}
/// <exception cref="ArgumentException" />
/// <exception cref="OutOfMemoryException" />
/// <exception cref="HOperationFailedException" />
[HNotNull]
public static string STR([HRequired] ref IntPtr pointer)
{
   var ptr = VERIFY( pointer ); pointer = IntPtr.Zero;
   try { return HSZ.FromPointer( ptr ); }
   finally { FREE( ptr ); }
}

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

/// <exception cref="ArgumentException" />
[HNotNull]
public static string STR_0([HRequired] IntPtr pointer)
{
   return STR_0( ref pointer );
}
/// <exception cref="ArgumentException" />
[HNotNull]
public static string STR_0([HRequired] ref IntPtr pointer)
{
   HPointerArgChecking.VerifyNotNull( pointer );
   var ptr = HPointer.NullValueExchange( ref pointer );
   try { return HSZ.FromPointer( ptr ); }
   finally { FREE( ptr ); }
}

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

[HNLibNotRequiresInit]
public static bool FREE([HOptional] IntPtr ipMemoryBlock)
{
   return HNLibAccess.FREE( ipMemoryBlock );
}
[HNLibNotRequiresInit]
public static bool FREE([HOptional] ref IntPtr ipMemoryBlock)
{
   return HNLibAccess.FREE( ref ipMemoryBlock );
}

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

const HNLibErrorToExceptonFlags ErrorToExceptonFlags = 0;
   //HNLibErrorToExceptonFlags.OutOfMemoryAsOperationFailed;
   //HNLibErrorToExceptonFlags.TreatAnyAsOperationFailed;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // SysUtilsHlp
//**************************************************************************************************

} // CommonHelpers
