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

namespace CommonHelpers {

//**************************************************************************************************
public static class HNLibAccess {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// <exception cref="TypeInitializationException" />
public static void Access()
{}

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

public static Exception LAST_ERROR()
{
   return LAST_ERROR_DATA().CreateException();
}
public static Exception LAST_ERROR(HNLibErrorToExceptonFlags flags)
{
   return LAST_ERROR_DATA().CreateException( flags );
}

public static unsafe string LAST_ERROR_MSG()
{
   return HSZ.FromPointer( (*(HNLibErrorStruct*)NLibrary.GetLastErrorPtr(
      "EC078259-E948-4936-B3AB-B36D058D1319" )).MessagePtr );
}
public static unsafe HNLibError LAST_ERROR_DATA()
{
   var pErrorStruct = (HNLibErrorStruct*)NLibrary.GetLastErrorPtr(
      "8161FFF0-E85A-45F0-919E-FD0AA954EC88" );
   return new HNLibError( ref *pErrorStruct );
}

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

/// <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 FREE( ref ipMemoryBlock );
}
[HNLibNotRequiresInit]
public static bool FREE([HOptional] ref IntPtr ipMemoryBlock)
{
   if ( ipMemoryBlock == IntPtr.Zero )
      return false;
   NativeLib_0.MemFree( HPointer.NullValueExchange( ref ipMemoryBlock ) );
   return true;
}

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

static HNLibAccess()
{
   CommonHelpers_Sys.Access();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HNLibAccess
//**************************************************************************************************

} // CommonHelpers
