aboutsummaryrefslogtreecommitdiffhomepage
path: root/content/2024-04-24-fn-wrapper-macro-magic/wrap-v3.cc
blob: 26e091366acb5dfebbffadf5b677a1d8a834c05b (plain) (blame)
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
#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*)