/**
 *
 * File:            ErrTrace.h 
 * Module:          System error and exception module
 *                  Real View Bench
 *
 * Revision:          2.1
 * Last edited:       2021/10/10 21:46:32  
 * Author:            Huang Dongyun
  *
 * PMN 2000-11-28 Created.
 * 
 */
 
#ifndef __RV_ERR_H_INCLUDED_
#define __RV_ERR_H_INCLUDED_

 
#include "eeh.h"


/* RVB error codes, for application writer. If you change them, 
 * remember to change the switch
 * statement in print_error() in errdefs.cpp.
 */ 
//#define RV_OK                           0
#define RV_ERR_NONE                      0
#define RV_OK                            0



//#define REC_FAILURE                   -1
#define RV_ERR_GENERIC                 ( -1)
#define RV_FAILED                       (-1)
#define RV_ERR_MEM_ALLOC                (-2)
#define RV_ERR_NULL_POINTER             (-3)
#define RV_ERR_INVALID_OBJECT           (-3)
#define RV_ERR_MISMATCH                 (-4) //不匹配的类型
#define RV_ERR_BAD_SIZE                 (-5)
#define RV_ERR_UNSUPPORTED              (-6)
#define RV_ERR_UNKNOWN_TYPE             (-6)
#define RV_ERR_OUT_OF_RANGE             (-7)
#define RV_ERR_FILE_MISSED              (-8)
#define RV_ERR_FILE_FAILURE             (-9)
#define RV_ERR_NOSAMPLE                 (-10)
#define RV_ERR_NO_TRAINING               (-11)
#define RV_ERR_CLOSED                   (-12)
#define RV_ERR_TIME_OUT                (-13)
#define RV_ERR_READ                    (-14)
#define RV_ERR_WRITE                   (-15)
#define RV_ERR_MATCH_NONE              (-16) //匹配失败，没有找到合适的实例
#define RV_ERR_BUSY_TRAINING           (-17) //正在训练
#define RV_ERR_NOT_INITED              (-18) //没有初始化
#define RV_ERR_NOT_READY               (-19) 
#define RV_ERR_FORMAT                  (-20) //格式错误
#define RV_ERR_VERSION                 (-21) //文本错误
#define RV_ERR_DUPLICATE_KEYWORD       (-22)  //主键冲突
#define RV_ERR_EMPTY_STRING            (-23)  //空字符串
#define RV_ERR_FULL_CONTAINER          (-24)  //容器满
#define RV_ERR_ACCESS                  (-25)    //访问权限错误
#define RV_ERR_LOADLIB                 (-26)  //动态库装载失败
#define RV_ERR_RE_ENTRY                (-27)  //重入,




#define RV_ERR_NOT_STARTED             RV_ERR_NOT_OPEN
#define RV_ERR_WAITING                 RV_ERR_BUSY
#define RV_ERR_INVALID_NAME            RV_ERR_INVALID_OBJECT
#define RV_ERR_BUSY                    RV_ERR_BUSY_TRAINING
#define RV_ERR_NONLEARNED              RV_ERR_NO_TRAINING
#define RV_ERR_WRONG_MODE              RV_ERR_MISMATCH   //模式错误,模式不支持某动作
#define RV_ERR_UNKNOWN_MSG             RV_ERR_UNKNOWN_TYPE 
#define RV_ERR_UNKNOWN_CMD             RV_ERR_UNKNOWN_MSG 
#define RV_ERR_OVERFLOW                RV_ERR_OUT_OF_RANGE
#define RV_ERR_NOT_OPEN                RV_ERR_CLOSED
#define RV_ERR_OPEN_ALREADY            RV_ERR_RE_ENTRY
#define RV_ERR_STARTED_ALREADY         RV_ERR_RE_ENTRY

#define RV_ERR_ZERO_SIZE               RV_ERR_NOSAMPLE
 
#define RV_ERR_DEVICE                   (-100)  //硬件错误
#define RV_ERR_FIRMWARE                 RV_ERR_DEVICE  



#define RV_ERR_UNAUTHORIZED             -999

  

#define rveRegister(strFuncName, errCode, message) \
                   rveRegisterFileLine(strFuncName, errCode, __FILE__, __LINE__, message, 0)
 
/**
 * \brief Macro: Registers an error.with a number attached
 * \param strFuncName    Name of function in which error occurs
 * \param errCode     Numeric code of error
 * \param message      Message string describing error
 * \param number       Number to attach to error
 * \return No value.
 *
 * This macro abbreviates rveRegisterFileLine() by hiding the file and
 * line argument. Refer to rveRegisterFileLine() for details on using
 * this macro.
 *
 * \sa rveRegister().
 */ 
#define  rveRegisterWithNumber(strFuncName, errCode, message, number) \
              rveRegisterFileLine(strFuncName, errCode, __FILE__, __LINE__, message, number)
 
 

 int  EEH_API  rveRegisterFileLine(
                 const TCHAR * strFuncName,
                 int          errCode,
                 const TCHAR * strfileName, /* Typically use __FILE__ */
                 int          lineNumber, /* Typically use __LINE__ */
                 const TCHAR * message,
                 int          number);


  

 

 
//The following three function is for application writer only
extern "C"  int   EEH_API     rvGetLastError(void);
extern "C"  void  EEH_API     rveSetLastError(int dwErrCode);

extern "C" const TCHAR* EEH_API rvGetErrorDescription(int errCode);
  
 

//internal use only.
/*
  RV_ERROR macro unconditionally raises error with passed code and message.
  After raising error, control will be transferred to the exit label.
*/
#define RV_REGISTER_ERROR( code, msg )                                       \
{                                                                   \
     rveSetLastError(code);  \
}

/* Simplified form of RV_ERROR */
#define RV_ERROR_CODE( code )     RV_REGISTER_ERROR(code,_T(""))
#define RV_ERROR( code, msg )     RV_REGISTER_ERROR( code, msg )           

#define RV_FLUSH_ERROR()  \
{ \
       rveSetLastError(RV_OK); \
}

#define RV_SET_LAST_ERROR(code)  rveSetLastError(code)

#define RV_GET_LAST_ERROR()  rvGetLastError()

#define ON_ERROR_RETURN(func, ret)                                                  \
{       \
	func;                    \
    if( rvGetLastError() < 0 ){                                      \
       /* RV_REGISTER_ERROR( REC_BACK_TRACE, "Inner function failed." );     */ \
		return ret; \
	} \
}

 
#define ON_ERROR_QUIT(func )    \
{       \
	func;                    \
    if( rvGetLastError() < 0 ){                                      \
		return  ; \
	} \
}
 
#define ON_ERROR_GOTO(func, label)           \
{        \
	func;               \
    if( rvGetLastError() < 0 ){                                      \
        /* RV_REGISTER_ERROR( REC_BACK_TRACE, "Inner function failed." );     */ \
		goto label; \
	} \
}



#define IF_FAIL_THEN_RET(condition, code,  ret)                                                  \
{  \
    if(! (condition) ){                                      \
        RV_ERROR( code , _T("")); \
		return ret; \
	} \
}


#define IF_FAIL_THEN_QUIT(condition, code )    \
{  \
    if(! (condition) ){      \
        RV_ERROR( code, _T("") ); \
		return  ; \
	} \
}
   
#define IF_FAIL_THEN_GOTO(condition, label)           \
{        \
    if(! (condition) ){                                      \
        RV_ERROR( REC_INTERNAL_CALL, _T("Assertion: ") #condition _T(" failed") ); \
		goto label; \
	} \
}

 
 
 

#endif /* #ifndef __RV_ERR_H_INCLUDED_ */
