Home > Back-end >  C macro time arithmetic
C macro time arithmetic

Time:11-28

I'm working on a wgl loader and typedef'd each openGL function that I use like this:

/*Let's say I'm defining n functions*/ 
typedef return_t (*f1)(params)
f1 _glFunc1;
#define glFunc1(params) _glFunc1(params)
...
typedef return_t (*fn)(params)
fn _glFuncn;
#define glFuncn(params) _glFuncn(params)

Then to get the definitions of these functions I have to use wglGetProcAddress or GetProcAddress and then cast the result to f1, f2 ... I tried to automate the casting with this macro:

#define GetFuncDef(glFunc) _##glFunc = (f##(__LINE__ - startingLineNumber   1))GetProcAddress(#glFunc)

Where startingLineNumber is the first line that I use this macro in(in my case it's 22), but the preprocessor does not compute __LINE__ - startingLineNumber.

Is there some way to force it to do so?

EDIT: startingLineNumber isn't a variable, macro etc. It's written out as a literal number in my code. like this: #define GetFuncDef(glFunc) _##glFunc = (f##(__LINE__ - 22 1))GetProcAddress(#glFunc), where 22 would be startingLineNumber

CodePudding user response:

Similar to C Preprocessor output int at Build first you have to implement operations:

typedef return_t (*f1)(params)
typedef return_t (*f2)(params)
void *GetProcAddress(charr *);

#define SUB_10_0   10
#define SUB_10_1   9
#define SUB_10_2   8
// etc. for each each possible combination, 1000 of lines ...
#define SUB_21_20  1
// etc. for each each possible combination, 1000 of lines ...
#define SUB_IN(a, b)  SUB_##a##_##b
#define SUB(a, b)  SUB_IN(a, b)


#define CONCAT_IN(a, b)  a##b
#define CONCAT(a, b)     CONCAT_IN(a, b)

#define startingLineNumber 20
#define GetFuncDef(glFunc) (CONCAT(f, SUB(__LINE__, startingLineNumber)))GetProcAddress(#glFunc)
int main() {
    f1 a = GetFuncDef();
}

Then gcc -E outputs:

typedef return_t (*f1)(params)
typedef return_t (*f2)(params)
void *GetProcAddress(charr *);
int main() {
    f1 a = (f1)GetProcAddress("");
}

In a similar fashion mentioned here Boost and P99 libraries can be used.

  • Related