// RuntimeForStatic base library | C+- (SysRoutine) | Author: Sergei Y.Kitáev
#include "Precompiled.h"
#include "SharedIncludes.h"
namespace RuntimeForStatic {
//==================================================================================================

$ErrorSign::$ErrorSign()
{
   $CLEAR_INSTANCE();
}

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

$ErrorSign::$ErrorSign($ErrorType type)
: $ErrorSign( type, true, false, 0, 0 )
{}

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

$ErrorSign::$ErrorSign($ErrorType type,$ErrorCode code)
: $ErrorSign( type, true, false, code, 0 )
{}
$ErrorSign::$ErrorSign($ErrorType type,$ErrorCode code,$ErrorCode exCode)
: $ErrorSign( type, true, false, code, exCode )
{}
$ErrorSign::$ErrorSign( $ErrorType type, bool hasError, bool isWarning,
                        $ErrorCode code, $ErrorCode exCode )
: $ErrorSign()
{
   _ErrorType_ = type; _HasError_ = hasError; _IsWarning_ = isWarning;
   _Code_ = code; _ExCode_ = exCode;
}

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

$ErrorSign::$ErrorSign( $ErrorType type, int code )
: $ErrorSign( type, true, false, code, 0 )
{}
$ErrorSign::$ErrorSign( $ErrorType type, int code, int exCode )
: $ErrorSign( type, true, false, code, exCode )
{}
$ErrorSign::$ErrorSign( $ErrorType type, int code, DWORD exCode )
: $ErrorSign( type, true, false, code, (int)exCode )
{}
$ErrorSign::$ErrorSign( $ErrorType type, bool hasError, bool isWarning,
                        int code, int exCode )
{
   _ErrorType_ = type; _HasError_ = hasError; _IsWarning_ = isWarning;
   _Code_.AsInteger = code; _ExCode_.AsInteger = exCode;
}
$ErrorSign::$ErrorSign( $ErrorType type, bool hasError, bool isWarning,
                        int code, DWORD exCode )
: $ErrorSign( type, hasError, isWarning, code, (int)exCode )
{}

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

$ErrorSign::$ErrorSign( $ErrorType type, DWORD code )
: $ErrorSign( type, true, false, code, 0 )
{}
$ErrorSign::$ErrorSign( $ErrorType type, DWORD code, int exCode )
: $ErrorSign( type, true, false, code, (DWORD)exCode )
{}
$ErrorSign::$ErrorSign( $ErrorType type, DWORD code, DWORD exCode )
: $ErrorSign( type, true, false, code, exCode )
{}
$ErrorSign::$ErrorSign( $ErrorType type, bool hasError, bool isWarning,
                        DWORD code, int exCode )
: $ErrorSign( type, hasError, isWarning, code, (DWORD)exCode )
{}
$ErrorSign::$ErrorSign( $ErrorType type, bool hasError, bool isWarning,
                        DWORD code, DWORD exCode )
{
   _ErrorType_ = type; _HasError_ = hasError; _IsWarning_ = isWarning;
   _Code_.AsDWord = code; _ExCode_.AsDWord = exCode;
}

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

bool $ErrorSign::GetHasError() const
{
   return _HasError_;
}
bool $ErrorSign::GetSuccess() const
{
   return !HasError;
}
bool $ErrorSign::GetIsWarning() const
{
   return _IsWarning_;
}

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

$ErrorType $ErrorSign::GetErrorType() const
{
   return _ErrorType_;
}

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

bool $ErrorSign::GetIsWin32Error() const
{
   return ErrorType == $ErrorType::Win32;
}
bool $ErrorSign::GetIsCOMError() const
{
   return ErrorType == $ErrorType::COM;
}
bool $ErrorSign::GetIsWin32OrCOMError() const
{
   return GetIsWin32Error() || GetIsCOMError();
}
bool $ErrorSign::GetIsCustomError() const
{
   return ErrorType == $ErrorType::Custom;
}
bool $ErrorSign::GetIsUnknownError() const
{
   return ErrorType == $ErrorType::UnknownError;
}

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

int $ErrorSign::GetIntCode() const
{
   return _Code_.AsInteger;
}
DWORD $ErrorSign::GetDWordCode() const
{
   return _Code_.AsDWord;
}

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

$ErrorCode $ErrorSign::GetCode() const
{
   return _Code_;
}
$ErrorCode $ErrorSign::GetExCode() const
{
   return _ExCode_;
}

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

DWORD $ErrorSign::StrictWin32Error($O) const $fatal_error_on_failure
{
   if ( ErrorType != $ErrorType::Win32 )
      $INVALID_OPERATION();
   return _Code_.AsDWord;
}
HRESULT $ErrorSign::StrictCOMError($O) const $fatal_error_on_failure
{
   if ( ErrorType != $ErrorType::COM )
      $INVALID_OPERATION();
   return _Code_.AsInteger;
}

DWORD $ErrorSign::GetWin32ErrorOrZero() const
{
   return _Code_.AsDWord;
}
HRESULT $ErrorSign::GetCOMErrorOrZero() const
{
   return _Code_.AsInteger;
}

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

$ErrorSign $ErrorSign::CreateWin32()
{
   $ErrorCode errorCode = {}, errorExCode = {};
   return $ErrorSign( $ErrorType::Win32, true, false, errorCode, errorExCode );
}
$ErrorSign $ErrorSign::CreateWin32(DWORD dwError)
{
   $ErrorCode errorCode = { dwError }, errorExCode = {};
   return $ErrorSign( $ErrorType::Win32, $BOOL(dwError), false, errorCode, errorExCode );
}

$ErrorSign $ErrorSign::CreateCOM()
{
   $ErrorCode errorCode = {}, errorExCode = {};
   return $ErrorSign( $ErrorType::COM, true, false, errorCode, errorExCode );
}
$ErrorSign $ErrorSign::CreateCOM(HRESULT status)
{
   $ErrorCode errorCode = { status }, errorExCode = {};
   return $ErrorSign( $ErrorType::COM, FAILED(status), $BOOL(status) && SUCCEEDED(status),
                      errorCode, errorExCode );
}

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

$ErrorSign $ErrorSign::CreateCustom(int code)
{
   return CreateCustom( $BOOL(code), false, code );
}
$ErrorSign $ErrorSign::CreateCustom(bool hasError,bool isWarning,int code)
{
   return CreateCustom( hasError, isWarning, code, (int)0 );
}
$ErrorSign $ErrorSign::CreateCustom(bool hasError,bool isWarning,int code,int exCode)
{
   return $ErrorSign( $ErrorType::Custom, hasError, isWarning,
                      $ErrorCode( code ), $ErrorCode( exCode ) );
}

$ErrorSign $ErrorSign::CreateCustom(DWORD code)
{
   return CreateCustom( $BOOL(code), false, code );
}
$ErrorSign $ErrorSign::CreateCustom(bool hasError,bool isWarning,DWORD code)
{
   return CreateCustom( hasError, isWarning, code, (DWORD)0 );
}
$ErrorSign $ErrorSign::CreateCustom(bool hasError,bool isWarning,DWORD code,DWORD exCode)
{
   return $ErrorSign( $ErrorType::Custom, hasError, isWarning,
                      $ErrorCode( code ), $ErrorCode( exCode ) );
}

//==================================================================================================
} // RuntimeForStatic
