Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Anyone keep a 'bag of tricks'?

Anyone keep a 'bag of tricks'?

Scheduled Pinned Locked Moved The Lounge
3 Posts 3 Posters 0 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Michael Dunn
    wrote on last edited by
    #1

    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 a CString 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 the CTASSERT 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/

    D C 2 Replies Last reply
    0
    • M Michael Dunn

      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 a CString 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 the CTASSERT 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/

      D Offline
      D Offline
      David Wulff
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • M Michael Dunn

        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 a CString 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 the CTASSERT 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/

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        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));
        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups