I'm trying to generate a bunch of types off a simple template using macros to remove some of the copy and paste work this would usually require but I've ran into a bit of a problem. These are my macros to generate the types:
#define VEC2_TYPES_LIST(X) \
X(Vec2f, float, ) \
X(Vec2d, double, ) \
X(Vec2u, uint32_t, ) \
X(Vec2i, int32_t, )
#define X_TYPE(u, t, s) \
typedef union u \
{ \
struct \
{ \
t x; \
t y; \
}; \
\
struct \
{ \
t elem[2]; \
}; \
\
s \
} u;
VEC2_TYPES_LIST(X_TYPE)
The problem is that I want to create common functions to these types that will be dynamically dispatched at runtime through a vtable. So as well as making the function definitions I also wanted to put them into a structure as function pointers. Something like this:
#define VEC2_FUNCTIONS_LIST( ?? )
X(u, u##_add, (u a, u b)) // return type, function name, params
To generate this code:
Vec2f vec2f_add(Vec2f a, Vec2f b);
...
Vec2i vec2i_add(Vec2i a, Vec2i b);
struct Vec2fVTable
{
Vec2f (*vec2f_add)(Vec2f a, Vec2f b);
};
// same for other types
I have no idea how to go about doing this so I'd appreciate if someone could teach me.