It is currently Sun Jul 22, 2018 6:12 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 18 posts ]  Go to page Previous  1, 2
Author Message
PostPosted: Mon Jun 25, 2018 12:34 am 
Offline
User avatar

Joined: Sat Sep 07, 2013 2:59 pm
Posts: 1648
Banshaku wrote:
without passing parameter from the C side and how much it would be worth it.

I do it like this:
Code:
void MyFunction(int a, int b, int c);

becomes:
Code:
int MyFunction_A;
int MyFunction_B;
int MyFunction_C;

void MyFunction_(void);

#define MyFunction(a, b, c)\
{\
    MyFunction_A = a;\
    MyFunction_B = b;\
    MyFunction_C = c;\
    MyFunction_();\
}

_________________
Available now: My game "City Trouble".
Website: https://megacatstudios.com/products/city-trouble
Trailer: https://youtu.be/IYXpP59qSxA
Gameplay: https://youtu.be/Eee0yurkIW4
German Retro Gamer article: http://i67.tinypic.com/345o108.jpg


Top
 Profile  
 
PostPosted: Mon Jun 25, 2018 6:48 am 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 20283
Location: NE Indiana, USA (NTSC)
You appear to be using a preprocessor macro as a workaround for lack of static inline. For this to work, you will need to change the { and } in your macro to do { and } while (0) respectively in order to keep the following semicolon from interfering with the parsing of if...else constructions. Otherwise, the macro will expand undesirably as follows:
Code:
if (some_condition)
    MyFunction(a, b, c);
else
    SomethingElse();

/* becomes, after preprocessing and reindenting */

if (some_condition)
    {
        MyFunction_A = a;
        MyFunction_B = b;
        MyFunction_C = c;
        MyFunction_();
    }

;  /* This ends the if statement because it is something other than else that directly follows its first statement or compound statement. */
else  /* This is a syntax error because it does not follow an open if statement. */
    SomethingElse();

/* But with my suggestion, it would become */

if (some_condition)
    do {
        MyFunction_A = a;
        MyFunction_B = b;
        MyFunction_C = c;
        MyFunction_();
    } while (0);  /* the do...while swallows up the semicolon */
else  /* properly recognized as belonging to the if statement */
    SomethingElse();


Top
 Profile  
 
PostPosted: Mon Jun 25, 2018 8:02 am 
Offline
User avatar

Joined: Sat Sep 07, 2013 2:59 pm
Posts: 1648
Yeah, I'm already doing this.
Or rather, I did macros for this as well:
Code:
#define MACRO_START do {
#define MACRO_END } while (false)


I used the brackets in the example for simplicity's sake.

One thing to keep in mind in this construct:
If you ever feel the desire to include a break out of a loop inside a macro, you cannot do this if the loop exists outside the macro:
Code:
#define IfErrorThenBreak(errorCode)\
do\
{\
    if ((errorCode) != 0)\
        break; /* Will not work as intended since it refers\
                  to the do while loop, not to some outside loop. */\
}\
while (false)

_________________
Available now: My game "City Trouble".
Website: https://megacatstudios.com/products/city-trouble
Trailer: https://youtu.be/IYXpP59qSxA
Gameplay: https://youtu.be/Eee0yurkIW4
German Retro Gamer article: http://i67.tinypic.com/345o108.jpg


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 18 posts ]  Go to page Previous  1, 2

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group