bgfx源码里的几个宏

Posted redips-l

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bgfx源码里的几个宏相关的知识,希望对你有一定的参考价值。

0. vs 查看预编译后的代码

技术图片

技术图片

 

1. # 和 ##

#define P(A) { printf(" the square of %s is %d
", #A, A * A); }
#define Q(B) { printf(" the square of "#B" is %d
" , B * B); }

#define Handle(_name) struct _name##Handle { unsigned int idx; };
Handle(Shader);
Handle(VertexBuffer);
Handle(IndexBuffer);

int main() {
    P(16);
    Q(25);
    IndexBufferHandle ibh; ibh.idx = 0;
    return 0;
}

上面代码会展开成:

struct ShaderHandle { unsigned int idx; };;
struct VertexBufferHandle { unsigned int idx; };;
struct IndexBufferHandle { unsigned int idx; };;

int main() {
    { printf(" the square of %s is %d
", "16", 16 * 16); };

    { printf(" the square of ""25"" is %d
" , 25 * 25); };

    IndexBufferHandle ibh; ibh.idx = 0;   

    return 0;  
}

2. BX_VA_ARGS_COUNT(...) -> 替换成参数列表的参数个数

#if BX_COMPILER_MSVC
// Workaround MSVS bug...
#    define BX_VA_ARGS_PASS(...) BX_VA_ARGS_PASS_1_ __VA_ARGS__ BX_VA_ARGS_PASS_2_
#    define BX_VA_ARGS_PASS_1_ (
#    define BX_VA_ARGS_PASS_2_ )
#else
#    define BX_VA_ARGS_PASS(...) (__VA_ARGS__)
#endif // BX_COMPILER_MSVC

#define BX_VA_ARGS_COUNT(...) BX_VA_ARGS_COUNT_ BX_VA_ARGS_PASS(__VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#define BX_VA_ARGS_COUNT_(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10, _a11, _a12, _a13, _a14, _a15, _a16, _last, ...) _last

int main() {
    auto v = BX_VA_ARGS_COUNT(42, 11);
    std::cout << v << std::endl;
    return 0;
}

 

 预编译后:

int main() {
   auto v = 2;
   std::cout << v << std::endl;
   return 0;      
}

 3. BX_UNUSED(...)

#define BX_MACRO_BLOCK_BEGIN for(;;) {
#define BX_MACRO_BLOCK_END break; }
#define BX_NOOP(...) BX_MACRO_BLOCK_BEGIN BX_MACRO_BLOCK_END

#define BX_PRAGMA_DIAGNOSTIC_PUSH              BX_PRAGMA_DIAGNOSTIC_PUSH_MSVC_
#define BX_PRAGMA_DIAGNOSTIC_POP               BX_PRAGMA_DIAGNOSTIC_POP_MSVC_
#define BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC(_x)

#define BX_PRAGMA_DIAGNOSTIC_PUSH_MSVC_()     __pragma(warning(push) )
#define BX_PRAGMA_DIAGNOSTIC_POP_MSVC_()      __pragma(warning(pop) )
#define BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(_x) __pragma(warning(disable:_x) )

#define BX_MACRO_DISPATCHER(_func, ...) BX_MACRO_DISPATCHER_1_(_func, BX_VA_ARGS_COUNT(__VA_ARGS__) )
#define BX_MACRO_DISPATCHER_1_(_func, _argCount) BX_MACRO_DISPATCHER_2_(_func, _argCount)
#define BX_MACRO_DISPATCHER_2_(_func, _argCount) BX_CONCATENATE(_func, _argCount)

#define BX_CONCATENATE(_x, _y) BX_CONCATENATE_(_x, _y)
#define BX_CONCATENATE_(_x, _y) _x ## _y

#if BX_COMPILER_MSVC
// Workaround MSVS bug...
#    define BX_UNUSED(...) BX_MACRO_DISPATCHER(BX_UNUSED_, __VA_ARGS__) BX_VA_ARGS_PASS(__VA_ARGS__)
#else
#    define BX_UNUSED(...) BX_MACRO_DISPATCHER(BX_UNUSED_, __VA_ARGS__)(__VA_ARGS__)
#endif // BX_COMPILER_MSVC

#define BX_UNUSED_1(_a1)                                              
    BX_MACRO_BLOCK_BEGIN                                                      BX_PRAGMA_DIAGNOSTIC_PUSH();                                          /*BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wuseless-cast");*/         (void)(true ? (void)0 : ( (void)(_a1) ) );                            BX_PRAGMA_DIAGNOSTIC_POP();                                       BX_MACRO_BLOCK_END

#define BX_UNUSED_2(_a1, _a2) BX_UNUSED_1(_a1); BX_UNUSED_1(_a2)
#define BX_UNUSED_3(_a1, _a2, _a3) BX_UNUSED_2(_a1, _a2); BX_UNUSED_1(_a3)
#define BX_UNUSED_4(_a1, _a2, _a3, _a4) BX_UNUSED_3(_a1, _a2, _a3); BX_UNUSED_1(_a4)
#define BX_UNUSED_5(_a1, _a2, _a3, _a4, _a5) BX_UNUSED_4(_a1, _a2, _a3, _a4); BX_UNUSED_1(_a5)
#define BX_UNUSED_6(_a1, _a2, _a3, _a4, _a5, _a6) BX_UNUSED_5(_a1, _a2, _a3, _a4, _a5); BX_UNUSED_1(_a6)
#define BX_UNUSED_7(_a1, _a2, _a3, _a4, _a5, _a6, _a7) BX_UNUSED_6(_a1, _a2, _a3, _a4, _a5, _a6); BX_UNUSED_1(_a7)
#define BX_UNUSED_8(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8) BX_UNUSED_7(_a1, _a2, _a3, _a4, _a5, _a6, _a7); BX_UNUSED_1(_a8)
#define BX_UNUSED_9(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9) BX_UNUSED_8(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8); BX_UNUSED_1(_a9)
#define BX_UNUSED_10(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10) BX_UNUSED_9(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9); BX_UNUSED_1(_a10)
#define BX_UNUSED_11(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10, _a11) BX_UNUSED_10(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10); BX_UNUSED_1(_a11)
#define BX_UNUSED_12(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10, _a11, _a12) BX_UNUSED_11(_a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8, _a9, _a10, _a11); BX_UNUSED_1(_a12)

int main() {
    BX_UNUSED(42);
    return 0;
}

 

 展开成:

int main() {
    for(;;) { __pragma(warning(push) ); (void)(true ? (void)0 : ( (void)(42) ) ); __pragma(warning(pop) ); break; };
    return 0;  
}

 其中 #pragma warning( push / pop )的作用是:暂存或者恢复编译器的警告状态,比如:

#pragma warning( push )
#pragma warning( disable : 4705 )
#pragma warning( disable : 4706 )
#pragma warning( disable : 4707 )
// Some code
#pragma warning( pop ) 

 // Some code这块代码不会报4705-4707这几个错误

 

以上是关于bgfx源码里的几个宏的主要内容,如果未能解决你的问题,请参考以下文章

嵌入式开发中100%会用的几个宏,建议收藏

常用的几个JQuery代码片段

BGFX 渲染引擎中着色器代码的调试方法

xmlreader与xmlwriter里的几个坑与解决方案

Python里的几个字符串处理函数

iOS小菜那些年写过的宏文件