afxstate

--------------------------------------------------------------------------------
//afxtls_.h
--------------------------------------------------------------------------------
CThreadLocal模板类,这个类很特别,没有数据成员,需要模板参数类型的数据时,都是new出来的

--------------------------------------------------------------------------------
//afxstat_.h
//afxstate.cpp
--------------------------------------------------------------------------------
EXTERN_THREAD_LOCAL(_AFX_THREAD_STATE, _afxThreadState)
THREAD_LOCAL(_AFX_THREAD_STATE, _afxThreadState)

AFX_THREAD_STATE* AfxGetThreadState();
AFX_MODULE_STATE* AfxGetModuleState();
AFX_MODULE_THREAD_STATE* AfxGetModalThreadState();
Afx_MODULE_STATE* AfxGetStaticModuleState();

AFX_THREAD_STATE包含AFX_MODULE_STATE* m_pModuleState;AFX_MODULE_STATE* m_pPrevModuleState;
AFX_MODULE_STATE包含CThreadLocal<AFX_MODULE_THREAD_STATE> m_thread;

--------------------------------------------------------------------------------
// 关于AFX_MANAGER_STATE(AfxGetStaticModuleState())
--------------------------------------------------------------------------------
// 宏展开
#define AFX_MANAGE_STATE_NO_INIT_MANAGED(p) AFX_MAINTAIN_STATE2 _ctlState(p);
#define AFX_MANAGE_STATE(p) _AfxInitManaged(); AFX_MANAGE_STATE_NO_INIT_MANAGED(p)

// AfxGetStaticModuleState函数
class _AFX_DLL_MODULE_STATE : public AFX_MODULE_STATE
{
public:
    _AFX_DLL_MODULE_STATE() : AFX_MODULE_STATE(TRUE, AfxWndProcDllStatic, _MFC_VER)
        { }
};

static _AFX_DLL_MODULE_STATE afxModuleState;
AFX_MODULE_STATE* AFXAPI AfxGetStaticModuleState()
{
    AFX_MODULE_STATE* pModuleState = &afxModuleState;
    return pModuleState;
}

//AFX_MAINTAIN_STATE2类的构造函数
AFX_MAINTAIN_STATE2::AFX_MAINTAIN_STATE2(AFX_MODULE_STATE* pNewState) throw()
{
#ifdef _AFXDLL
    m_pThreadState = _afxThreadState.GetData();
    ASSERT(m_pThreadState);
    if(m_pThreadState)
    {
        m_pPrevModuleState = m_pThreadState->m_pModuleState;
        m_pThreadState->m_pModuleState = pNewState;
    }
    else
    {
        // This is a very bad state; we have no good way to report the error at this moment
        // since exceptions from here are not expected
        m_pPrevModuleState=NULL;
        m_pThreadState=NULL;
    }
#endif
}
posted @ 2012-10-29 18:15  荷包蛋  阅读(851)  评论(0编辑  收藏  举报