Annotation of sql/odbc/MFCpatches/winhand_.h, revision 1.1
1.1 ! paf 1: /////////////////////////////////////////////////////////////////////////////
! 2: // CHandleMap
! 3: //
! 4: // Note: Do not access the members of this class directly.
! 5: // Use CWnd::FromHandle, CDC::FromHandle, etc.
! 6: // The actual definition is only included because it is
! 7: // necessary for the definition of CWinThread.
! 8: //
! 9: // Most Windows objects are represented with a HANDLE, including
! 10: // the most important ones, HWND, HDC, HPEN, HFONT etc.
! 11: // We want C++ objects to wrap these handle based objects whenever we can.
! 12: // Since Windows objects can be created outside of C++ (eg: calling
! 13: // ::CreateWindow will return an HWND with no C++ wrapper) we must
! 14: // support a reasonably uniform mapping from permanent handles
! 15: // (i.e. the ones allocated in C++) and temporary handles (i.e.
! 16: // the ones allocated in C, but passed through a C++ interface.
! 17: // We keep two dictionaries for this purpose. The permanent dictionary
! 18: // stores those C++ objects that have been explicitly created by
! 19: // the developer. The C++ constructor for the wrapper class will
! 20: // insert the mapping into the permanent dictionary and the C++
! 21: // destructor will remove it and possibly free up the associated
! 22: // Windows object.
! 23: // When a handle passes through a C++ interface that doesn't exist in
! 24: // the permanent dictionary, we allocate a temporary wrapping object
! 25: // and store that mapping into the temporary dictionary.
! 26: // At idle time the temporary wrapping objects are flushed (since you better
! 27: // not be holding onto something you didn't create).
! 28: //
! 29:
! 30: #if _MSC_VER > 1000
! 31: #pragma once
! 32: #endif
! 33:
! 34: #include "fixalloc.h"
! 35:
! 36: template<class TYPE>
! 37: struct ConstructDestruct
! 38: {
! 39: static void PASCAL Construct(CObject* pObject)
! 40: {
! 41: new (pObject) TYPE;
! 42: }
! 43: static void PASCAL Destruct(CObject* pObject)
! 44: {
! 45: TYPE* p = (TYPE*)pObject;
! 46: p->~TYPE();
! 47: }
! 48: };
! 49:
! 50: class CWinThread; // forward reference for friend declaration
! 51:
! 52: class CHandleMap
! 53: {
! 54: private: // implementation
! 55: CFixedAllocNoSync m_alloc;
! 56: void (PASCAL* m_pfnConstructObject)(CObject* pObject);
! 57: void (PASCAL* m_pfnDestructObject)(CObject* pObject);
! 58: CMapPtrToPtr m_permanentMap;
! 59: CMapPtrToPtr m_temporaryMap;
! 60: CRuntimeClass* m_pClass;
! 61: size_t m_nOffset; // offset of handles in the object
! 62: int m_nHandles; // 1 or 2 (for CDC)
! 63:
! 64: // Constructor/Destructor
! 65: public:
! 66: CHandleMap(CRuntimeClass* pClass,
! 67: void (PASCAL* pfnConstructObject)(CObject* pObject),
! 68: void (PASCAL* pfnDestructObject)(CObject* pObject),
! 69: size_t nOffset, int nHandles = 1);
! 70: #ifdef _AFXDLL
! 71: ~CHandleMap()
! 72: #else
! 73: virtual ~CHandleMap()
! 74: #endif
! 75: { DeleteTemp(); }
! 76:
! 77: // Operations
! 78: public:
! 79: CObject* FromHandle(HANDLE h);
! 80: void DeleteTemp();
! 81:
! 82: void SetPermanent(HANDLE h, CObject* permOb);
! 83: void RemoveHandle(HANDLE h);
! 84:
! 85: CObject* LookupPermanent(HANDLE h);
! 86: CObject* LookupTemporary(HANDLE h);
! 87:
! 88:
! 89: friend class CWinThread;
! 90: };
! 91:
! 92: // Note: out-of-line _DEBUG version is in winhand.cpp
! 93: #ifndef _DEBUG
! 94: inline void CHandleMap::SetPermanent(HANDLE h, CObject* permOb)
! 95: { m_permanentMap[(LPVOID)h] = permOb; }
! 96:
! 97: inline void CHandleMap::RemoveHandle(HANDLE h)
! 98: {
! 99: // remove only from permanent map -- temporary objects are removed
! 100: // at idle in CHandleMap::DeleteTemp, always!
! 101: m_permanentMap.RemoveKey((LPVOID)h);
! 102: }
! 103: #endif
! 104:
! 105: inline CObject* CHandleMap::LookupPermanent(HANDLE h)
! 106: { return (CObject*)m_permanentMap.GetValueAt((LPVOID)h); }
! 107: inline CObject* CHandleMap::LookupTemporary(HANDLE h)
! 108: { return (CObject*)m_temporaryMap.GetValueAt((LPVOID)h); }
E-mail: