(C++) Trick or Treat ( Make/Expand our tricks.h file) [modified]
-
Okay, Haloween is coming and I'm trying to get in the spirit of the thing. So I figured we could swap treats that are tricks. I've got a file tricks.h that I include at the bottom of my stdafx.h header file that I stuff with neat little programming tricks. The idea is that they are all one or just a few lines, no multipage functions. Anyhow, I figured I'd share my best and my easiest trick treats and see if anyone else will reciprocate. My best trick:
#ifdef TRACE #undef TRACE #define TRACE(s, ...) ATLTRACE((LPCSTR)(CStringA("%s(%d) : ")+s),__FILE__,__LINE__,__VA_ARGS__) #endif
Now, I'm not one who's really fond of macros... Some programmers like to express the size of their manhood by how complicated and obfuscated their macros are. I'm not one of those coders. I only use a complicated macro when nothing else will get the job done as easily. Originally I had a full page function and a few macros that I replaced with this thing when Visual C++ got support for variadic macros. Anyhow what use is it? Well, when Visual C++ is compiling, it will spit out error messages into the debug window, something like this:
c:\programming\orgprintview.cpp(32) : error C2143: syntax error : missing ';' before '!'
When you hit F4 or doubleclick on that line, the IDE will deliver you to the file and line that generated the error. It turns out that if you format your TRACE statements to print "file(line) : message", then you can doubleclick in the output window on that line and the IDE will take you to that location. Pretty nifty. So those 4 lines when added to your tricks.h file will change all TRACE statements to do just that. Note that it only works on the char version of TRACE, not the wchar or tchar. that's all I use though so it's fine by me. My simplest tricks.h entry is this:
#define IDENTICAL 0 // use: if(IDENTICAL==strcmp(a,b)) {;}
And the comment shows it's usage... I try as hard as possible to have my code read like english (so long as it doesnt get more complicated), and this easy macro helps a bit :) Anyhow, Happy Haloween, and hit me back with something to add to my tricks.h Dan K (p.s. I'm looking for a good paying job anywhere in the US, or abroad actually, hook me up :))
modified on Monday, October 12, 2009 8:32 PM
-
Okay, Haloween is coming and I'm trying to get in the spirit of the thing. So I figured we could swap treats that are tricks. I've got a file tricks.h that I include at the bottom of my stdafx.h header file that I stuff with neat little programming tricks. The idea is that they are all one or just a few lines, no multipage functions. Anyhow, I figured I'd share my best and my easiest trick treats and see if anyone else will reciprocate. My best trick:
#ifdef TRACE #undef TRACE #define TRACE(s, ...) ATLTRACE((LPCSTR)(CStringA("%s(%d) : ")+s),__FILE__,__LINE__,__VA_ARGS__) #endif
Now, I'm not one who's really fond of macros... Some programmers like to express the size of their manhood by how complicated and obfuscated their macros are. I'm not one of those coders. I only use a complicated macro when nothing else will get the job done as easily. Originally I had a full page function and a few macros that I replaced with this thing when Visual C++ got support for variadic macros. Anyhow what use is it? Well, when Visual C++ is compiling, it will spit out error messages into the debug window, something like this:
c:\programming\orgprintview.cpp(32) : error C2143: syntax error : missing ';' before '!'
When you hit F4 or doubleclick on that line, the IDE will deliver you to the file and line that generated the error. It turns out that if you format your TRACE statements to print "file(line) : message", then you can doubleclick in the output window on that line and the IDE will take you to that location. Pretty nifty. So those 4 lines when added to your tricks.h file will change all TRACE statements to do just that. Note that it only works on the char version of TRACE, not the wchar or tchar. that's all I use though so it's fine by me. My simplest tricks.h entry is this:
#define IDENTICAL 0 // use: if(IDENTICAL==strcmp(a,b)) {;}
And the comment shows it's usage... I try as hard as possible to have my code read like english (so long as it doesnt get more complicated), and this easy macro helps a bit :) Anyhow, Happy Haloween, and hit me back with something to add to my tricks.h Dan K (p.s. I'm looking for a good paying job anywhere in the US, or abroad actually, hook me up :))
modified on Monday, October 12, 2009 8:32 PM
For quick loading of string resources:
#define _S(id) (CString(LPCTSTR(id)))
And use it like this:
MessageBox ( hwnd, _S(IDS_TITLE), _S(IDS_MESSAGE), MB_OK );
This constructor trick is actually documented, although I haven't seen it used too often. Note that this macro is the release version; the debug version is a real function that checks whether the
LoadString()
succeeds.--Mike-- Dunder-Mifflin, this is Pam.
modified on Thursday, October 22, 2009 6:36 PM
-
Okay, Haloween is coming and I'm trying to get in the spirit of the thing. So I figured we could swap treats that are tricks. I've got a file tricks.h that I include at the bottom of my stdafx.h header file that I stuff with neat little programming tricks. The idea is that they are all one or just a few lines, no multipage functions. Anyhow, I figured I'd share my best and my easiest trick treats and see if anyone else will reciprocate. My best trick:
#ifdef TRACE #undef TRACE #define TRACE(s, ...) ATLTRACE((LPCSTR)(CStringA("%s(%d) : ")+s),__FILE__,__LINE__,__VA_ARGS__) #endif
Now, I'm not one who's really fond of macros... Some programmers like to express the size of their manhood by how complicated and obfuscated their macros are. I'm not one of those coders. I only use a complicated macro when nothing else will get the job done as easily. Originally I had a full page function and a few macros that I replaced with this thing when Visual C++ got support for variadic macros. Anyhow what use is it? Well, when Visual C++ is compiling, it will spit out error messages into the debug window, something like this:
c:\programming\orgprintview.cpp(32) : error C2143: syntax error : missing ';' before '!'
When you hit F4 or doubleclick on that line, the IDE will deliver you to the file and line that generated the error. It turns out that if you format your TRACE statements to print "file(line) : message", then you can doubleclick in the output window on that line and the IDE will take you to that location. Pretty nifty. So those 4 lines when added to your tricks.h file will change all TRACE statements to do just that. Note that it only works on the char version of TRACE, not the wchar or tchar. that's all I use though so it's fine by me. My simplest tricks.h entry is this:
#define IDENTICAL 0 // use: if(IDENTICAL==strcmp(a,b)) {;}
And the comment shows it's usage... I try as hard as possible to have my code read like english (so long as it doesnt get more complicated), and this easy macro helps a bit :) Anyhow, Happy Haloween, and hit me back with something to add to my tricks.h Dan K (p.s. I'm looking for a good paying job anywhere in the US, or abroad actually, hook me up :))
modified on Monday, October 12, 2009 8:32 PM
I got the idea for this from the CFMsg localization utility from codeproject. I really liked being able to insert formatted text into function parameters without having to create a CString variable or a TCHAR buffer in the line above.
// formatted string
struct sf {
enum { MAX = 500 };
TCHAR buffer[MAX];
sf( LPCTSTR fmt, ... ) {
va_list args;
va_start( args, fmt );
_vsntprintf(buffer, MAX, fmt, args);
va_end( args );
}
operator LPTSTR () { return buffer; }
};....
MessageBox(hWnd, sf("The current date is %d/%d/%d", m, d, y), lpCaption, MB_OK);
...
FILE* f = fopen(sf("file-%d.%s", num, extension), "r");
...
if (system(sf("copy %s %s", src, dest)))
MessageBox(hWnd, sf("Error - %s could not be copied to %s", src, dest), lpCaption, MB_OK);You have to remember not to have code like
LPCTSTR text = sf("%d", number);
though, or you'll end up with a dangling pointer, and it has a limit to how much text you can put in it, but I can live with that.There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal
-
I got the idea for this from the CFMsg localization utility from codeproject. I really liked being able to insert formatted text into function parameters without having to create a CString variable or a TCHAR buffer in the line above.
// formatted string
struct sf {
enum { MAX = 500 };
TCHAR buffer[MAX];
sf( LPCTSTR fmt, ... ) {
va_list args;
va_start( args, fmt );
_vsntprintf(buffer, MAX, fmt, args);
va_end( args );
}
operator LPTSTR () { return buffer; }
};....
MessageBox(hWnd, sf("The current date is %d/%d/%d", m, d, y), lpCaption, MB_OK);
...
FILE* f = fopen(sf("file-%d.%s", num, extension), "r");
...
if (system(sf("copy %s %s", src, dest)))
MessageBox(hWnd, sf("Error - %s could not be copied to %s", src, dest), lpCaption, MB_OK);You have to remember not to have code like
LPCTSTR text = sf("%d", number);
though, or you'll end up with a dangling pointer, and it has a limit to how much text you can put in it, but I can live with that.There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal
I have a FormatCString(LPCTSTR lpszFmt, ...) that bakes a CString using it's method FormatV(). Don't have to deal with arbitrary upper limits that way.
-- Kein Mitleid Für Die Mehrheit