The MSDN feed MSDN Just Published interestingly has posted a link pointing to a code project article, which covers a simple C++ framework as an alternative to MFC. I have downloaded the core and sample source files and played with a few samples. Just as the author claims, this framework doesn't use macros, templates, MI as in MFC and WTL so it's much easier to comprehend. It bears a resemblance to the approach that .NET WinForm has taken when wrapping Win32 underneath. So kudos to the author for an exellent job!
The code project site has some reader feedback regarding the choice of using TLS to store the association of a HWND and its corresponding WndProc. Being always conscious about performance, my only concern, though, is related to the implementation of StaticWindowProc:
LRESULT CALLBACK CWnd::StaticWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
try
{
{
// Allocate an iterator for our HWND map
std::map<HWND, CWnd*, CompareHWND>::iterator m;
GetApp()->m_MapLock.Lock();
m = GetApp()->GetHWNDMap().find(hWnd);
GetApp()->m_MapLock.Release();
if (m != GetApp()->GetHWNDMap().end())
return m->second->WndProc(hWnd, uMsg, wParam, lParam);
}
throw CWinException(TEXT("CWnd::StaticWindowProc .. Failed to route message"));
}
catch (const CWinException &e)
{
e.MessageBox();
}
catch (...)
{
DebugErrMsg(TEXT("Exception in CWnd::StaticWindowProc"));
}
return ::DefWindowProc(hWnd, uMsg, wParam, lParam);;
} // LRESULT CALLBACK StaticWindowProc(...)
Finding the WndProc corresponding to a HWND in the STL map will go through a critial section via m_MapLock.Lock() and m_MapLock.Release(). Albeit the light nature of crtitical sections, I'd always be wary of using it in this scenario, where all the windows messages (keyboard, mouse, etc.) of all the CWnd subclasses in the application get funneled through this function.
Your thoughts? Please post comments.
0 comments:
Post a Comment