2 registered members (Akow, tomaslolo),
1,536
guests, and 12
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Useful Macros
#148667
08/17/07 14:34
08/17/07 14:34
|
Joined: Jul 2006
Posts: 783 London, UK
sheefo
OP
User
|
OP
User
Joined: Jul 2006
Posts: 783
London, UK
|
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...
|
|
|
Re: Useful Macros
[Re: TWO]
#148669
08/18/07 17:33
08/18/07 17:33
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
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.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: Useful Macros
[Re: Uhrwerk]
#148670
08/19/07 16:32
08/19/07 16:32
|
Joined: Jul 2006
Posts: 783 London, UK
sheefo
OP
User
|
OP
User
Joined: Jul 2006
Posts: 783
London, UK
|
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
|
|
|
Re: Useful Macros
[Re: sheefo]
#148671
11/19/07 16:48
11/19/07 16:48
|
Joined: Nov 2005
Posts: 94 Texas
3Dski
Junior Member
|
Junior Member
Joined: Nov 2005
Posts: 94
Texas
|
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.
Last edited by 3Dski; 11/19/07 19:44.
|
|
|
Re: Useful Macros
[Re: 3Dski]
#148672
11/19/07 19:57
11/19/07 19:57
|
Joined: Nov 2005
Posts: 94 Texas
3Dski
Junior Member
|
Junior Member
Joined: Nov 2005
Posts: 94
Texas
|
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
Last edited by 3Dski; 11/19/07 20:09.
|
|
|
|