I have several functions in my program that look like this:

void foo(int x, int y)


Now I want my program to take a string that looks like:

foo(3, 5)


And execute the corresponding function. What's the most straightforward way to implement this?

When I say straightforward, I mean reasonably extensible and elegant, but it shouldn't take too long to code up.

Edit:

While using a real scripting language would of course solve my problem, I'd still like to know if there is a quick way to implement this in pure C++.


Question: Show tooltip on invalid input in edit control
Answer: As a follow-up to comments regarding the use of the SetProp function to remove the need to hold onto a pair of globals for the tool-tip data, I present the following solution.

Note: By error-checking on calls to GetProp, I've designed a WndProc for the subclassed edit control that would function regardless of whether or not it was desired to make use of tool-tips. If the property isn't found, I simply omit any tool-tip handling code.

Note 2: One downside to all of the available approaches to making the tooltip info non-global is that it introduces coupling between the subclassed WndProc and the parent window's wndProc.


By using dwRefData, one must check that it holds a non-NULL
pointer.
By using SetWindowLongPtr, one must remember an index into the
user-data.
By using SetProp, one must remember a textual property name. I find
this easier.


Removing the call to SetProp removes the tool-tip functionality. I.e you could use the same subclassed wndProc for edit controls whether they took advantage of tooltips or not.

Anyhoo, on with the (Code::Blocks) code.

#define _WIN32_IE 0x0500
#define _WIN32_WINNT 0x0501

#if defined(UNICODE) &amp;&amp; !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) &amp;&amp; !defined(UNICODE)
    #define UNICODE
#endif

#include &lt;tchar.h&gt;
#include &lt;windows.h&gt;
#include &lt;windowsx.h&gt;
#include &lt;commctrl.h&gt;
#include &lt;ctype.h&gt;
#include &lt;cstdio&gt;

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");



HWND g_hwndTT;
TOOLINFO g_ti;
typedef struct mToolTipInfo
{
    HWND hwnd;
    TOOLINFO tInfo;
} * p_mToolTipInfo;


LRESULT CALLBACK EditSubProc ( HWND hwnd, UINT message,
    WPARAM wParam, LPARAM lParam,
    UINT_PTR uIdSubclass, DWORD_PTR dwRefData )
{
    p_mToolTipInfo tmp = (p_mToolTipInfo)GetProp(hwnd, _T("tipData"));

    switch (message)
    {
    case WM_CHAR:
        {
            POINT pt;

            if( ! isdigit( wParam ) )  // if not a number pop a tooltip!
            {
                if (GetCaretPos(&amp;pt))  // here comes the problem
                {
                    // coordinates are not good, so tooltip is misplaced
                    ClientToScreen( hwnd, &amp;pt );

                    RECT lastCharRect;
                    lastCharRect.left = lastCharRect.top = 0;
                    lastCharRect.right = lastCharRect.bottom = 32;

                    HDC editHdc;
                    char lastChar;
                    int charHeight, charWidth;

                    lastChar = (char)wParam;
                    editHdc = GetDC(hwnd);
                    charHeight = DrawText(editHdc, &amp;lastChar, 1, &amp;lastCharRect, DT_CALCRECT);
                    charWidth = lastCharRect.right;
                    ReleaseDC(hwnd, editHdc);

                    //pt.x += xOfs + charWidth; // invalid char isn't drawn, so no need to advance xPos to reflect width of last char
                    pt.y += charHeight;

                    if (tmp)
                    {
                        SendMessage(tmp-&gt;hwnd, TTM_TRACKACTIVATE, TRUE, (LPARAM)&amp;tmp-&gt;tInfo);
                        SendMessage(tmp-&gt;hwnd, TTM_TRACKPOSITION, 0, MAKELPARAM(pt.x, pt.y));
                    }
                }
                return FALSE;
            }
            else
            {
                if (tmp)
                    SendMessage(tmp-&gt;hwnd, TTM_TRACKACTIVATE,
                    FALSE, (LPARAM)&amp;tmp-&gt;tInfo  );
                return ::DefSubclassProc( hwnd, message, wParam, lParam );
            }
        }
        break;

    case WM_DESTROY:
        {
            p_mToolTipInfo tmp = (p_mToolTipInfo)GetProp(hwnd, _T("tipData"));
            if (tmp)
            {
                delete(tmp);
                RemoveProp(hwnd, _T("tipData"));
            }
        }
        return 0;

    case WM_NCDESTROY:
        ::RemoveWindowSubclass( hwnd, EditSubProc, 0 );
        return DefSubclassProc( hwnd, message, wParam, lParam);
        break;
    }
    return DefSubclassProc( hwnd, message, wParam, lParam);
}






HINSTANCE hInst;

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&amp;wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           _T("Code::Blocks Template Windows App"),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&amp;messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&amp;messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&amp;messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */
LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
        {
            HWND hEdit = CreateWindowEx( 0, _T("EDIT"), _T("edit"), WS_CHILD | WS_VISIBLE |
                WS_BORDER | ES_CENTER, 150, 150, 100, 30, hWnd, (HMENU)1000, hInst, 0 );

            p_mToolTipInfo tmp = new mToolTipInfo;
            SetProp(hEdit, _T("tipData"), tmp);

            // try with tooltip
            //g_hwndTT = CreateWindow(TOOLTIPS_CLASS, NULL,
            tmp-&gt;hwnd = CreateWindow(TOOLTIPS_CLASS, NULL,
                WS_POPUP | TTS_ALWAYSTIP | TTS_BALLOON,
                0, 0, 0, 0, hWnd, NULL, hInst, NULL);

            //if( !g_hwndTT )
            if( !tmp-&gt;hwnd )
                MessageBeep(0);  // just to signal error somehow

//            g_ti.cbSize = sizeof(TOOLINFO);
//            g_ti.uFlags = TTF_TRACK | TTF_ABSOLUTE;
//            g_ti.hwnd = hWnd;
//            g_ti.hinst = hInst;
//            g_ti.lpszText = _T("Hi there");
            tmp-&gt;tInfo.cbSize = sizeof(TOOLINFO);
            tmp-&gt;tInfo.uFlags = TTF_TRACK | TTF_ABSOLUTE;
            tmp-&gt;tInfo.hwnd = hWnd;
            tmp-&gt;tInfo.hinst = hInst;
            tmp-&gt;tInfo.lpszText = _T("Hi there");

//            if( ! SendMessage(g_hwndTT, TTM_ADDTOOL, 0, (LPARAM)&amp;g_ti) )
            if( ! SendMessage(tmp-&gt;hwnd, TTM_ADDTOOL, 0, (LPARAM)&amp;tmp-&gt;tInfo) )
                MessageBeep(0);  // just to have some error signal

            // subclass edit control
            SetWindowSubclass( hEdit, EditSubProc, 0, 0 );
        }
        return 0L;

        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hWnd, message, wParam, lParam);
    }

    return 0;
}