clean up assert and verify

This commit is contained in:
hathach
2018-03-29 13:07:27 +07:00
parent ccf6f03817
commit c50da4962e
8 changed files with 75 additions and 78 deletions

View File

@@ -41,15 +41,21 @@
#include "tusb_option.h"
#include "tusb_compiler.h"
/*------------------------------------------------------------------*/
/* This file use an advanced macro technique to mimic the default parameter
* as C++ for the sake of code simplicity. Beware of a headache macro
* manipulation that you are told to stay away.
*
* e.g
*
* - VERIFY( cond ) will return false if cond is false
* - VERIFY( cond, err) will return err instead if cond is false
*------------------------------------------------------------------*/
#ifdef __cplusplus
extern "C" {
#endif
/**
* Helper to implement optional parameter for VERIFY Macro family
*/
#define GET_3RD_ARG(arg1, arg2, arg3, ...) arg3
#define GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4
//--------------------------------------------------------------------+
// VERIFY Helper
@@ -76,93 +82,99 @@ static inline void verify_breakpoint(void)
}
#else
#define verify_breakpoint()
#endif
/*------------------------------------------------------------------*/
/* VERIFY STATUS
* - VERIFY_STS_1ARGS : return status of condition if failed
* - VERIFY_STS_2ARGS : return provided status code if failed
/* Macro Generator
*------------------------------------------------------------------*/
#define VERIFY_STS_1ARGS(sts) \
do { \
uint32_t _status = (uint32_t)(sts); \
if ( 0 != _status ) { _VERIFY_MESS(_status) return _status; } \
} while(0)
#define VERIFY_STS_2ARGS(sts, _error) \
do { \
uint32_t _status = (uint32_t)(sts); \
if ( 0 != _status ) { _VERIFY_MESS(_status) return _error; }\
} while(0)
// Helper to implement optional parameter for VERIFY Macro family
#define GET_3RD_ARG(arg1, arg2, arg3, ...) arg3
#define GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4
/**
* Check if status is success (zero), otherwise return
* - status value if called with 1 parameter e.g VERIFY_STATUS(status)
* - 2 parameter if called with 2 parameters e.g VERIFY_STATUS(status, errorcode)
*/
#define VERIFY_STATUS(...) GET_3RD_ARG(__VA_ARGS__, VERIFY_STS_2ARGS, VERIFY_STS_1ARGS)(__VA_ARGS__)
/*------------- Generator for VERIFY and VERIFY_HDLR -------------*/
#define VERIFY_DEFINE(_cond, _handler, _error) do { if ( !(_cond) ) { _handler return _error; } } while(0)
/*------------------------------------------------------------------*/
/* VERIFY STATUS WITH HANDLER
* - VERIFY_STS_HDLR_2ARGS : execute handler, return status if failed
* - VERIFY_STS_HDLR_3ARGS : execute handler, return provided error if failed
*------------------------------------------------------------------*/
#define VERIFY_STS_HDLR_2ARGS(sts, _handler) \
/*------------- Generator for VERIFY_STATUS and VERIFY_STATUS_HDLR -------------*/
#define VERIFY_STS_DEF2(sts, _handler) \
do { \
uint32_t _status = (uint32_t)(sts); \
if ( 0 != _status ) { verify_breakpoint(); _VERIFY_MESS(_status) _handler; return _status; }\
if ( 0 != _status ) { _VERIFY_MESS(_status) _handler return _status; }\
} while(0)
#define VERIFY_STS_HDLR_3ARGS(sts, _handler, _error) \
#define VERIFY_STS_DEF3(sts, _handler, _error) \
do { \
uint32_t _status = (uint32_t)(sts); \
if ( 0 != _status ) { verify_breakpoint(); _VERIFY_MESS(_status) _handler; return _error; }\
if ( 0 != _status ) { _VERIFY_MESS(_status) _handler return _error; }\
} while(0)
#define VERIFY_STATUS_HDLR(...) GET_4TH_ARG(__VA_ARGS__, VERIFY_STS_HDLR_3ARGS, VERIFY_STS_HDLR_2ARGS)(__VA_ARGS__)
/*------------------------------------------------------------------*/
/* VERIFY
* - VERIFY_1ARGS : return false if failed
* - VERIFY_2ARGS : return provided value if failed
*------------------------------------------------------------------*/
#define VERIFY_1ARGS(cond) do { if (!(cond)) return false; } while(0)
#define VERIFY_2ARGS(cond, _error) do { if (!(cond)) return _error; } while(0)
#define VERIFY_1ARGS(_cond) VERIFY_DEFINE(_cond, , false)
#define VERIFY_2ARGS(_cond, _error) VERIFY_DEFINE(_cond, , _error)
#define VERIFY(...) GET_3RD_ARG(__VA_ARGS__, VERIFY_2ARGS, VERIFY_1ARGS)(__VA_ARGS__)
/**
* Check if condition is success (true), otherwise return
* - false value if called with 1 parameter e.g VERIFY(condition)
* - 2 parameter if called with 2 parameters e.g VERIFY(condition, errorcode)
*/
#define VERIFY(...) GET_3RD_ARG(__VA_ARGS__, VERIFY_2ARGS, VERIFY_1ARGS)(__VA_ARGS__)
/*------------------------------------------------------------------*/
/* VERIFY WITH HANDLER
* - VERIFY_HDLR_2ARGS : execute handler, return false if failed
* - VERIFY_HDLR_3ARGS : execute handler, return provided error if failed
*------------------------------------------------------------------*/
#define VERIFY_HDLR_2ARGS(cond, _handler) \
do { if ( !(cond) ) { _handler; return false; } } while(0)
#define VERIFY_HDLR_2ARGS(cond, _handler) VERIFY_DEFINE(cond, _handler; , false)
#define VERIFY_HDLR_3ARGS(cond, _handler, _error) VERIFY_DEFINE(cond, _handler; , _error)
#define VERIFY_HDLR_3ARGS(cond, _handler, _error) \
do { if ( !(cond) ) { _handler; return _error; } } while(0)
#define VERIFY_HDLR(...) GET_4TH_ARG(__VA_ARGS__, VERIFY_HDLR_3ARGS, VERIFY_HDLR_2ARGS)(__VA_ARGS__)
/*------------------------------------------------------------------*/
/* VERIFY STATUS
* - VERIFY_STS_1ARGS : return status of condition if failed
* - VERIFY_STS_2ARGS : return provided status code if failed
*------------------------------------------------------------------*/
#define VERIFY_STS_1ARGS(sts) VERIFY_STS_DEF2(sts, )
#define VERIFY_STS_2ARGS(sts, _error) VERIFY_STS_DEF3(sts, ,_error)
#define VERIFY_STATUS(...) GET_3RD_ARG(__VA_ARGS__, VERIFY_STS_2ARGS, VERIFY_STS_1ARGS)(__VA_ARGS__)
/*------------------------------------------------------------------*/
/* VERIFY STATUS WITH HANDLER
* - VERIFY_STS_HDLR_2ARGS : execute handler, return status if failed
* - VERIFY_STS_HDLR_3ARGS : execute handler, return provided error if failed
*------------------------------------------------------------------*/
#define VERIFY_STS_HDLR_2ARGS(sts, _handler) VERIFY_STS_DEF2(sts, _handler;)
#define VERIFY_STS_HDLR_3ARGS(sts, _handler, _error) VERIFY_STS_DEF3(sts, _handler; , _error)
#define VERIFY_STATUS_HDLR(...) GET_4TH_ARG(__VA_ARGS__, VERIFY_STS_HDLR_3ARGS, VERIFY_STS_HDLR_2ARGS)(__VA_ARGS__)
#define VERIFY_HDLR(...) GET_4TH_ARG(__VA_ARGS__, VERIFY_HDLR_3ARGS, VERIFY_HDLR_2ARGS)(__VA_ARGS__)
/*------------------------------------------------------------------*/
/* ASSERT
* basically VERIFY with verify_breakpoint as handler
* basically VERIFY with verify_breakpoint() as handler
* - 1 arg : return false if failed
* - 2 arg : return error if failed
*------------------------------------------------------------------*/
#define ASSERT_1ARGS(cond) do { if (!(cond)) { verify_breakpoint(); _ASSERT_MESS() return false; } } while(0)
#define ASSERT_2ARGS(cond, _error) do { if (!(cond)) { verify_breakpoint(); _ASSERT_MESS() return _error;} } while(0)
#define ASSERT_1ARGS(cond) VERIFY_DEFINE(cond, verify_breakpoint(); , false)
#define ASSERT_2ARGS(cond, _error) VERIFY_DEFINE(cond, verify_breakpoint(); , _error)
#define TU_ASSERT(...) GET_3RD_ARG(__VA_ARGS__, ASSERT_2ARGS, ASSERT_1ARGS)(__VA_ARGS__)
#define TU_ASSERT(...) GET_3RD_ARG(__VA_ARGS__, ASSERT_2ARGS, ASSERT_1ARGS)(__VA_ARGS__)
/*------------------------------------------------------------------*/
/* ASSERT Error
* basically VERIFY Error with verify_breakpoint() as handler
*------------------------------------------------------------------*/
#define ASERT_ERR_1ARGS(_err) VERIFY_STS_DEF2(_err, verify_breakpoint();)
#define ASERT_ERR_2ARGS(_err, _ret) VERIFY_STS_DEF3(_err, verify_breakpoint();, _ret)
#define ASSERT_ERR(...) GET_3RD_ARG(__VA_ARGS__, ASERT_ERR_2ARGS, ASERT_ERR_1ARGS)(__VA_ARGS__)
#ifdef __cplusplus
}