// NativeLib DLL (for DotNetHelpers) | C+- (SysRoutine) | Author: Sergei Y.Kitáev
#include "Precompiled.h"
#include "SharedIncludes.h"
namespace NativeLib {
//==================================================================================================

bool NLFailure::SetError( NLErrorCat category $optional ) $ret_false
{
   return SetError( category, nullptr, 0, 0 );
}
bool NLFailure::SetError( NLErrorCat category $optional, INT code $optional ) $ret_false
{
   return SetError( category, nullptr, code, 0 );
}
bool NLFailure::SetError( NLErrorCat category $optional, UINT uCode $optional ) $ret_false
{
   return SetError( category, nullptr, uCode, 0 );
}
bool NLFailure::SetError( NLErrorCat category $optional,
                          INT code $optional, DWORD value $optional ) $ret_false
{
   return SetError( category, nullptr, code, value );
}
bool NLFailure::SetError( NLErrorCat category $optional,
                          UINT uCode $optional, DWORD value $optional ) $ret_false
{
   return SetError( category, nullptr, uCode, value );
}

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

bool NLFailure::SetError( PCWSTR message )
{
   return SetError( NLErrorCat_General, message, 0, 0 );
}
bool NLFailure::SetError( PCWSTR message $optional, INT code $optional ) $ret_false
{
   return SetError( NLErrorCat_General, message, code, 0 );
}
bool NLFailure::SetError( PCWSTR message $optional, UINT uCode $optional ) $ret_false
{
   return SetError( NLErrorCat_General, message, uCode, 0 );
}
bool NLFailure::SetError( PCWSTR message $optional, INT code $optional, DWORD value $optional )
                          $ret_false
{
   return SetError( NLErrorCat_General, message, code, value );
}
bool NLFailure::SetError( PCWSTR message $optional, UINT uCode $optional, DWORD value $optional )
                          $ret_false
{
   return SetError( NLErrorCat_General, message, uCode, value );
}

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

bool NLFailure::SetError( NLErrorCat category $optional, PCWSTR message $optional ) $ret_false
{
   return SetError( category, message, 0, 0 );
}
bool NLFailure::SetError( NLErrorCat category $optional, PCWSTR message $optional,
                          INT code $optional ) $ret_false
{
   return SetError( category, message, code, 0 );
}
bool NLFailure::SetError( NLErrorCat category $optional, PCWSTR message $optional,
                          UINT uCode $optional ) $ret_false
{
   return SetError( category, message, uCode, 0 );
}
bool NLFailure::SetError( NLErrorCat category $optional, PCWSTR message $optional,
                          INT code $optional, DWORD value $optional ) $ret_false
{
   return SetError( category, message, (UINT)code, value );
}
bool NLFailure::SetError( NLErrorCat category $optional, PCWSTR message $optional,
                          UINT uCode $optional, DWORD value $optional ) $ret_false
{
   NLError &data = *FreeErrorData();
   if ( !message )
   {
      data.Category = category;
      data.Code32 = uCode;
      data.Value32 = value;
      switch ( category )
      {
      case NLErrorCat_None:
         ResetError();
         data.Code32 = uCode;
         data.Value32 = value;
         return false;
      case NLErrorCat_General:
         data.MessagePtr = (PWSTR)$TEXT(
            L"General error.",
            L"Ошибка общей категории." );
         return false;
      case NLErrorCat_Argument:
         SetArgumentError();
         data.Code32 = uCode;
         data.Value32 = value;
         return false;
      case NLErrorCat_OutOfMemory:
         SetOutOfMemoryError();
         data.Code32 = uCode;
         data.Value32 = value;
         return false;
      case NLErrorCat_System:
         SetSystemError( uCode );
         data.Value32 = value;
         return false;
      case NLErrorCat_COM:
         SetCOMError( uCode );
         data.Value32 = value;
         return false;
/*
      case NLErrorCat_...:
         data.MessagePtr = (PWSTR)$TEXT(
            L"Error of ... category.",
            L"Ошибка ... категории." );
         return false;
*/
      default:
         data.MessagePtr = (PWSTR)$TEXT(
            L"Error of undetermined category.",
            L"Ошибка неопределённой категории." );
         return false;
      }; // switch
   } // !message
   PWSTR pMessage = $SZ::DuplicateNS($, message );
   if ( !pMessage )
   {
      data.Category = NLErrorCat_System;
      data.MessagePtr = (PWSTR)$StaticMessages::MemoryAllocationFailure();
      return false;
   }
   data.Category = category;
   data.MessagePtr = pMessage;
   data.IsAllocatedMsg = true;
   data.Code32 = uCode;
   data.Value32 = value;
   return false;
}

//==================================================================================================
} // NativeLib
