Anyone keep a 'bag of tricks'?
-
I was just thinking about some nifty macros or other little snippets I've come up with or seen during my years, and was wondering if anyone else had something similar. The first one that comes to mind is the macro
_S()
which loads aCString
from the string table inline. You'd use it in a place where you just need to grab a string from the string table and use it immediately, such as:MessageBox ( _S(IDS_FILEOPEN_FAILED), _S(IDS_MYAPP_TITLE), MB_ICONERROR );
_S()
is defined in release builds as:#define _S(id) (CString((LPCTSTR)(id)))
and in debug builds as:CString _S(UINT uID) { CString sRet; ASSERT ( 0 != sRet.LoadString(uID) ); return sRet; }
That way you get the assert checking in debug builds, but it's streamlined in release builds. Another one I got from a co-worker is a "compile-time assert":#define CTASSERT(x) typedef qwertyuiop int[x]
where "x" is some boolean or unsigned-int expression. If the expression evaluates to zero (false), the compiler will give an error at theCTASSERT
line because it'll try to reference a zero-length array. You'd use this type of assert for compile-time checks. For example, if you are doing some text case conversion manually, and you want to be sure the compiler is using the ASCII ordering you expect, you'd do:CTASSERT( 'A' < 'a' );
The nice thing is that this type of assert generates no code. (I know that's a pretty contrived example, but it was the first thing I could think of. :) ) --Mike-- http://home.inreach.com/mdunn/ -
I was just thinking about some nifty macros or other little snippets I've come up with or seen during my years, and was wondering if anyone else had something similar. The first one that comes to mind is the macro
_S()
which loads aCString
from the string table inline. You'd use it in a place where you just need to grab a string from the string table and use it immediately, such as:MessageBox ( _S(IDS_FILEOPEN_FAILED), _S(IDS_MYAPP_TITLE), MB_ICONERROR );
_S()
is defined in release builds as:#define _S(id) (CString((LPCTSTR)(id)))
and in debug builds as:CString _S(UINT uID) { CString sRet; ASSERT ( 0 != sRet.LoadString(uID) ); return sRet; }
That way you get the assert checking in debug builds, but it's streamlined in release builds. Another one I got from a co-worker is a "compile-time assert":#define CTASSERT(x) typedef qwertyuiop int[x]
where "x" is some boolean or unsigned-int expression. If the expression evaluates to zero (false), the compiler will give an error at theCTASSERT
line because it'll try to reference a zero-length array. You'd use this type of assert for compile-time checks. For example, if you are doing some text case conversion manually, and you want to be sure the compiler is using the ASCII ordering you expect, you'd do:CTASSERT( 'A' < 'a' );
The nice thing is that this type of assert generates no code. (I know that's a pretty contrived example, but it was the first thing I could think of. :) ) --Mike-- http://home.inreach.com/mdunn/I use one fairly frequently, as I tend to use message boxes a lot to check that code is being executed in the correct order, if at all, and to check viariables, etc. It simply 'wraps' the MessageBox function to mean less typing:
#ifdef _MSGS #define MSG(s) MessageBox(0, s, "Debug", ID_OK) #else #define MSG(s) 0 #endif
I'll use your_S(...)
macro from now on though, as I usually just create a new CString each time. David Wulff -
I was just thinking about some nifty macros or other little snippets I've come up with or seen during my years, and was wondering if anyone else had something similar. The first one that comes to mind is the macro
_S()
which loads aCString
from the string table inline. You'd use it in a place where you just need to grab a string from the string table and use it immediately, such as:MessageBox ( _S(IDS_FILEOPEN_FAILED), _S(IDS_MYAPP_TITLE), MB_ICONERROR );
_S()
is defined in release builds as:#define _S(id) (CString((LPCTSTR)(id)))
and in debug builds as:CString _S(UINT uID) { CString sRet; ASSERT ( 0 != sRet.LoadString(uID) ); return sRet; }
That way you get the assert checking in debug builds, but it's streamlined in release builds. Another one I got from a co-worker is a "compile-time assert":#define CTASSERT(x) typedef qwertyuiop int[x]
where "x" is some boolean or unsigned-int expression. If the expression evaluates to zero (false), the compiler will give an error at theCTASSERT
line because it'll try to reference a zero-length array. You'd use this type of assert for compile-time checks. For example, if you are doing some text case conversion manually, and you want to be sure the compiler is using the ASCII ordering you expect, you'd do:CTASSERT( 'A' < 'a' );
The nice thing is that this type of assert generates no code. (I know that's a pretty contrived example, but it was the first thing I could think of. :) ) --Mike-- http://home.inreach.com/mdunn/i use this one all the time:
#define ARRAYSIZE(__a) (sizeof(__a)/sizeof(__a[0]))
int array[] = {5,3,2,5,6,2,1,5,7,9,0};
...
int sizeofarray = ARRAYSIZE(array);and, this one is fun:
// instatiate one of these and it diables all child controls in a
// window. let it go out of scope and all the controls are re-enabled.
class CDisableChildControls
{
public :
CDisableChildControls() {ASSERT(0);}CDisableChildControls(HWND h) { ASSERT(h); m\_hWnd = h; EnableChildWindows(m\_hWnd,FALSE); } ~CDisableChildControls() { ASSERT(m\_hWnd); EnableChildWindows(m\_hWnd, TRUE); }
protected:
HWND m_hWnd;
};BOOL CALLBACK EnumEnableChildWndProc( HWND hwnd, LPARAM lParam)
{
::EnableWindow(hwnd, (lParam ? TRUE : FALSE));
return TRUE;
}
BOOL EnableChildWindows(HWND hWndParent, BOOL bEnable)
{
return EnumChildWindows(hWndParent, EnumEnableChildWndProc, (bEnable ? 1 : 0));
}