1
0

winhand_.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. /////////////////////////////////////////////////////////////////////////////
  11. // CHandleMap
  12. //
  13. // Note: Do not access the members of this class directly.
  14. // Use CWnd::FromHandle, CDC::FromHandle, etc.
  15. // The actual definition is only included because it is
  16. // necessary for the definition of CWinThread.
  17. //
  18. // Most Windows objects are represented with a HANDLE, including
  19. // the most important ones, HWND, HDC, HPEN, HFONT etc.
  20. // We want C++ objects to wrap these handle based objects whenever we can.
  21. // Since Windows objects can be created outside of C++ (eg: calling
  22. // ::CreateWindow will return an HWND with no C++ wrapper) we must
  23. // support a reasonably uniform mapping from permanent handles
  24. // (i.e. the ones allocated in C++) and temporary handles (i.e.
  25. // the ones allocated in C, but passed through a C++ interface.
  26. // We keep two dictionaries for this purpose. The permanent dictionary
  27. // stores those C++ objects that have been explicitly created by
  28. // the developer. The C++ constructor for the wrapper class will
  29. // insert the mapping into the permanent dictionary and the C++
  30. // destructor will remove it and possibly free up the associated
  31. // Windows object.
  32. // When a handle passes through a C++ interface that doesn't exist in
  33. // the permanent dictionary, we allocate a temporary wrapping object
  34. // and store that mapping into the temporary dictionary.
  35. // At idle time the temporary wrapping objects are flushed (since you better
  36. // not be holding onto something you didn't create).
  37. //
  38. class CWinThread; // forward reference for friend declaration
  39. class CHandleMap
  40. {
  41. private: // implementation
  42. CMapPtrToPtr m_permanentMap;
  43. CMapPtrToPtr m_temporaryMap;
  44. CRuntimeClass* m_pClass;
  45. size_t m_nOffset; // offset of handles in the object
  46. int m_nHandles; // 1 or 2 (for CDC)
  47. // Constructor/Destructor
  48. public:
  49. CHandleMap(CRuntimeClass* pClass, size_t nOffset, int nHandles = 1);
  50. #ifdef _AFXDLL
  51. ~CHandleMap()
  52. #else
  53. virtual ~CHandleMap()
  54. #endif
  55. { DeleteTemp(); }
  56. // Operations
  57. public:
  58. CObject* FromHandle(HANDLE h);
  59. void DeleteTemp();
  60. void SetPermanent(HANDLE h, CObject* permOb);
  61. void RemoveHandle(HANDLE h);
  62. CObject* LookupPermanent(HANDLE h);
  63. CObject* LookupTemporary(HANDLE h);
  64. friend class CWinThread;
  65. };
  66. // Note: out-of-line _DEBUG version is in winhand.cpp
  67. #ifndef _DEBUG
  68. inline void CHandleMap::SetPermanent(HANDLE h, CObject* permOb)
  69. { m_permanentMap[(LPVOID)h] = permOb; }
  70. inline void CHandleMap::RemoveHandle(HANDLE h)
  71. {
  72. // remove only from permanent map -- temporary objects are removed
  73. // at idle in CHandleMap::DeleteTemp, always!
  74. m_permanentMap.RemoveKey((LPVOID)h);
  75. }
  76. #endif
  77. inline CObject* CHandleMap::LookupPermanent(HANDLE h)
  78. { return (CObject*)m_permanentMap.GetValueAt((LPVOID)h); }
  79. inline CObject* CHandleMap::LookupTemporary(HANDLE h)
  80. { return (CObject*)m_temporaryMap.GetValueAt((LPVOID)h); }