VS 中 __VA_ARGS__
2019-07-19 本文已影响0人
零度心脉
//遍历不定参数,不定参数展开 <=9个
//vs中这种方式定义宏过度也不能用
define EXTAND_ARGS(args) args //VA_ARGS在vs中会被认为是一个实参,所以需要定义该宏过度
define FOR_EACH_1(what,ParamType, ...) what(ParamType,1)
define FOR_EACH_2(what,ParamType, ...) what(ParamType,2), EXTAND_ARGS(FOR_EACH_1(what, VA_ARGS))
define FOR_EACH_3(what,ParamType, ...) what(ParamType,3), EXTAND_ARGS(FOR_EACH_2(what, VA_ARGS))
define FOR_EACH_4(what,ParamType, ...) what(ParamType,4), EXTAND_ARGS(FOR_EACH_3(what, VA_ARGS))
define FOR_EACH_5(what,ParamType, ...) what(ParamType,5), EXTAND_ARGS(FOR_EACH_4(what, VA_ARGS))
define FOR_EACH_6(what,ParamType, ...) what(ParamType,6), EXTAND_ARGS(FOR_EACH_5(what, VA_ARGS))
define FOR_EACH_7(what,ParamType, ...) what(ParamType,7), EXTAND_ARGS(FOR_EACH_6(what, VA_ARGS))
define FOR_EACH_8(what,ParamType, ...) what(ParamType,8), EXTAND_ARGS(FOR_EACH_7(what, VA_ARGS))
define FOR_EACH_9(what,ParamType, ...) what(ParamType,9), EXTAND_ARGS(FOR_EACH_8(what, VA_ARGS))
define CONCATENATE(arg1, arg2) arg1##arg2
//Parameters Table
define PARAM_TABLE(ParamType,N) ParamType param##N
define PARAM_TABLE_FOR_EACH_(N,...) EXTAND_ARGS(CONCATENATE(FOR_EACH_, N)(PARAM_TABLE,VA_ARGS))
// Parameters Name
define PARAM_NAME(ParamType,N) param##N
define PARAM_NAME_FOR_EACH_(N,...) EXTAND_ARGS(CONCATENATE(FOR_EACH_, N)(PARAM_NAME,VA_ARGS))
// 计算 VA_ARGS 参数个数,最大支持64个参数 //vs中无参数时会返回1
define FL_ARG_COUNT(...) EXTAND_ARGS(FL_INTERNAL_ARG_COUNT_PRIVATE(0, VA_ARGS,\
64, 63, 62, 61, 60, \
59, 58, 57, 56, 55, 54, 53, 52, 51, 50, \
49, 48, 47, 46, 45, 44, 43, 42, 41, 40, \
39, 38, 37, 36, 35, 34, 33, 32, 31, 30, \
29, 28, 27, 26, 25, 24, 23, 22, 21, 20, \
19, 18, 17, 16, 15, 14, 13, 12, 11, 10, \
9, 8, 7, 6, 5, 4, 3, 2, 1, 0))
define FL_INTERNAL_ARG_COUNT_PRIVATE(\
_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, \
_10, _11, _12, _13, _14, _15, _16, _17, _18, _19, \
_20, _21, _22, _23, _24, _25, _26, _27, _28, _29, \
_30, _31, _32, _33, _34, _35, _36, _37, _38, _39, \
_40, _41, _42, _43, _44, _45, _46, _47, _48, _49, \
_50, _51, _52, _53, _54, _55, _56, _57, _58, _59, \
_60, _61, _62, _63, _64, N, ...) N
define DeclarationAndDifinitionInterface(funcName,...) \
virtual NPSDKError funcName(PARAM_TABLE_FOR_EACH_(EXTAND_ARGS(FL_ARG_COUNT(__VA_ARGS__)),__VA_ARGS__)) \
{
if (pImpl != NULL)
{
return pImpl->funcName(PARAM_NAME_FOR_EACH_(EXTAND_ARGS(FL_ARG_COUNT(VA_ARGS)),VA_ARGS));
}
return NPSDK_ERR_NOT_SUPPORT;
}
define CONCATSTR(...) fun(FL_ARG_COUNT(VA_ARGS),VA_ARGS)
std::string fun(int d,const char* h,...)
{
va_list ap;
va_start(ap,h);
std::string sf =h;
for (int i = 1; i < d; i++)
{
sf += va_arg(ap,const char*);
}
va_end(ap);
return sf.data();
}