Useful Macros

Posted By: sheefo

Useful Macros - 08/17/07 14:34

I was thinking, perhaps people can post some useful macros definitions here so we can have a collection for everyone to benefit from.

Here is one I came up with, since I found out about using tokens in macros.
It is so much better than using "malloc(sizeof(type))", and in VC++ it is highlighter
See below:

MACRO:
Code:

#define new(type) malloc(sizeof(type##))
#define delete(object) free(object)



USE:
Code:

// Create new object like this...
int *myint = new(int);
// ...Instead of like this.
int *myint = (int*)malloc(sizeof(int));
...
// Lookes better than 'free'
delete(myint);



Now it's your turn...
Posted By: TWO

Re: Useful Macros - 08/17/07 20:21

Not bad mate, I had the same idea some time ago. I only defined a bigger, but in my eyes more useful, delete macro. (error should be replaced by some log function)

#define new(x) malloc(sizeof(x))
#define delete(x) if(x!=NULL) free(x); else error("Memory Access Error!")
Posted By: Uhrwerk

Re: Useful Macros - 08/18/07 17:33

Here is my assert macro:
Code:
#ifdef ASSERTIONS
#define assert(condition,message) if(!(condition)) printf("Assertion failed: %s",&message)
#else
#define assert(condition,message) ;
#endif



If you now write #define ASSERTIONS you can use the assert macro as you would use as any other function. If you delete the #define ASSERTIONS command from your main file all assertions in your code will be ignored.

For those who don't know what an assertion is: An assertion is a assumption you do while programming. Let's imaging you had a function that takes an int as a parameter that should never be 0. If you write as the very first instruction in your function assert(MyInt != 0,"Passed 0 to MyFunction"); you will be notified whenever your assumption is proven wrong. Assertions do not influence the runtime speed of your application as you can deactivate them when publishing your game.

Unfortunately the __line__ and __file__ macros are not yet implemented. They would greatly contribute to the assert function.
Posted By: sheefo

Re: Useful Macros - 08/19/07 16:32

This isn't that useful, but it works.
If you need to initialze a variable in a for loop, you can call a custom loop, like this:

Code:

#define loop(i,j,k) i##;for(;j;k)
...
loop(int i = 0, i < n, i++) { ... }





Another one, this one is a really cool one. This one I use a lot of the time.
An easy way to loop through linked lists.

Code:

#define lListLoop(list, node) for(node = list.start; node != NULL; node = node->next)
...
lListLoop(MyLinkedList, node)
{
i = node->data;
...
}




PS: Thanks guys
Posted By: 3Dski

Re: Useful Macros - 11/19/07 16:48

Quick macro related question. Excuse me if I missed something in the manual. Is there a way to view a preprocess code listing to view macro expansions for debugging them?

Editted: I also tried a similar syntax as the "assert" macro above, with ";" as being the only replacement text when "assertions" is not defined and this doesn't seem to work under Lite-C pure mode.
Posted By: 3Dski

Re: Useful Macros - 11/19/07 19:57

Okay... I puzzled over the assertion macro posted above, and the replacement text of ";" for the #ifndef side doesn't seem to work under Lite-C pure mode. I've written some logging related macros that use a work around for the #ifndef side:

EDITTED: I think it best to add a reminder that logging is on, so I added some addtional code to show that logging is on and when the log file is closed.
Code:

#define LOGGING

#ifdef LOGGING
#define START_LOGGER(n) printf("Logging is on.");FILE* LOG_HANDLE = fopen( n, "w"); if (LOG_HANDLE == NULL) error("Couldn't open log file");
#define LOG(log_msg) fprintf( LOG_HANDLE, "%0.2d:%0.2d:%0.2d - %s\n", (int)sys_hours, (int)sys_minutes, (int)sys_seconds, (char*)#log_msg );
#define CLOSE_LOGGER if ( LOG_HANDLE != NULL) fclose(LOG_HANDLE);printf("Log closed.");
#else
#define COMMENT //
#define START_LOGGER(n) COMMENT
#define LOG(log_msg) COMMENT
#define CLOSE_LOGGER COMMENT
#endif

...
START_LOGGER("test.log")
...
LOG( "start_panel" )
...
CLOSE_LOGGER



© 2024 lite-C Forums