diff options
Diffstat (limited to 'content/2024-04-24-fn-wrapper-macro-magic/wrap-v3.cc')
-rw-r--r-- | content/2024-04-24-fn-wrapper-macro-magic/wrap-v3.cc | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/content/2024-04-24-fn-wrapper-macro-magic/wrap-v3.cc b/content/2024-04-24-fn-wrapper-macro-magic/wrap-v3.cc new file mode 100644 index 0000000..26e0913 --- /dev/null +++ b/content/2024-04-24-fn-wrapper-macro-magic/wrap-v3.cc @@ -0,0 +1,30 @@ +#define ARGS0() +#define ARGS1() a0 +#define ARGS2() a1, ARGS1() + +#define TYPEDARGS0() +#define TYPEDARGS1(type) type a0 +#define TYPEDARGS2(type, ...) type a1, TYPEDARGS1(__VA_ARGS__) + +#define MOCK_WRAPPER_IMPL(ret, fn) \ + /* do common work */ \ + static ret wrap_##fn(...); + +// Utility to generate wrapper boilerplate for functions with *one* argument. +#define WRAP1(ret, fn, ty1) \ + MOCK_WRAPPER_IMPL(ret, fn) \ + \ + extern "C" ret fn(TYPEDARGS1(ty1)) { \ + return wrap_##fn(ARGS1()); \ + } + +// Utility to generate wrapper boilerplate for functions with *two* arguments. +#define WRAP2(ret, fn, ty1, ty2) \ + MOCK_WRAPPER_IMPL(ret, fn) \ + \ + extern "C" ret fn(TYPEDARGS2(ty1, ty2)) { \ + return wrap_##fn(ARGS2()); \ + } + +WRAP1(int, foo, const char*) +WRAP2(int, bar, const char*, const char*) |