Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ macro time arithmetic

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading