home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / WINHAND_.H_ / WINHAND_.H
Encoding:
C/C++ Source or Header  |  1993-02-08  |  3.1 KB  |  90 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library. 
  2. // Copyright (C) 1992 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 Microsoft 
  7. // QuickHelp and/or WinHelp documentation provided with the library. 
  8. // See these sources for detailed information regarding the 
  9. // Microsoft Foundation Classes product. 
  10.  
  11. /////////////////////////////////////////////////////////////////////////////
  12.  
  13. /* Included in this file:
  14.  *  Most Windows objects are represented with a HANDLE, including
  15.  *      the most important ones, HWND, HDC, HPEN, HFONT etc.
  16.  *  We want C++ objects to wrap these handle based objects whenever we can.
  17.  *  Since Windows objects can be created outside of C++ (eg: calling
  18.  *      ::CreateWindow will return an HWND with no C++ wrapper) we must
  19.  *      support a reasonably uniform mapping from permanent handles
  20.  *      (i.e. the ones allocated in C++) and temporary handles (i.e.
  21.  *      the ones allocated in C, but passed through a C++ interface.
  22.  *  We keep two dictionaries for this purpose.  The permanent dictionary
  23.  *      stores those C++ objects that have been explicitly created by
  24.  *      the developer.  The C++ constructor for the wrapper class will
  25.  *      insert the mapping into the permanent dictionary and the C++
  26.  *      destructor will remove it and possibly free up the associated
  27.  *      Windows object.
  28.  *  When a handle passes through a C++ interface that doesn't exist in
  29.  *      the permanent dictionary, we allocate a temporary wrapping object
  30.  *      and store that mapping into the temporary dictionary.
  31.  *  At idle time the temporary wrapping objects are flushed (since you better
  32.  *      not be holding onto something you didn't create).
  33.  */
  34.  
  35. #ifdef AFX_CORE1_SEG
  36. #pragma code_seg(AFX_CORE1_SEG)
  37. #endif
  38.  
  39. typedef WORD MAPTYPE;
  40.  
  41. // forward class declarations
  42. #ifndef _AFXDLL
  43. #ifdef AFX_CLASS_MODEL
  44. class NEAR CHandleMap
  45. #else
  46. class CHandleMap
  47. #endif
  48. #else
  49. class CHandleMap
  50. #endif
  51. {
  52. private:    // implementation
  53.     CMapWordToPtr m_permanentMap;
  54.     CMapWordToPtr m_temporaryMap;
  55.     CRuntimeClass*  m_pClass;
  56.     int m_nHandles;         // 1 or 2 (for CDC)
  57.  
  58. // Constructors
  59. public:
  60.     CHandleMap(CRuntimeClass* pClass, int nHandles = 1);
  61.  
  62. // Operations
  63. public:
  64.     CObject* FromHandle(HANDLE h);
  65.     void     DeleteTemp();
  66.  
  67.     void    SetPermanent(HANDLE h, CObject* permOb);
  68.     void    RemoveHandle(HANDLE h);
  69.  
  70.     BOOL    LookupPermanent(HANDLE h, CObject*& pObject)
  71.                 { return m_permanentMap.Lookup((MAPTYPE)h, (void*&)pObject); }
  72.     BOOL    LookupTemporary(HANDLE h, CObject*& pObject)
  73.                 { return m_temporaryMap.Lookup((MAPTYPE)h, (void*&)pObject); }
  74. };
  75.  
  76. /////////////////////////////////////////////////////////////////////////////
  77. // non-debug inline versions
  78.  
  79. #ifndef _DEBUG
  80. inline void CHandleMap::SetPermanent(HANDLE h, CObject* permOb)
  81. {
  82.     m_permanentMap[(MAPTYPE)h] = permOb;
  83. }
  84. #endif
  85.  
  86. /////////////////////////////////////////////////////////////////////////////
  87. #ifdef AFX_CORE1_SEG
  88. #pragma code_seg()
  89. #endif
  90.